From 5aaa60e30a5695c2a5eb49283388fa36809e389e Mon Sep 17 00:00:00 2001 From: A Vertex SDK engineer Date: Wed, 22 Oct 2025 14:45:12 -0700 Subject: [PATCH] feat: add more attributes to OTel resource for ADK tracing PiperOrigin-RevId: 822747314 --- .../test_agent_engine_templates_adk.py | 22 +++++++++++++++++-- vertexai/agent_engines/templates/adk.py | 9 +++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/tests/unit/vertex_adk/test_agent_engine_templates_adk.py b/tests/unit/vertex_adk/test_agent_engine_templates_adk.py index 1bcbbf4de7..2da0429c37 100644 --- a/tests/unit/vertex_adk/test_agent_engine_templates_adk.py +++ b/tests/unit/vertex_adk/test_agent_engine_templates_adk.py @@ -27,6 +27,7 @@ from vertexai import agent_engines from google.genai import types import pytest +import uuid try: @@ -521,10 +522,23 @@ def test_custom_instrumentor_enablement( else: custom_instrumentor.assert_not_called() - @mock.patch.dict(os.environ, {"GOOGLE_CLOUD_AGENT_ENGINE_ID": "test_agent_id"}) + @mock.patch.dict( + os.environ, + { + "GOOGLE_CLOUD_AGENT_ENGINE_ID": "test_agent_id", + "OTEL_RESOURCE_ATTRIBUTES": "some-attribute=some-value", + }, + ) def test_tracing_setup( - self, trace_provider_mock: mock.Mock, cloud_trace_exporter_mock: mock.Mock + self, + trace_provider_mock: mock.Mock, + cloud_trace_exporter_mock: mock.Mock, + monkeypatch, ): + monkeypatch.setattr( + "uuid.uuid4", lambda: uuid.UUID("12345678123456781234567812345678") + ) + monkeypatch.setattr("os.getpid", lambda: 123123123) app = agent_engines.AdkApp(agent=_TEST_AGENT, enable_tracing=True) app.set_up() @@ -533,8 +547,12 @@ def test_tracing_setup( "telemetry.sdk.name": "opentelemetry", "telemetry.sdk.version": "1.36.0", "gcp.project_id": "test-project", + "cloud.account.id": "test-project", "service.name": "test_agent_id", "cloud.resource_id": "//aiplatform.googleapis.com/projects/test-project/locations/us-central1/reasoningEngines/test_agent_id", + "service.instance.id": "12345678123456781234567812345678-123123123", + "cloud.region": "us-central1", + "some-attribute": "some-value", } @dataclasses.dataclass diff --git a/vertexai/agent_engines/templates/adk.py b/vertexai/agent_engines/templates/adk.py index b70c293291..51331a7c2c 100644 --- a/vertexai/agent_engines/templates/adk.py +++ b/vertexai/agent_engines/templates/adk.py @@ -273,18 +273,25 @@ def _detect_cloud_resource_id(project_id: str) -> Optional[str]: "opentelemetry-sdk", needed_for_tracing=True, needed_for_logging=True ) + import uuid + + # Provide a set of resource attributes but allow to override them with env + # variables like OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME. cloud_resource_id = _detect_cloud_resource_id(project_id) resource = opentelemetry.sdk.resources.Resource.create( attributes={ "gcp.project_id": project_id, + "cloud.account.id": project_id, "service.name": os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", ""), + "service.instance.id": f"{uuid.uuid4().hex}-{os.getpid()}", + "cloud.region": os.getenv("GOOGLE_CLOUD_LOCATION", ""), } | ( {"cloud.resource_id": cloud_resource_id} if cloud_resource_id is not None else {} ) - ) + ).merge(opentelemetry.sdk.resources.OTELResourceDetector().detect()) if enable_tracing: try: