Skip to content

feat: refactor OpenTelemetry (OTel) instrumentation within the ADK core, moving from manual span management to structured helper classes#1223

Open
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_918116236
Open

feat: refactor OpenTelemetry (OTel) instrumentation within the ADK core, moving from manual span management to structured helper classes#1223
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_918116236

Conversation

@copybara-service
Copy link
Copy Markdown

@copybara-service copybara-service Bot commented May 26, 2026

feat: refactor OpenTelemetry (OTel) instrumentation within the ADK core, moving from manual span management to structured helper classes

Key Changes

  • Structured Instrumentation: Replaces manual Tracing.trace calls and explicit Scope management with Flowable.using and Maybe.using patterns. It introduces helper classes like AgentInvocation and ToolExecution to encapsulate telemetry logic.
  • Metrics Integration: Adds support for tracking new metrics during agent execution:
    • gen_ai.agent.invocation.duration
    • gen_ai.agent.request.size / gen_ai.agent.response.size
    • gen_ai.agent.workflow.steps
  • Reactive API Improvements: Leverages Tracing.withContext() and doOnNext/doOnError hooks within AgentInvocation and ToolExecution to automatically capture events and errors without polluting the core logic.
  • Trace Hierarchy Refinement: Updates how spans are nested. For example, in ContextPropagationTest, the child agent span is now correctly parented to the specific LLM call span that triggered it, rather than the parent agent span.
  • Testing: Significantly enhances BaseAgentTest to verify metric collection using InMemoryMetricReader. Updates various tests to match the new span naming convention (e.g., removing brackets from tool execution spans).

Impact

This refactor simplifies the core agent and flow logic by removing boilerplate telemetry code, making the instrumentation more robust and easier to maintain while expanding the observability of the ADK through new metrics.

@copybara-service copybara-service Bot force-pushed the test_918116236 branch 13 times, most recently from 43e9726 to b5504bf Compare May 27, 2026 20:49
…re, moving from manual span management to structured helper classes

### Key Changes

*   **Structured Instrumentation:** Replaces manual `Tracing.trace` calls and explicit `Scope` management with `Flowable.using` and `Maybe.using` patterns. It introduces helper classes like `AgentInvocation` and `ToolExecution` to encapsulate telemetry logic.
*   **Metrics Integration:** Adds support for tracking new metrics during agent execution:
    *   `gen_ai.agent.invocation.duration`
    *   `gen_ai.agent.request.size` / `gen_ai.agent.response.size`
    *   `gen_ai.agent.workflow.steps`
*   **Reactive API Improvements:** Leverages `Tracing.withContext()` and `doOnNext`/`doOnError` hooks within `AgentInvocation` and `ToolExecution` to automatically capture events and errors without polluting the core logic.
*   **Trace Hierarchy Refinement:** Updates how spans are nested. For example, in `ContextPropagationTest`, the child agent span is now correctly parented to the specific LLM call span that triggered it, rather than the parent agent span.
*   **Testing:** Significantly enhances `BaseAgentTest` to verify metric collection using `InMemoryMetricReader`. Updates various tests to match the new span naming convention (e.g., removing brackets from tool execution spans).

### Impact
This refactor simplifies the core agent and flow logic by removing boilerplate telemetry code, making the instrumentation more robust and easier to maintain while expanding the observability of the ADK through new metrics.

PiperOrigin-RevId: 918116236
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant