feat(otel): add extra attributes to span generated with opentelemetry-instrumentation-google-genai

PiperOrigin-RevId: 865825792
This commit is contained in:
Google Team Member
2026-02-05 01:57:54 -08:00
committed by Copybara-Service
parent e752bbb756
commit a08bf62b95
2 changed files with 9 additions and 0 deletions
+4
View File
@@ -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)
+5
View File
@@ -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