(monkeypatch)
| 382 | |
| 383 | @pytest.mark.asyncio |
| 384 | async def test_metrics_tool_error(monkeypatch): |
| 385 | reader = _setup_test_metrics(monkeypatch) |
| 386 | |
| 387 | async def get_current_time(): |
| 388 | return "2026-04-15T14:26:03Z" |
| 389 | |
| 390 | async def failing_tool(): |
| 391 | raise ValueError("Tool failed") |
| 392 | |
| 393 | mock_model = MockModel.create( |
| 394 | responses=[ |
| 395 | Part.from_function_call(name="get_current_time", args={}), |
| 396 | Part.from_function_call(name="failing_tool", args={}), |
| 397 | Part.from_text(text="Should not reach here"), |
| 398 | ] |
| 399 | ) |
| 400 | test_agent = Agent( |
| 401 | name="error_agent", |
| 402 | model=mock_model, |
| 403 | tools=[FunctionTool(get_current_time), FunctionTool(failing_tool)], |
| 404 | ) |
| 405 | |
| 406 | runner = InMemoryRunner(root_agent=test_agent) |
| 407 | with pytest.raises(ValueError, match="Tool failed"): |
| 408 | await runner.run_async("Run tools") |
| 409 | |
| 410 | metrics_data = reader.get_metrics_data() |
| 411 | metrics_list = metrics_data.resource_metrics[0].scope_metrics[0].metrics |
| 412 | |
| 413 | # Verify Tool Execution Duration |
| 414 | got = _extract_metrics( |
| 415 | metrics_list, "gen_ai.tool.execution.duration", "error_agent" |
| 416 | ) |
| 417 | assert len(got) == 2 |
| 418 | for p in got: |
| 419 | p.value = None |
| 420 | |
| 421 | want = [ |
| 422 | MetricPoint( |
| 423 | attributes={ |
| 424 | "gen_ai.agent.name": "error_agent", |
| 425 | "gen_ai.tool.name": "failing_tool", |
| 426 | "error.type": "ValueError", |
| 427 | }, |
| 428 | value=None, |
| 429 | ), |
| 430 | MetricPoint( |
| 431 | attributes={ |
| 432 | "gen_ai.agent.name": "error_agent", |
| 433 | "gen_ai.tool.name": "get_current_time", |
| 434 | }, |
| 435 | value=None, |
| 436 | ), |
| 437 | ] |
| 438 | |
| 439 | got.sort(key=lambda p: str(p.attributes.get("gen_ai.tool.name", ""))) |
| 440 | want.sort(key=lambda p: str(p.attributes.get("gen_ai.tool.name", ""))) |
| 441 | assert got == want |
nothing calls this directly
no test coverage detected