Skip to content

Commit b542b96

Browse files
committed
Add kaggle user agent to gcp integration client.s
1 parent 4634370 commit b542b96

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

patches/kaggle_gcp.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
import inspect
33
from google.auth import credentials
44
from google.auth.exceptions import RefreshError
5+
from google.api_core.client_info import ClientInfo
56
from google.cloud import bigquery
67
from google.cloud.exceptions import Forbidden
78
from google.cloud.bigquery._http import Connection
89
from kaggle_secrets import GcpTarget, UserSecretsClient
910

1011
from log import Log
1112

13+
KAGGLE_GCP_CLIENT_USER_AGENT="kaggle-gcp-client/1.0"
1214

1315
def 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+
191211
def init_gcs():
192212
is_user_secrets_token_set = "KAGGLE_USER_SECRETS_TOKEN" in os.environ
193213
from google.cloud import storage

tests/test_automl.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ def test_version(self):
1919
self.assertGreaterEqual(version, 0.5);
2020

2121
class FakeClient:
22-
def __init__(self, credentials=None, **kwargs):
22+
def __init__(self, credentials=None, client_info=None, **kwargs):
2323
self.credentials = credentials
2424

25+
class FakeConnection():
26+
def __init__(self, user_agent):
27+
self.user_agent = user_agent
28+
if (client_info is not None):
29+
self._connection = FakeConnection(client_info.user_agent)
30+
2531
@patch("google.cloud.automl_v1beta1.AutoMlClient", new=FakeClient)
2632
def test_user_provided_credentials(self):
2733
credentials = _make_credentials()
@@ -64,6 +70,7 @@ def test_default_credentials_automl_client(self):
6470
automl_client = automl.AutoMlClient()
6571
self.assertIsNotNone(automl_client.credentials)
6672
self.assertIsInstance(automl_client.credentials, KaggleKernelCredentials)
73+
self.assertTrue(automl_client._connection.user_agent.startswith("kaggle-gcp-client/1.0"))
6774

6875
@patch("google.cloud.automl_v1beta1.TablesClient", new=FakeClient)
6976
def test_default_credentials_tables_client(self):

tests/test_bigquery.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def test_project_with_connected_account_default_credentials(self, mock_access_to
103103
env.set('KAGGLE_KERNEL_INTEGRATIONS', 'BIGQUERY')
104104
with env:
105105
client = bigquery.Client(project='ANOTHER_PROJECT')
106+
self.assertTrue(client._connection.user_agent.startswith("kaggle-gcp-client/1.0"))
106107
self._test_integration(client)
107108

108109
@patch.object(Connection, 'API_BASE_URL')

tests/test_gcs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def test_default_credentials_gcs_enabled(self):
4444
init_gcs()
4545
client = storage.Client(project="xyz")
4646
self.assertIsInstance(client._credentials, KaggleKernelCredentials)
47+
self.assertTrue(client._connection.user_agent.startswith("kaggle-gcp-client/1.0"))
4748

4849
def test_monkeypatching_idempotent(self):
4950
env = EnvironmentVarGuard()

0 commit comments

Comments
 (0)