22import inspect
33from google .auth import credentials
44from google .auth .exceptions import RefreshError
5+ from google .api_core .client_info import ClientInfo
56from google .cloud import bigquery
67from google .cloud .exceptions import Forbidden
78from google .cloud .bigquery ._http import Connection
89from kaggle_secrets import GcpTarget , UserSecretsClient
910
1011from log import Log
1112
13+ KAGGLE_GCP_CLIENT_USER_AGENT = "kaggle-gcp-client/1.0"
1214
1315def get_integrations ():
1416 kernel_integrations_var = os .getenv ("KAGGLE_KERNEL_INTEGRATIONS" )
@@ -165,6 +167,7 @@ def monkeypatch_bq(bq_client, *args, **kwargs):
165167 Log .info ("No project specified while using the unmodified client." )
166168 print ('Please ensure you specify a project id when creating the client'
167169 ' in order to use your BigQuery account.' )
170+ kwargs ['client_info' ] = set_kaggle_user_agent (kwargs .get ('client_info' ))
168171 return bq_client (* args , ** kwargs )
169172
170173 # Monkey patches BigQuery client creation to use proxy or user-connected GCP account.
@@ -183,11 +186,28 @@ def patched_init(self, *args, **kwargs):
183186 if specified_credentials is None :
184187 Log .info ("No credentials specified, using KaggleKernelCredentials." )
185188 kwargs ['credentials' ] = kaggle_kernel_credentials
189+
190+ # TODO(vimota): Remove the exclusion of TablesClient once
191+ # the client has fixed the error:
192+ # `multiple values for keyword argument 'client_info'``
193+ from google .cloud import automl_v1beta1
194+ if (client_klass != automl_v1beta1 .TablesClient ):
195+ kwargs ['client_info' ] = set_kaggle_user_agent (kwargs .get ('client_info' ))
196+
197+
186198 return client_init (self , * args , ** kwargs )
187199
188200 if (not has_been_monkeypatched (client_klass .__init__ )):
189201 client_klass .__init__ = patched_init
190202
203+ def set_kaggle_user_agent (client_info : ClientInfo ):
204+ # Add kaggle client user agent in order to attribute usage.
205+ if client_info is None :
206+ client_info = ClientInfo (user_agent = KAGGLE_GCP_CLIENT_USER_AGENT )
207+ else :
208+ client_info .user_agent = KAGGLE_GCP_CLIENT_USER_AGENT
209+ return client_info
210+
191211def init_gcs ():
192212 is_user_secrets_token_set = "KAGGLE_USER_SECRETS_TOKEN" in os .environ
193213 from google .cloud import storage
0 commit comments