Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions tests/unit/vertex_adk/test_agent_engine_templates_adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from vertexai import agent_engines
from google.genai import types
import pytest
import uuid


try:
Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion vertexai/agent_engines/templates/adk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down