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
11 changes: 7 additions & 4 deletions src/google/adk/telemetry/_experimental_semconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,13 +678,16 @@ def set_operation_details_attributes_from_response(
_build_response_common_attributes(llm_response)
)
response_attributes = _build_response_operation_details(llm_response)
# A turn arriving as several responses is reported as all of them rather than
# whichever came last, one message each, as
# opentelemetry-instrumentation-google-genai reports a stream too.
# Several responses for one call are chunks no aggregator assembled
# (`partial` unset), so they extend one message. Parts are appended rather
# than text-joined: thought and answer text are both `text` parts here.
recorded = operation_details_attributes.get(GEN_AI_OUTPUT_MESSAGES)
arrived = response_attributes.get(GEN_AI_OUTPUT_MESSAGES)
if isinstance(recorded, list) and isinstance(arrived, list):
response_attributes[GEN_AI_OUTPUT_MESSAGES] = recorded + arrived
parts = [*recorded[0]['parts'], *arrived[0]['parts']]
response_attributes[GEN_AI_OUTPUT_MESSAGES] = [
{**arrived[0], 'parts': parts}
]
operation_details_attributes.update(response_attributes)


Expand Down
13 changes: 10 additions & 3 deletions src/google/adk/telemetry/_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,19 @@ def record_llm_response(
self, invocation_context: InvocationContext, response: LlmResponse
) -> None:
self._llm_responses.append(response)
# Anything after the span ended (a stream's aggregate, a second complete
# response) is neither traced nor timed, but still counts for the metrics.
# Anything after the span ended (a second complete response) is neither
# traced nor timed, but still counts for the metrics.
if self._inference_span_ended:
return

self._record_chunk_timing(response)
# A stream's aggregate (`partial=False`) is assembled by ADK rather than
# waited for, so only the chunks are timed.
if response.partial is not False:
self._record_chunk_timing(response)
# A `partial` chunk is not traced: its aggregator yields the whole answer
# after it, with `partial=False`.
if response.partial:
return
tracing.trace_inference_result(invocation_context, self.span, response)
# The inference ends at the response that says why generation stopped. End
# the span there, before the response is handed back to the caller, so what
Expand Down
2 changes: 2 additions & 0 deletions src/google/adk/telemetry/_web_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ def _restored_attributes(log_record: LogRecord) -> dict[str, AnyValue]:
content: dict[str, AnyValue] = {}
set_operation_details_attributes_from_request(content, scope.llm_request)
for llm_response in scope.llm_responses:
if llm_response.partial:
continue
set_operation_details_attributes_from_response(llm_response, content, {})
# The event stops at the finish reason, as InferenceScope does.
if is_reported_finish_reason(llm_response.finish_reason):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@
"gcp.vertex.agent.event_id": "PRESENT",
"gcp.vertex.agent.invocation_id": "PRESENT",
"user.id": "test_user",
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.usage.input_tokens": 150,
"gen_ai.usage.output_tokens": 50,
"gen_ai.usage.cache_read.input_tokens": 60,
"gen_ai.usage.reasoning.output_tokens": 15,
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.input.messages": [
{
"role": "user",
Expand Down Expand Up @@ -286,17 +286,7 @@
"role": "assistant",
"parts": [
{
"content": "text ",
"type": "text"
}
],
"finish_reason": ""
},
{
"role": "assistant",
"parts": [
{
"content": "response",
"content": "text response",
"type": "text"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@
"gcp.vertex.agent.invocation_id": "PRESENT",
"gen_ai.provider.name": "gcp.gemini",
"user.id": "test_user",
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.usage.input_tokens": 150,
"gen_ai.usage.output_tokens": 50,
"gen_ai.usage.cache_read.input_tokens": 60,
"gen_ai.usage.reasoning.output_tokens": 15,
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.input.messages": [
{
"role": "user",
Expand Down Expand Up @@ -247,17 +247,7 @@
"role": "assistant",
"parts": [
{
"content": "text ",
"type": "text"
}
],
"finish_reason": ""
},
{
"role": "assistant",
"parts": [
{
"content": "response",
"content": "text response",
"type": "text"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@
"gen_ai.conversation.id": "PRESENT",
"gcp.vertex.agent.event_id": "PRESENT",
"gcp.vertex.agent.invocation_id": "PRESENT",
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.usage.input_tokens": 150,
"gen_ai.usage.output_tokens": 50,
"gen_ai.usage.cache_read.input_tokens": 60,
"gen_ai.usage.reasoning.output_tokens": 15,
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.tool.definitions": [
{
"name": "some_tool",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@
"gcp.vertex.agent.event_id": "PRESENT",
"gcp.vertex.agent.invocation_id": "PRESENT",
"gen_ai.provider.name": "gcp.gemini",
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.usage.input_tokens": 150,
"gen_ai.usage.output_tokens": 50,
"gen_ai.usage.cache_read.input_tokens": 60,
"gen_ai.usage.reasoning.output_tokens": 15,
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.tool.definitions": [
{
"name": "some_tool",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,17 +305,7 @@
"role": "assistant",
"parts": [
{
"content": "text ",
"type": "text"
}
],
"finish_reason": ""
},
{
"role": "assistant",
"parts": [
{
"content": "response",
"content": "text response",
"type": "text"
}
],
Expand All @@ -335,13 +325,13 @@
"gcp.vertex.agent.event_id": "PRESENT",
"gcp.vertex.agent.invocation_id": "PRESENT",
"user.id": "test_user",
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.usage.input_tokens": 150,
"gen_ai.usage.output_tokens": 50,
"gen_ai.usage.cache_read.input_tokens": 60,
"gen_ai.usage.reasoning.output_tokens": 15,
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.input.messages": [
{
"role": "user",
Expand Down Expand Up @@ -409,17 +399,7 @@
"role": "assistant",
"parts": [
{
"content": "text ",
"type": "text"
}
],
"finish_reason": ""
},
{
"role": "assistant",
"parts": [
{
"content": "response",
"content": "text response",
"type": "text"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,7 @@
"role": "assistant",
"parts": [
{
"content": "text ",
"type": "text"
}
],
"finish_reason": ""
},
{
"role": "assistant",
"parts": [
{
"content": "response",
"content": "text response",
"type": "text"
}
],
Expand All @@ -302,13 +292,13 @@
"gcp.vertex.agent.invocation_id": "PRESENT",
"gen_ai.provider.name": "gcp.gemini",
"user.id": "test_user",
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.usage.input_tokens": 150,
"gen_ai.usage.output_tokens": 50,
"gen_ai.usage.cache_read.input_tokens": 60,
"gen_ai.usage.reasoning.output_tokens": 15,
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.input.messages": [
{
"role": "user",
Expand Down Expand Up @@ -376,17 +366,7 @@
"role": "assistant",
"parts": [
{
"content": "text ",
"type": "text"
}
],
"finish_reason": ""
},
{
"role": "assistant",
"parts": [
{
"content": "response",
"content": "text response",
"type": "text"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,7 @@
"role": "assistant",
"parts": [
{
"content": "text ",
"type": "text"
}
],
"finish_reason": ""
},
{
"role": "assistant",
"parts": [
{
"content": "response",
"content": "text response",
"type": "text"
}
],
Expand All @@ -287,13 +277,13 @@
"gen_ai.conversation.id": "PRESENT",
"gcp.vertex.agent.event_id": "PRESENT",
"gcp.vertex.agent.invocation_id": "PRESENT",
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.usage.input_tokens": 150,
"gen_ai.usage.output_tokens": 50,
"gen_ai.usage.cache_read.input_tokens": 60,
"gen_ai.usage.reasoning.output_tokens": 15,
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.tool.definitions": [
{
"name": "some_tool",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,7 @@
"role": "assistant",
"parts": [
{
"content": "text ",
"type": "text"
}
],
"finish_reason": ""
},
{
"role": "assistant",
"parts": [
{
"content": "response",
"content": "text response",
"type": "text"
}
],
Expand All @@ -254,13 +244,13 @@
"gcp.vertex.agent.event_id": "PRESENT",
"gcp.vertex.agent.invocation_id": "PRESENT",
"gen_ai.provider.name": "gcp.gemini",
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.usage.input_tokens": 150,
"gen_ai.usage.output_tokens": 50,
"gen_ai.usage.cache_read.input_tokens": 60,
"gen_ai.usage.reasoning.output_tokens": 15,
"gen_ai.response.finish_reasons": [
"stop"
],
"gen_ai.tool.definitions": [
{
"name": "some_tool",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,24 +246,7 @@
"content": {
"parts": [
{
"text": "text "
}
],
"role": "model"
},
"index": 0
},
"attributes": {
"gen_ai.system": "gemini"
}
},
{
"event_name": "gen_ai.choice",
"body": {
"content": {
"parts": [
{
"text": "response"
"text": "text response"
}
],
"role": "model"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,6 @@
"gen_ai.system": "gemini"
}
},
{
"event_name": "gen_ai.choice",
"body": {
"content": "<elided>",
"index": 0
},
"attributes": {
"gen_ai.system": "gemini"
}
},
{
"event_name": "gen_ai.choice",
"body": {
Expand Down
6 changes: 3 additions & 3 deletions tests/unittests/telemetry/functional_test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ def experimental_adk_matrix(
schema_version=2,
tool_exception=TOOL_ERROR,
),
# The same turn streamed, its answer arriving in two chunks that leave
# `partial` unset: one inference span per turn either way, and a completion
# log holding the whole answer rather than whichever chunk arrived first.
# The same turn streamed through Gemini's aggregator, its answer arriving in
# two chunks: one inference span per turn either way, and a completion log
# holding the aggregated answer rather than the chunks.
*semconv_matrix("streaming"),
# Compaction scenarios. Both triggers, because they place the
# `compact_events` span differently: token-threshold nests inside
Expand Down
Loading
Loading