Skip to content

Commit 5aaa60e

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: add more attributes to OTel resource for ADK tracing
PiperOrigin-RevId: 822747314
1 parent cd99635 commit 5aaa60e

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

tests/unit/vertex_adk/test_agent_engine_templates_adk.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from vertexai import agent_engines
2828
from google.genai import types
2929
import pytest
30+
import uuid
3031

3132

3233
try:
@@ -521,10 +522,23 @@ def test_custom_instrumentor_enablement(
521522
else:
522523
custom_instrumentor.assert_not_called()
523524

524-
@mock.patch.dict(os.environ, {"GOOGLE_CLOUD_AGENT_ENGINE_ID": "test_agent_id"})
525+
@mock.patch.dict(
526+
os.environ,
527+
{
528+
"GOOGLE_CLOUD_AGENT_ENGINE_ID": "test_agent_id",
529+
"OTEL_RESOURCE_ATTRIBUTES": "some-attribute=some-value",
530+
},
531+
)
525532
def test_tracing_setup(
526-
self, trace_provider_mock: mock.Mock, cloud_trace_exporter_mock: mock.Mock
533+
self,
534+
trace_provider_mock: mock.Mock,
535+
cloud_trace_exporter_mock: mock.Mock,
536+
monkeypatch,
527537
):
538+
monkeypatch.setattr(
539+
"uuid.uuid4", lambda: uuid.UUID("12345678123456781234567812345678")
540+
)
541+
monkeypatch.setattr("os.getpid", lambda: 123123123)
528542
app = agent_engines.AdkApp(agent=_TEST_AGENT, enable_tracing=True)
529543
app.set_up()
530544

@@ -533,8 +547,12 @@ def test_tracing_setup(
533547
"telemetry.sdk.name": "opentelemetry",
534548
"telemetry.sdk.version": "1.36.0",
535549
"gcp.project_id": "test-project",
550+
"cloud.account.id": "test-project",
536551
"service.name": "test_agent_id",
537552
"cloud.resource_id": "//aiplatform.googleapis.com/projects/test-project/locations/us-central1/reasoningEngines/test_agent_id",
553+
"service.instance.id": "12345678123456781234567812345678-123123123",
554+
"cloud.region": "us-central1",
555+
"some-attribute": "some-value",
538556
}
539557

540558
@dataclasses.dataclass

vertexai/agent_engines/templates/adk.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,25 @@ def _detect_cloud_resource_id(project_id: str) -> Optional[str]:
273273
"opentelemetry-sdk", needed_for_tracing=True, needed_for_logging=True
274274
)
275275

276+
import uuid
277+
278+
# Provide a set of resource attributes but allow to override them with env
279+
# variables like OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME.
276280
cloud_resource_id = _detect_cloud_resource_id(project_id)
277281
resource = opentelemetry.sdk.resources.Resource.create(
278282
attributes={
279283
"gcp.project_id": project_id,
284+
"cloud.account.id": project_id,
280285
"service.name": os.getenv("GOOGLE_CLOUD_AGENT_ENGINE_ID", ""),
286+
"service.instance.id": f"{uuid.uuid4().hex}-{os.getpid()}",
287+
"cloud.region": os.getenv("GOOGLE_CLOUD_LOCATION", ""),
281288
}
282289
| (
283290
{"cloud.resource_id": cloud_resource_id}
284291
if cloud_resource_id is not None
285292
else {}
286293
)
287-
)
294+
).merge(opentelemetry.sdk.resources.OTELResourceDetector().detect())
288295

289296
if enable_tracing:
290297
try:

0 commit comments

Comments
 (0)