Skip to content

Commit f1eed38

Browse files
committed
feat: agent run span
1 parent 5fb7aae commit f1eed38

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

veadk/tracing/telemetry/exporters/inmemory_exporter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(self, exporter: _InMemoryExporter) -> None:
7777
super().__init__(exporter)
7878

7979
def on_start(self, span, parent_context) -> None:
80-
if span.name.startswith("invocation") or span.name.startswith("invoke") :
80+
if span.name.startswith("invocation"):
8181
span.set_attribute("gen_ai.operation.name", "chain")
8282
span.set_attribute("gen_ai.span.kind", "workflow")
8383
span.set_attribute("gen_ai.usage.total_tokens", 0)
@@ -88,7 +88,7 @@ def on_start(self, span, parent_context) -> None:
8888
token = attach(ctx) # mount context on `invocation` root span in Google ADK
8989
setattr(span, "_invocation_token", token) # for later detach
9090

91-
if span.name.startswith("agent_run"):
91+
if span.name.startswith("agent_run") or span.name.startswith("invoke_agent"):
9292
span.set_attribute("gen_ai.operation.name", "agent")
9393
span.set_attribute("gen_ai.span.kind", "agent")
9494

veadk/tracing/telemetry/telemetry.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ def _set_agent_input_attribute(
9393
user_content = invocation_context.user_content
9494
if user_content and user_content.parts:
9595
# set gen_ai.input attribute required by APMPlus
96-
# TODO: 优化 gen_ai.input,目前无法序列化
97-
span.set_attribute("gen_ai.input", safe_json_serialize(user_content.parts))
96+
span.set_attribute("gen_ai.input", user_content.model_dump(exclude_none=True))
9897

9998
span.add_event(
10099
"gen_ai.user.message",
@@ -132,15 +131,9 @@ def _set_agent_input_attribute(
132131

133132
def _set_agent_output_attribute(span: Span, llm_response: LlmResponse) -> None:
134133
content = llm_response.content
135-
print(f"### response: {llm_response}")
136-
print(f"### output_transcription: {llm_response.output_transcription}")
137-
print(f"### input_transcription: {llm_response.input_transcription}")
138-
139-
140134
if content and content.parts:
141135
# set gen_ai.output attribute required by APMPlus
142-
span.set_attribute("gen_ai.output", safe_json_serialize(content.parts))
143-
print("### parts: ", content.parts)
136+
span.set_attribute("gen_ai.output", content.model_dump(exclude_none=True))
144137
for idx, part in enumerate(content.parts):
145138
if part.text:
146139
span.add_event(
@@ -163,7 +156,7 @@ def set_common_attributes_on_model_span(
163156
invocation_span: Span = get_value("invocation_span_instance") # type: ignore
164157
agent_run_span: Span = get_value("agent_run_span_instance") # type: ignore
165158

166-
if invocation_span and (invocation_span.name.startswith("invocation") or invocation_span.name.startswith("invoke")):
159+
if invocation_span and invocation_span.name.startswith("invocation"):
167160
_set_agent_input_attribute(invocation_span, invocation_context)
168161
_set_agent_output_attribute(invocation_span, llm_response)
169162
for attr_name, attr_extractor in common_attributes.items():
@@ -190,7 +183,7 @@ def set_common_attributes_on_model_span(
190183
"gen_ai.usage.total_tokens", accumulated_total_token_usage
191184
)
192185

193-
if agent_run_span and agent_run_span.name.startswith("agent_run"):
186+
if agent_run_span and ( agent_run_span.name.startswith("agent_run") or agent_run_span.name.startswith("invoke_agent") ):
194187
_set_agent_input_attribute(agent_run_span, invocation_context)
195188
_set_agent_output_attribute(agent_run_span, llm_response)
196189
for attr_name, attr_extractor in common_attributes.items():

0 commit comments

Comments
 (0)