Skip to content

Commit c0be1df

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: set per-tool user agent in BQ calls and tool label in BQ jobs
This will help per tool usage for BigQuery tools. PiperOrigin-RevId: 829142106
1 parent f1f4467 commit c0be1df

File tree

6 files changed

+463
-230
lines changed

6 files changed

+463
-230
lines changed

src/google/adk/tools/bigquery/client.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@
2525
USER_AGENT = f"adk-bigquery-tool google-adk/{version.__version__}"
2626

2727

28+
from typing import List
29+
from typing import Union
30+
31+
2832
def get_bigquery_client(
2933
*,
3034
project: Optional[str],
3135
credentials: Credentials,
3236
location: Optional[str] = None,
33-
user_agent: Optional[str] = None,
37+
user_agent: Optional[Union[str, List[str]]] = None,
3438
) -> bigquery.Client:
3539
"""Get a BigQuery client.
3640
@@ -44,9 +48,16 @@ def get_bigquery_client(
4448
A BigQuery client.
4549
"""
4650

47-
user_agent = f"{USER_AGENT} {user_agent}" if user_agent else USER_AGENT
51+
user_agents = [USER_AGENT]
52+
if user_agent:
53+
if isinstance(user_agent, str):
54+
user_agents.append(user_agent)
55+
else:
56+
user_agents.extend([ua for ua in user_agent if ua])
4857

49-
client_info = google.api_core.client_info.ClientInfo(user_agent=user_agent)
58+
client_info = google.api_core.client_info.ClientInfo(
59+
user_agent=" ".join(user_agents)
60+
)
5061

5162
bigquery_client = bigquery.Client(
5263
project=project,

src/google/adk/tools/bigquery/metadata_tool.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def list_dataset_ids(
5151
project=project_id,
5252
credentials=credentials,
5353
location=settings.location,
54-
user_agent=settings.application_name,
54+
user_agent=[settings.application_name, "list_dataset_ids"],
5555
)
5656

5757
datasets = []
@@ -123,7 +123,7 @@ def get_dataset_info(
123123
project=project_id,
124124
credentials=credentials,
125125
location=settings.location,
126-
user_agent=settings.application_name,
126+
user_agent=[settings.application_name, "get_dataset_info"],
127127
)
128128
dataset = bq_client.get_dataset(
129129
bigquery.DatasetReference(project_id, dataset_id)
@@ -162,7 +162,7 @@ def list_table_ids(
162162
project=project_id,
163163
credentials=credentials,
164164
location=settings.location,
165-
user_agent=settings.application_name,
165+
user_agent=[settings.application_name, "list_table_ids"],
166166
)
167167

168168
tables = []
@@ -285,7 +285,7 @@ def get_table_info(
285285
project=project_id,
286286
credentials=credentials,
287287
location=settings.location,
288-
user_agent=settings.application_name,
288+
user_agent=[settings.application_name, "get_table_info"],
289289
)
290290
return bq_client.get_table(
291291
bigquery.TableReference(
@@ -579,8 +579,9 @@ def get_job_info(
579579
project=project_id,
580580
credentials=credentials,
581581
location=settings.location,
582-
user_agent=settings.application_name,
582+
user_agent=[settings.application_name, "get_job_info"],
583583
)
584+
584585
job = bq_client.get_job(job_id)
585586
# We need to use _properties to get the job info because it contains all
586587
# the job info.

0 commit comments

Comments
 (0)