forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracing.py
More file actions
357 lines (299 loc) · 12 KB
/
Copy pathtracing.py
File metadata and controls
357 lines (299 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# NOTE:
#
# We expect that the underlying GenAI SDK will provide a certain
# level of tracing and logging telemetry aligned with Open Telemetry
# Semantic Conventions (such as logging prompts, responses,
# request properties, etc.) and so the information that is recorded by the
# Agent Development Kit should be focused on the higher-level
# constructs of the framework that are not observable by the SDK.
from __future__ import annotations
import json
from typing import Any
from typing import TYPE_CHECKING
from google.genai import types
from opentelemetry import trace
from .. import version
from ..events.event import Event
# TODO: Replace with constant from opentelemetry.semconv when it reaches version 1.37 in g3.
GEN_AI_AGENT_DESCRIPTION = 'gen_ai.agent.description'
GEN_AI_AGENT_NAME = 'gen_ai.agent.name'
GEN_AI_CONVERSATION_ID = 'gen_ai.conversation.id'
GEN_AI_OPERATION_NAME = 'gen_ai.operation.name'
GEN_AI_TOOL_CALL_ID = 'gen_ai.tool.call.id'
GEN_AI_TOOL_DESCRIPTION = 'gen_ai.tool.description'
GEN_AI_TOOL_NAME = 'gen_ai.tool.name'
GEN_AI_TOOL_TYPE = 'gen_ai.tool.type'
# Needed to avoid circular imports
if TYPE_CHECKING:
from ..agents.base_agent import BaseAgent
from ..agents.invocation_context import InvocationContext
from ..models.llm_request import LlmRequest
from ..models.llm_response import LlmResponse
from ..tools.base_tool import BaseTool
tracer = trace.get_tracer(
instrumenting_module_name='gcp.vertex.agent',
instrumenting_library_version=version.__version__,
# TODO: Replace with constant from opentelemetry.semconv when it reaches version 1.37 in g3.
schema_url='https://opentelemetry.io/schemas/1.37.0',
)
def _safe_json_serialize(obj) -> str:
"""Convert any Python object to a JSON-serializable type or string.
Args:
obj: The object to serialize.
Returns:
The JSON-serialized object string or <non-serializable> if the object cannot be serialized.
"""
try:
# Try direct JSON serialization first
return json.dumps(
obj, ensure_ascii=False, default=lambda o: '<not serializable>'
)
except (TypeError, OverflowError):
return '<not serializable>'
def trace_agent_invocation(
span: trace.Span, agent: BaseAgent, ctx: InvocationContext
) -> None:
"""Sets span attributes immedietely available on agent invocation according to OTEL semconv version 1.37.
Args:
span: Span on which attributes are set.
agent: Agent from which attributes are gathered.
ctx: InvocationContext from which attrbiutes are gathered.
Inference related fields are not set, due to their planned removal from invoke_agent span:
https://github.com/open-telemetry/semantic-conventions/issues/2632
`gen_ai.agent.id` is not set because currently it's unclear what attributes this field should have, specifically:
- In which scope should it be unique (globally, given project, given agentic flow, given deployment).
- Should it be unchanging between deployments, and how this should this be achieved.
`gen_ai.data_source.id` is not set because it's not available.
Closest type which could contain this information is types.GroundingMetadata, which does not have an ID.
`server.*` attributes are not set pending confirmation from aabmass.
"""
# Required
span.set_attribute(GEN_AI_OPERATION_NAME, 'invoke_agent')
# Conditionally Required
span.set_attribute(GEN_AI_AGENT_DESCRIPTION, agent.description)
span.set_attribute(GEN_AI_AGENT_NAME, agent.name)
span.set_attribute(GEN_AI_CONVERSATION_ID, ctx.session.id)
def trace_tool_call(
tool: BaseTool,
args: dict[str, Any],
function_response_event: Event,
):
"""Traces tool call.
Args:
tool: The tool that was called.
args: The arguments to the tool call.
function_response_event: The event with the function response details.
"""
span = trace.get_current_span()
span.set_attribute(GEN_AI_OPERATION_NAME, 'execute_tool')
span.set_attribute(GEN_AI_TOOL_DESCRIPTION, tool.description)
span.set_attribute(GEN_AI_TOOL_NAME, tool.name)
# e.g. FunctionTool
span.set_attribute(GEN_AI_TOOL_TYPE, tool.__class__.__name__)
# Setting empty llm request and response (as UI expect these) while not
# applicable for tool_response.
span.set_attribute('gcp.vertex.agent.llm_request', '{}')
span.set_attribute('gcp.vertex.agent.llm_response', '{}')
span.set_attribute(
'gcp.vertex.agent.tool_call_args',
_safe_json_serialize(args),
)
# Tracing tool response
tool_call_id = '<not specified>'
tool_response = '<not specified>'
if (
function_response_event.content is not None
and function_response_event.content.parts
):
response_parts = function_response_event.content.parts
function_response = response_parts[0].function_response
if function_response is not None:
if function_response.id is not None:
tool_call_id = function_response.id
if function_response.response is not None:
tool_response = function_response.response
span.set_attribute(GEN_AI_TOOL_CALL_ID, tool_call_id)
if not isinstance(tool_response, dict):
tool_response = {'result': tool_response}
span.set_attribute('gcp.vertex.agent.event_id', function_response_event.id)
span.set_attribute(
'gcp.vertex.agent.tool_response',
_safe_json_serialize(tool_response),
)
def trace_merged_tool_calls(
response_event_id: str,
function_response_event: Event,
):
"""Traces merged tool call events.
Calling this function is not needed for telemetry purposes. This is provided
for preventing /debug/trace requests (typically sent by web UI).
Args:
response_event_id: The ID of the response event.
function_response_event: The merged response event.
"""
span = trace.get_current_span()
span.set_attribute(GEN_AI_OPERATION_NAME, 'execute_tool')
span.set_attribute(GEN_AI_TOOL_NAME, '(merged tools)')
span.set_attribute(GEN_AI_TOOL_DESCRIPTION, '(merged tools)')
span.set_attribute(GEN_AI_TOOL_CALL_ID, response_event_id)
# TODO(b/441461932): See if these are still necessary
span.set_attribute('gcp.vertex.agent.tool_call_args', 'N/A')
span.set_attribute('gcp.vertex.agent.event_id', response_event_id)
try:
function_response_event_json = function_response_event.model_dumps_json(
exclude_none=True
)
except Exception: # pylint: disable=broad-exception-caught
function_response_event_json = '<not serializable>'
span.set_attribute(
'gcp.vertex.agent.tool_response',
function_response_event_json,
)
# Setting empty llm request and response (as UI expect these) while not
# applicable for tool_response.
span.set_attribute('gcp.vertex.agent.llm_request', '{}')
span.set_attribute(
'gcp.vertex.agent.llm_response',
'{}',
)
def trace_call_llm(
invocation_context: InvocationContext,
event_id: str,
llm_request: LlmRequest,
llm_response: LlmResponse,
):
"""Traces a call to the LLM.
This function records details about the LLM request and response as
attributes on the current OpenTelemetry span.
Args:
invocation_context: The invocation context for the current agent run.
event_id: The ID of the event.
llm_request: The LLM request object.
llm_response: The LLM response object.
"""
span = trace.get_current_span()
# Special standard Open Telemetry GenaI attributes that indicate
# that this is a span related to a Generative AI system.
span.set_attribute('gen_ai.system', 'gcp.vertex.agent')
span.set_attribute('gen_ai.request.model', llm_request.model)
span.set_attribute(
'gcp.vertex.agent.invocation_id', invocation_context.invocation_id
)
span.set_attribute(
'gcp.vertex.agent.session_id', invocation_context.session.id
)
span.set_attribute('gcp.vertex.agent.event_id', event_id)
# Consider removing once GenAI SDK provides a way to record this info.
span.set_attribute(
'gcp.vertex.agent.llm_request',
_safe_json_serialize(_build_llm_request_for_trace(llm_request)),
)
# Consider removing once GenAI SDK provides a way to record this info.
if llm_request.config:
if llm_request.config.top_p:
span.set_attribute(
'gen_ai.request.top_p',
llm_request.config.top_p,
)
if llm_request.config.max_output_tokens:
span.set_attribute(
'gen_ai.request.max_tokens',
llm_request.config.max_output_tokens,
)
try:
llm_response_json = llm_response.model_dump_json(exclude_none=True)
except Exception: # pylint: disable=broad-exception-caught
llm_response_json = '<not serializable>'
span.set_attribute(
'gcp.vertex.agent.llm_response',
llm_response_json,
)
if llm_response.usage_metadata is not None:
span.set_attribute(
'gen_ai.usage.input_tokens',
llm_response.usage_metadata.prompt_token_count,
)
if llm_response.usage_metadata.candidates_token_count is not None:
span.set_attribute(
'gen_ai.usage.output_tokens',
llm_response.usage_metadata.candidates_token_count,
)
if llm_response.finish_reason:
if isinstance(llm_response.finish_reason, types.FinishReason):
finish_reason_str = llm_response.finish_reason.name.lower()
else:
# Fallback for string values (should not occur with LiteLLM after enum mapping)
finish_reason_str = str(llm_response.finish_reason).lower()
span.set_attribute(
'gen_ai.response.finish_reasons',
[finish_reason_str],
)
def trace_send_data(
invocation_context: InvocationContext,
event_id: str,
data: list[types.Content],
):
"""Traces the sending of data to the agent.
This function records details about the data sent to the agent as
attributes on the current OpenTelemetry span.
Args:
invocation_context: The invocation context for the current agent run.
event_id: The ID of the event.
data: A list of content objects.
"""
span = trace.get_current_span()
span.set_attribute(
'gcp.vertex.agent.invocation_id', invocation_context.invocation_id
)
span.set_attribute('gcp.vertex.agent.event_id', event_id)
# Once instrumentation is added to the GenAI SDK, consider whether this
# information still needs to be recorded by the Agent Development Kit.
span.set_attribute(
'gcp.vertex.agent.data',
_safe_json_serialize([
types.Content(role=content.role, parts=content.parts).model_dump(
exclude_none=True
)
for content in data
]),
)
def _build_llm_request_for_trace(llm_request: LlmRequest) -> dict[str, Any]:
"""Builds a dictionary representation of the LLM request for tracing.
This function prepares a dictionary representation of the LlmRequest
object, suitable for inclusion in a trace. It excludes fields that cannot
be serialized (e.g., function pointers) and avoids sending bytes data.
Args:
llm_request: The LlmRequest object.
Returns:
A dictionary representation of the LLM request.
"""
# Some fields in LlmRequest are function pointers and can not be serialized.
result = {
'model': llm_request.model,
'config': llm_request.config.model_dump(
exclude_none=True, exclude='response_schema'
),
'contents': [],
}
# We do not want to send bytes data to the trace.
for content in llm_request.contents:
parts = [part for part in content.parts if not part.inline_data]
result['contents'].append(
types.Content(role=content.role, parts=parts).model_dump(
exclude_none=True
)
)
return result