2727
2828# TODO(b/477553411): add these attributes to Otel semconv.
2929GEN_AI_AGENT_VERSION = "gen_ai.agent.version"
30- GEN_AI_INPUT_TYPE = "gen_ai.input.type"
3130GEN_AI_TOOL_VERSION = "gen_ai.tool.version"
3231
3332# Initialize meter
6564)
6665
6766
68- def record_agent_request_size (
69- agent_name : str , user_content : types .Content | None
70- ):
71- """Records the size of the agent request."""
72- size = _get_content_size (user_content )
73- attrs = {
74- gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
75- GEN_AI_INPUT_TYPE : _get_modality_from_content (user_content ),
76- }
77- _agent_request_size .record (size , attributes = attrs )
78-
79-
8067def record_agent_invocation_duration (
8168 agent_name : str ,
8269 elapsed_ms : float ,
83- user_content : types .Content | None ,
84- events : list [Event ],
8570 error : Exception | None = None ,
8671):
8772 """Records the duration of the agent invocation."""
88- response_content : types .Content | None = None
89- for event in reversed (events ):
90- if event .author == agent_name and event .content :
91- response_content = event .content
92- break
93-
94- attrs = {
95- gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
96- GEN_AI_INPUT_TYPE : _get_modality_from_content (user_content ),
97- gen_ai_attributes .GEN_AI_OUTPUT_TYPE : _get_modality_from_content (
98- response_content
99- ),
100- }
73+ attrs = {gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name }
10174 if error is not None :
10275 attrs [error_attributes .ERROR_TYPE ] = type (error ).__name__
10376 _agent_invocation_duration .record (elapsed_ms , attributes = attrs )
10477
10578
79+ def record_agent_request_size (
80+ agent_name : str , user_content : types .Content | None
81+ ):
82+ """Records the size of the agent request."""
83+ size = _get_content_size (user_content )
84+ attrs = {gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name }
85+ _agent_request_size .record (size , attributes = attrs )
86+
87+
10688def record_agent_response_size (agent_name : str , events : list [Event ]):
10789 """Records the size of the agent response by extracting content from events."""
10890 response_content : types .Content | None = None
10991 for event in reversed (events ):
110- # Need to look for author matching agent_name and having content
11192 if event .author == agent_name and event .content :
11293 response_content = event .content
11394 break
11495
11596 size = _get_content_size (response_content )
116- attrs = {
117- gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
118- gen_ai_attributes .GEN_AI_OUTPUT_TYPE : _get_modality_from_content (
119- response_content
120- ),
121- }
97+ attrs = {gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name }
12298 _agent_response_size .record (size , attributes = attrs )
12399
124100
@@ -134,52 +110,18 @@ def record_tool_execution_duration(
134110 tool_name : str ,
135111 agent_name : str ,
136112 elapsed_ms : float ,
137- input_content : types .Content | None ,
138- output_content : types .Content | None ,
139113 error : Exception | None = None ,
140114):
141115 """Records the duration of the tool execution."""
142116 attrs = {
143117 gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
144118 gen_ai_attributes .GEN_AI_TOOL_NAME : tool_name ,
145- GEN_AI_INPUT_TYPE : _get_modality_from_content (input_content ),
146119 }
147120 if error is not None :
148121 attrs [error_attributes .ERROR_TYPE ] = type (error ).__name__
149- else :
150- attrs [gen_ai_attributes .GEN_AI_OUTPUT_TYPE ] = _get_modality_from_content (
151- output_content
152- )
153122 _tool_execution_duration .record (elapsed_ms , attributes = attrs )
154123
155124
156- # Helper functions copied from metrics_plugin.py
157-
158-
159- def _get_modality_from_content (
160- content : types .Content | None ,
161- ) -> str :
162- if content is None or not content .parts :
163- return "unknown"
164- modalities = set ()
165- for part in content .parts :
166- if part .text is not None :
167- modalities .add ("text" )
168- inline_data = part .inline_data
169- if inline_data and inline_data .mime_type :
170- mime = inline_data .mime_type
171- if "/" in mime :
172- modalities .add (mime .split ("/" )[0 ])
173- file_data = part .file_data
174- if file_data and file_data .mime_type :
175- mime = file_data .mime_type
176- if "/" in mime :
177- modalities .add (mime .split ("/" )[0 ])
178- if not modalities :
179- return "text"
180- return "," .join (sorted (modalities ))
181-
182-
183125def _get_content_size (
184126 content : types .Content | None ,
185127) -> int :
0 commit comments