MCPcopy Index your code
hub / github.com/google/adk-python / test_metrics

Function test_metrics

tests/unittests/telemetry/test_functional.py:236–380  ·  view source on GitHub ↗
(monkeypatch)

Source from the content-addressed store, hash-verified

234
235@pytest.mark.asyncio
236async def test_metrics(monkeypatch):
237 reader = _setup_test_metrics(monkeypatch)
238
239 async def get_current_time():
240 return "2026-04-15T14:26:03Z"
241
242 async def generate_random_number():
243 return 42
244
245 mock_model = MockModel.create(
246 responses=[
247 Part.from_function_call(name="get_current_time", args={}),
248 Part.from_function_call(name="generate_random_number", args={}),
249 Part.from_text(text="Both tools executed."),
250 ],
251 usage_metadata=types.GenerateContentResponseUsageMetadata(
252 prompt_token_count=10,
253 candidates_token_count=20,
254 tool_use_prompt_token_count=5,
255 thoughts_token_count=10,
256 total_token_count=45,
257 ),
258 )
259 test_agent = Agent(
260 name="complex_agent",
261 model=mock_model,
262 tools=[
263 FunctionTool(get_current_time),
264 FunctionTool(generate_random_number),
265 ],
266 )
267
268 runner = InMemoryRunner(root_agent=test_agent)
269 await runner.run_async("Run both tools")
270
271 metrics_data = reader.get_metrics_data()
272 assert len(metrics_data.resource_metrics) > 0
273 scope_metrics = metrics_data.resource_metrics[0].scope_metrics
274 assert len(scope_metrics) > 0
275 metrics_list = scope_metrics[0].metrics
276 got_invocation = _extract_metrics(
277 metrics_list, "gen_ai.agent.invocation.duration", "complex_agent"
278 )
279 assert len(got_invocation) == 1
280 for p in got_invocation:
281 p.value = None
282 want_invocation = [
283 MetricPoint(
284 attributes={
285 "gen_ai.agent.name": "complex_agent",
286 },
287 value=None,
288 )
289 ]
290 assert got_invocation == want_invocation
291 got_tool_exec = _extract_metrics(
292 metrics_list, "gen_ai.tool.execution.duration", "complex_agent"
293 )

Callers

nothing calls this directly

Calls 9

run_asyncMethod · 0.95
FunctionToolClass · 0.90
_setup_test_metricsFunction · 0.85
_extract_metricsFunction · 0.85
MetricPointClass · 0.85
sortMethod · 0.80
InMemoryRunnerClass · 0.50
createMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected