@@ -69,17 +69,12 @@ def record_agent_request_size(
6969 agent_name : str , user_content : types .Content | None
7070):
7171 """Records the size of the agent request."""
72- try :
73- size = _get_content_size (user_content )
74- attrs = {
75- gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
76- GEN_AI_INPUT_TYPE : _get_modality_from_content (user_content ),
77- }
78- _agent_request_size .record (size , attributes = attrs )
79- except Exception : # pylint: disable=broad-exception-caught
80- logger .exception (
81- "Failed to record agent request size for agent %s" , agent_name
82- )
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 )
8378
8479
8580def record_agent_invocation_duration (
@@ -90,64 +85,49 @@ def record_agent_invocation_duration(
9085 error : Exception | None = None ,
9186):
9287 """Records the duration of the agent invocation."""
93- try :
94- response_content : types .Content | None = None
95- for event in reversed (events ):
96- if event .author == agent_name and event .content :
97- response_content = event .content
98- break
99-
100- attrs = {
101- gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
102- GEN_AI_INPUT_TYPE : _get_modality_from_content (user_content ),
103- gen_ai_attributes .GEN_AI_OUTPUT_TYPE : _get_modality_from_content (
104- response_content
105- ),
106- }
107- if error is not None :
108- attrs [error_attributes .ERROR_TYPE ] = type (error ).__name__
109- _agent_invocation_duration .record (elapsed_ms , attributes = attrs )
110- except Exception : # pylint: disable=broad-exception-caught
111- logger .exception (
112- "Failed to record agent invocation duration for agent %s" , agent_name
113- )
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+ }
101+ if error is not None :
102+ attrs [error_attributes .ERROR_TYPE ] = type (error ).__name__
103+ _agent_invocation_duration .record (elapsed_ms , attributes = attrs )
114104
115105
116106def record_agent_response_size (agent_name : str , events : list [Event ]):
117107 """Records the size of the agent response by extracting content from events."""
118- try :
119- response_content : types .Content | None = None
120- for event in reversed (events ):
121- # Need to look for author matching agent_name and having content
122- if event .author == agent_name and event .content :
123- response_content = event .content
124- break
125-
126- size = _get_content_size (response_content )
127- attrs = {
128- gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
129- gen_ai_attributes .GEN_AI_OUTPUT_TYPE : _get_modality_from_content (
130- response_content
131- ),
132- }
133- _agent_response_size .record (size , attributes = attrs )
134- except Exception : # pylint: disable=broad-exception-caught
135- logger .exception (
136- "Failed to record agent response size for agent %s" , agent_name
137- )
108+ response_content : types .Content | None = None
109+ for event in reversed (events ):
110+ # Need to look for author matching agent_name and having content
111+ if event .author == agent_name and event .content :
112+ response_content = event .content
113+ break
114+
115+ 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+ }
122+ _agent_response_size .record (size , attributes = attrs )
138123
139124
140125def record_agent_workflow_steps (agent_name : str , steps_count : int ):
141126 """Records the number of steps in the agent workflow."""
142- try :
143- attrs = {
144- gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
145- }
146- _agent_workflow_steps .record (steps_count , attributes = attrs )
147- except Exception : # pylint: disable=broad-exception-caught
148- logger .exception (
149- "Failed to record agent workflow steps for agent %s" , agent_name
150- )
127+ attrs = {
128+ gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
129+ }
130+ _agent_workflow_steps .record (steps_count , attributes = attrs )
151131
152132
153133def record_tool_execution_duration (
@@ -159,23 +139,18 @@ def record_tool_execution_duration(
159139 error : Exception | None = None ,
160140):
161141 """Records the duration of the tool execution."""
162- try :
163- attrs = {
164- gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
165- gen_ai_attributes .GEN_AI_TOOL_NAME : tool_name ,
166- GEN_AI_INPUT_TYPE : _get_modality_from_content (input_content ),
167- }
168- if error is not None :
169- attrs [error_attributes .ERROR_TYPE ] = type (error ).__name__
170- else :
171- attrs [gen_ai_attributes .GEN_AI_OUTPUT_TYPE ] = _get_modality_from_content (
172- output_content
173- )
174- _tool_execution_duration .record (elapsed_ms , attributes = attrs )
175- except Exception : # pylint: disable=broad-exception-caught
176- logger .exception (
177- "Failed to record tool execution duration for tool %s" , tool_name
142+ attrs = {
143+ gen_ai_attributes .GEN_AI_AGENT_NAME : agent_name ,
144+ gen_ai_attributes .GEN_AI_TOOL_NAME : tool_name ,
145+ GEN_AI_INPUT_TYPE : _get_modality_from_content (input_content ),
146+ }
147+ if error is not None :
148+ 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
178152 )
153+ _tool_execution_duration .record (elapsed_ms , attributes = attrs )
179154
180155
181156# Helper functions copied from metrics_plugin.py
0 commit comments