Skip to content

Commit d5e8bc7

Browse files
google-genai-botcopybara-github
authored andcommitted
feat(telemetry): add tool type attribute to tool execution duration
Add `gen_ai.tool.type` attribute to tool execution duration metric, as suggested in the OTel semconv update PR: open-telemetry/semantic-conventions-genai#201. PiperOrigin-RevId: 938566496
1 parent abf9258 commit d5e8bc7

4 files changed

Lines changed: 10 additions & 0 deletions

File tree

src/google/adk/telemetry/_instrumentation.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ async def record_tool_execution(
175175
try:
176176
_metrics.record_tool_execution_duration(
177177
tool_name=tool.name,
178+
tool_type=tool.__class__.__name__,
178179
agent_name=agent.name,
179180
elapsed_s=_get_elapsed_s(span, start_time),
180181
error=caught_error,

src/google/adk/telemetry/_metrics.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ def record_agent_workflow_steps(agent_name: str, events: list[Event]):
191191

192192
def record_tool_execution_duration(
193193
tool_name: str,
194+
tool_type: str,
194195
agent_name: str,
195196
elapsed_s: float,
196197
error: Exception | None = None,
@@ -199,6 +200,7 @@ def record_tool_execution_duration(
199200
attrs = {
200201
gen_ai_attributes.GEN_AI_AGENT_NAME: agent_name,
201202
gen_ai_attributes.GEN_AI_TOOL_NAME: tool_name,
203+
gen_ai_attributes.GEN_AI_TOOL_TYPE: tool_type,
202204
}
203205
if error is not None:
204206
attrs[error_attributes.ERROR_TYPE] = type(error).__name__

tests/unittests/telemetry/test_functional.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,15 @@ async def generate_random_number():
299299
attributes={
300300
"gen_ai.agent.name": "complex_agent",
301301
"gen_ai.tool.name": "generate_random_number",
302+
"gen_ai.tool.type": "FunctionTool",
302303
},
303304
value=None,
304305
),
305306
MetricPoint(
306307
attributes={
307308
"gen_ai.agent.name": "complex_agent",
308309
"gen_ai.tool.name": "get_current_time",
310+
"gen_ai.tool.type": "FunctionTool",
309311
},
310312
value=None,
311313
),
@@ -423,6 +425,7 @@ async def failing_tool():
423425
attributes={
424426
"gen_ai.agent.name": "error_agent",
425427
"gen_ai.tool.name": "failing_tool",
428+
"gen_ai.tool.type": "FunctionTool",
426429
"error.type": "ValueError",
427430
},
428431
value=None,
@@ -431,6 +434,7 @@ async def failing_tool():
431434
attributes={
432435
"gen_ai.agent.name": "error_agent",
433436
"gen_ai.tool.name": "get_current_time",
437+
"gen_ai.tool.type": "FunctionTool",
434438
},
435439
value=None,
436440
),

tests/unittests/telemetry/test_metrics.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def test_record_tool_execution_duration(mock_meter_setup):
169169
"""Tests record_tool_execution_duration records correctly."""
170170
_metrics.record_tool_execution_duration(
171171
"test_tool",
172+
"test_tool_type",
172173
"test_agent",
173174
0.5,
174175
)
@@ -179,6 +180,7 @@ def test_record_tool_execution_duration(mock_meter_setup):
179180
want_attributes = {
180181
"gen_ai.agent.name": "test_agent",
181182
"gen_ai.tool.name": "test_tool",
183+
"gen_ai.tool.type": "test_tool_type",
182184
}
183185
assert kwargs["attributes"] == want_attributes
184186

@@ -188,6 +190,7 @@ def test_record_tool_execution_duration_with_error(mock_meter_setup):
188190
test_error = ValueError("tool failed")
189191
_metrics.record_tool_execution_duration(
190192
"test_tool",
193+
"test_tool_type",
191194
"test_agent",
192195
0.5,
193196
error=test_error,

0 commit comments

Comments
 (0)