2020from google .adk .models .llm_response import LlmResponse
2121from google .adk .tools import BaseTool
2222from opentelemetry import trace
23- from opentelemetry .context import get_value
23+ from opentelemetry .context import get_value , set_value
2424from opentelemetry .sdk .trace import Span , _Span
2525
26+ from veadk .utils .misc import safe_json_serialize
2627from veadk .tracing .telemetry .attributes .attributes import ATTRIBUTES
2728from veadk .tracing .telemetry .attributes .extractors .types import (
2829 ExtractorResponse ,
@@ -91,6 +92,10 @@ def _set_agent_input_attribute(
9192
9293 user_content = invocation_context .user_content
9394 if user_content and user_content .parts :
95+ # 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 ))
98+
9499 span .add_event (
95100 "gen_ai.user.message" ,
96101 {
@@ -127,7 +132,15 @@ def _set_agent_input_attribute(
127132
128133def _set_agent_output_attribute (span : Span , llm_response : LlmResponse ) -> None :
129134 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+
130140 if content and content .parts :
141+ # 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 )
131144 for idx , part in enumerate (content .parts ):
132145 if part .text :
133146 span .add_event (
@@ -150,7 +163,7 @@ def set_common_attributes_on_model_span(
150163 invocation_span : Span = get_value ("invocation_span_instance" ) # type: ignore
151164 agent_run_span : Span = get_value ("agent_run_span_instance" ) # type: ignore
152165
153- if invocation_span and invocation_span .name .startswith ("invocation" ):
166+ if invocation_span and ( invocation_span .name .startswith ("invocation" ) or invocation_span . name . startswith ( "invoke" ) ):
154167 _set_agent_input_attribute (invocation_span , invocation_context )
155168 _set_agent_output_attribute (invocation_span , llm_response )
156169 for attr_name , attr_extractor in common_attributes .items ():
@@ -173,6 +186,7 @@ def set_common_attributes_on_model_span(
173186 current_step_token_usage + int (prev_total_token_usage ) # type: ignore
174187 ) # we can ignore this warning, cause we manually set the attribute to int before
175188 invocation_span .set_attribute (
189+ # record input/output token usage?
176190 "gen_ai.usage.total_tokens" , accumulated_total_token_usage
177191 )
178192
0 commit comments