Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/google/adk/telemetry/_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ def record_llm_response(
def _record_agent_metrics(
agent_name: str,
elapsed_s: float,
events: object,
caught_error: Exception | None,
) -> None:
try:
Expand All @@ -134,7 +133,6 @@ def _record_agent_metrics(
elapsed_s,
caught_error,
)
_metrics.record_agent_workflow_steps(agent_name, events)
except Exception: # pylint: disable=broad-exception-caught
logger.exception("Failed to record agent metrics for agent %s", agent_name)

Expand Down Expand Up @@ -196,7 +194,6 @@ async def record_agent_invocation(
_record_agent_metrics(
agent.name,
_metrics.get_elapsed_s(span, start_time),
getattr(getattr(ctx, "session", None), "events", []),
caught_error,
)
_flush_invoke_agent_metrics(tel_ctx, agent.name)
Expand Down
26 changes: 0 additions & 26 deletions src/google/adk/telemetry/_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from opentelemetry.semconv.attributes import error_attributes

if TYPE_CHECKING:
from google.adk.events.event import Event
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from opentelemetry.trace import Span
Expand Down Expand Up @@ -91,24 +90,6 @@
81.92,
],
)
_agent_workflow_steps = meter.create_histogram(
"gen_ai.agent.workflow.steps",
unit="1",
description="Length of agentic workflow (# of events).",
explicit_bucket_boundaries_advisory=[
1,
2,
4,
8,
16,
32,
64,
128,
256,
512,
1024,
],
)
_client_operation_duration = (
gen_ai_metrics.create_gen_ai_client_operation_duration(meter)
)
Expand Down Expand Up @@ -198,13 +179,6 @@ def record_invoke_agent_tool_calls(agent_name: str, count: int) -> None:
_invoke_agent_tool_calls.record(count, attributes=attrs)


def record_agent_workflow_steps(agent_name: str, events: list[Event]):
"""Records the number of steps in the agent workflow by counting the number of events."""
attrs = {gen_ai_attributes.GEN_AI_AGENT_NAME: agent_name}
count = sum(1 for event in events if event.author == agent_name)
_agent_workflow_steps.record(count, attributes=attrs)


def record_tool_execution_duration(
tool_name: str,
tool_type: str,
Expand Down
28 changes: 19 additions & 9 deletions src/google/adk/telemetry/node_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,6 @@ def _use_invoke_workflow_span(
# The flag rides along the otel_context propagated to child nodes, so nested
# workflows see it set.
nested = bool(context_api.get_value(_ENTRYPOINT_WORKFLOW_KEY, otel_context))
if not nested:
otel_context = context_api.set_value(
_ENTRYPOINT_WORKFLOW_KEY, True, otel_context
)
attributes: dict[str, AttributeValue] = {
GEN_AI_OPERATION_NAME: "invoke_workflow",
GEN_AI_CONVERSATION_ID: conversation_id,
Expand All @@ -207,11 +203,14 @@ def _use_invoke_workflow_span(
start_s = time.monotonic()
workflow_span: Span | None = None
try:
with tracer.start_as_current_span(
name=span_name,
attributes=attributes,
context=otel_context,
) as span:
with (
tracer.start_as_current_span(
name=span_name,
attributes=attributes,
context=otel_context,
) as span,
_mark_nested_workflows(),
):
workflow_span = span
yield span
finally:
Expand All @@ -221,3 +220,14 @@ def _use_invoke_workflow_span(
nested=nested,
error=sys.exc_info()[1],
)


@contextmanager
def _mark_nested_workflows() -> Iterator[None]:
token = context_api.attach(
context_api.set_value(_ENTRYPOINT_WORKFLOW_KEY, True)
)
try:
yield
finally:
context_api.detach(token)
Loading
Loading