Skip to content

Commit 0f90ff4

Browse files
google-genai-botcopybara-github
authored andcommitted
chore: Adding adk header to track MB and session service usage via ADK SDK
PiperOrigin-RevId: 938299570
1 parent 2b1c932 commit 0f90ff4

4 files changed

Lines changed: 39 additions & 10 deletions

File tree

src/google/adk/memory/vertex_ai_memory_bank_service.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from google.genai import types
2727
from typing_extensions import override
2828

29+
from ..utils._google_client_headers import get_tracking_headers
2930
from ..utils.vertex_ai_utils import get_express_mode_api_key
3031
from .base_memory_service import BaseMemoryService
3132
from .base_memory_service import SearchMemoryResponse
@@ -616,9 +617,17 @@ def _get_api_client(self) -> vertexai.AsyncClient:
616617
"""
617618
import vertexai
618619

620+
http_options = types.HttpOptions(headers=get_tracking_headers())
619621
if self._express_mode_api_key:
620-
return vertexai.Client(api_key=self._express_mode_api_key).aio
621-
return vertexai.Client(project=self._project, location=self._location).aio
622+
return vertexai.Client(
623+
http_options=http_options,
624+
api_key=self._express_mode_api_key,
625+
).aio
626+
return vertexai.Client(
627+
project=self._project,
628+
location=self._location,
629+
http_options=http_options,
630+
).aio
622631

623632

624633
def _log_ingest_task_error(task: asyncio.Task) -> None:

src/google/adk/sessions/vertex_ai_session_service.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from typing import Any
2323
from typing import Optional
2424
from typing import TYPE_CHECKING
25-
from typing import Union
2625

2726
from google.genai import types
2827
from google.genai.errors import ClientError
@@ -36,6 +35,7 @@
3635
from ..events.event import Event
3736
from ..events.event_actions import EventActions
3837
from ..events.event_actions import EventCompaction
38+
from ..utils._google_client_headers import get_tracking_headers
3939
from ..utils.vertex_ai_utils import get_express_mode_api_key
4040
from .base_session_service import BaseSessionService
4141
from .base_session_service import GetSessionConfig
@@ -506,11 +506,6 @@ def _get_reasoning_engine_id(self, app_name: str):
506506

507507
return match.groups()[-1]
508508

509-
def _api_client_http_options_override(
510-
self,
511-
) -> Optional[Union[types.HttpOptions, types.HttpOptionsDict]]:
512-
return None
513-
514509
def _get_api_client(self) -> vertexai.AsyncClient:
515510
"""Instantiates an API client for the given project and location.
516511
@@ -519,15 +514,16 @@ def _get_api_client(self) -> vertexai.AsyncClient:
519514
"""
520515
import vertexai
521516

517+
http_options = types.HttpOptions(headers=get_tracking_headers())
522518
if self._express_mode_api_key:
523519
return vertexai.Client(
524-
http_options=self._api_client_http_options_override(),
520+
http_options=http_options,
525521
api_key=self._express_mode_api_key,
526522
).aio
527523
return vertexai.Client(
528524
project=self._project,
529525
location=self._location,
530-
http_options=self._api_client_http_options_override(),
526+
http_options=http_options,
531527
).aio
532528

533529

tests/unittests/memory/test_vertex_ai_memory_bank_service.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,3 +1205,15 @@ async def failing_async_iterator():
12051205

12061206
assert len(result.memories) == 1
12071207
assert result.memories[0].content.parts[0].text == 'good fact'
1208+
1209+
1210+
def test_get_api_client_attaches_tracking_headers():
1211+
memory_service = mock_vertex_ai_memory_bank_service()
1212+
with mock.patch('vertexai.Client') as mock_client:
1213+
_ = memory_service._get_api_client()
1214+
mock_client.assert_called_once()
1215+
_, kwargs = mock_client.call_args
1216+
assert 'http_options' in kwargs
1217+
http_options = kwargs['http_options']
1218+
assert 'x-goog-api-client' in http_options.headers
1219+
assert 'google-adk/' in http_options.headers['x-goog-api-client']

tests/unittests/sessions/test_vertex_ai_session_service.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,3 +1522,15 @@ async def test_get_session_strips_full_resource_name(
15221522
mock_api_client_instance.agent_engines.sessions.get.assert_called_once_with(
15231523
name='reasoningEngines/123/sessions/session-123'
15241524
)
1525+
1526+
1527+
def test_get_api_client_attaches_tracking_headers():
1528+
session_service = mock_vertex_ai_session_service()
1529+
with mock.patch('vertexai.Client') as mock_client:
1530+
_ = session_service._get_api_client()
1531+
mock_client.assert_called_once()
1532+
_, kwargs = mock_client.call_args
1533+
assert 'http_options' in kwargs
1534+
http_options = kwargs['http_options']
1535+
assert 'x-goog-api-client' in http_options.headers
1536+
assert 'google-adk/' in http_options.headers['x-goog-api-client']

0 commit comments

Comments
 (0)