Skip to content

Commit 34fbedf

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: add native OpenTelemetry agentic metrics
New metrics added: - gen_ai.agent.invocation.duration - gen_ai.tool.execution.duration - gen_ai.agent.request.size - gen_ai.agent.response.size - gen_ai.agent.workflow.steps New metrics and attributes are in line with OTel semantic conventions where possible, and will be formally to the semconv later. PiperOrigin-RevId: 901223911
1 parent 58432a2 commit 34fbedf

File tree

8 files changed

+69
-943
lines changed

8 files changed

+69
-943
lines changed

src/google/adk/agents/base_agent.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
from ..events.event_actions import EventActions
4343
from ..features import experimental
4444
from ..features import FeatureName
45-
from ..telemetry import _instrumentation
45+
from ..telemetry import tracing
46+
from ..telemetry.tracing import tracer
4647
from ..utils.context_utils import Aclosing
4748
from .base_agent_config import BaseAgentConfig
4849
from .callback_context import CallbackContext
@@ -284,8 +285,9 @@ async def run_async(
284285
Event: the events generated by the agent.
285286
"""
286287

287-
ctx = self._create_invocation_context(parent_context)
288-
async with _instrumentation.record_agent_invocation(ctx, self):
288+
with tracer.start_as_current_span(f'invoke_agent {self.name}') as span:
289+
ctx = self._create_invocation_context(parent_context)
290+
tracing.trace_agent_invocation(span, self, ctx)
289291
if event := await self._handle_before_agent_callback(ctx):
290292
yield event
291293
if ctx.end_invocation:
@@ -316,8 +318,9 @@ async def run_live(
316318
Event: the events generated by the agent.
317319
"""
318320

319-
ctx = self._create_invocation_context(parent_context)
320-
async with _instrumentation.record_agent_invocation(ctx, self):
321+
with tracer.start_as_current_span(f'invoke_agent {self.name}') as span:
322+
ctx = self._create_invocation_context(parent_context)
323+
tracing.trace_agent_invocation(span, self, ctx)
321324
if event := await self._handle_before_agent_callback(ctx):
322325
yield event
323326
if ctx.end_invocation:

src/google/adk/flows/llm_flows/functions.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from concurrent.futures import ThreadPoolExecutor
2323
import contextvars
2424
import copy
25+
import functools
2526
import inspect
2627
import logging
2728
import threading
@@ -42,8 +43,8 @@
4243
from ...auth.auth_tool import AuthToolArguments
4344
from ...events.event import Event
4445
from ...events.event_actions import EventActions
45-
from ...telemetry import _instrumentation
4646
from ...telemetry.tracing import trace_merged_tool_calls
47+
from ...telemetry.tracing import trace_tool_call
4748
from ...telemetry.tracing import tracer
4849
from ...tools.base_tool import BaseTool
4950
from ...tools.tool_confirmation import ToolConfirmation
@@ -590,11 +591,22 @@ async def _run_with_trace():
590591
)
591592
return function_response_event
592593

593-
async with _instrumentation.record_tool_execution(
594-
tool, agent, invocation_context, function_args
595-
) as tel_ctx:
596-
tel_ctx.function_response_event = await _run_with_trace()
597-
return tel_ctx.function_response_event
594+
with tracer.start_as_current_span(f'execute_tool {tool.name}'):
595+
function_response_event = None
596+
caught_error = None
597+
try:
598+
function_response_event = await _run_with_trace()
599+
return function_response_event
600+
except Exception as e:
601+
caught_error = e
602+
raise
603+
finally:
604+
trace_tool_call(
605+
tool=tool,
606+
args=function_args,
607+
function_response_event=function_response_event,
608+
error=caught_error,
609+
)
598610

599611

600612
async def handle_function_calls_live(
@@ -818,11 +830,22 @@ async def _run_with_trace():
818830
)
819831
return function_response_event
820832

821-
async with _instrumentation.record_tool_execution(
822-
tool, agent, invocation_context, function_args
823-
) as tel_ctx:
824-
tel_ctx.function_response_event = await _run_with_trace()
825-
return tel_ctx.function_response_event
833+
with tracer.start_as_current_span(f'execute_tool {tool.name}'):
834+
function_response_event = None
835+
caught_error = None
836+
try:
837+
function_response_event = await _run_with_trace()
838+
return function_response_event
839+
except Exception as e:
840+
caught_error = e
841+
raise
842+
finally:
843+
trace_tool_call(
844+
tool=tool,
845+
args=function_args,
846+
function_response_event=function_response_event,
847+
error=caught_error,
848+
)
826849

827850

828851
async def _process_function_live_helper(

src/google/adk/telemetry/_instrumentation.py

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)