From a08bf62b951ad788665a23a5084e210351e0895e Mon Sep 17 00:00:00 2001 From: Google Team Member Date: Thu, 5 Feb 2026 01:57:54 -0800 Subject: [PATCH] feat(otel): add extra attributes to span generated with `opentelemetry-instrumentation-google-genai` PiperOrigin-RevId: 865825792 --- src/google/adk/telemetry/tracing.py | 4 ++++ tests/unittests/telemetry/test_spans.py | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/google/adk/telemetry/tracing.py b/src/google/adk/telemetry/tracing.py index 42977414..cd6b7071 100644 --- a/src/google/adk/telemetry/tracing.py +++ b/src/google/adk/telemetry/tracing.py @@ -52,6 +52,7 @@ from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_A from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_USAGE_INPUT_TOKENS from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_USAGE_OUTPUT_TOKENS from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GenAiSystemValues +from opentelemetry.semconv._incubating.attributes.user_attributes import USER_ID from opentelemetry.semconv.schemas import Schemas from opentelemetry.trace import Span from opentelemetry.util.types import AnyValue @@ -438,8 +439,11 @@ def use_generate_content_span( """ common_attributes = { + GEN_AI_AGENT_NAME: invocation_context.agent.name, GEN_AI_CONVERSATION_ID: invocation_context.session.id, + USER_ID: invocation_context.session.user_id, 'gcp.vertex.agent.event_id': model_response_event.id, + 'gcp.vertex.agent.invocation_id': invocation_context.invocation_id, } if ( _is_gemini_agent(invocation_context.agent) diff --git a/tests/unittests/telemetry/test_spans.py b/tests/unittests/telemetry/test_spans.py index ed7a588b..2deebda1 100644 --- a/tests/unittests/telemetry/test_spans.py +++ b/tests/unittests/telemetry/test_spans.py @@ -34,6 +34,7 @@ from google.adk.telemetry.tracing import use_generate_content_span from google.adk.tools.base_tool import BaseTool from google.genai import types from opentelemetry._logs import LogRecord +from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_AGENT_NAME from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_CONVERSATION_ID from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_OPERATION_NAME from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_REQUEST_MODEL @@ -41,6 +42,7 @@ from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_A from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_SYSTEM from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_USAGE_INPUT_TOKENS from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_USAGE_OUTPUT_TOKENS +from opentelemetry.semconv._incubating.attributes.user_attributes import USER_ID import pytest @@ -709,8 +711,11 @@ async def test_generate_content_span( mock_span.set_attribute.assert_any_call(GEN_AI_USAGE_OUTPUT_TOKENS, 20) mock_span.set_attributes.assert_called_once_with({ + GEN_AI_AGENT_NAME: invocation_context.agent.name, GEN_AI_CONVERSATION_ID: invocation_context.session.id, + USER_ID: invocation_context.session.user_id, 'gcp.vertex.agent.event_id': 'event-123', + 'gcp.vertex.agent.invocation_id': invocation_context.invocation_id, }) # Assert Logs