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
Conversation
43e9726 to
b5504bf
Compare
…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
b5504bf to
b904bb6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
feat: refactor OpenTelemetry (OTel) instrumentation within the ADK core, moving from manual span management to structured helper classes
Key Changes
Tracing.tracecalls and explicitScopemanagement withFlowable.usingandMaybe.usingpatterns. It introduces helper classes likeAgentInvocationandToolExecutionto encapsulate telemetry logic.gen_ai.agent.invocation.durationgen_ai.agent.request.size/gen_ai.agent.response.sizegen_ai.agent.workflow.stepsTracing.withContext()anddoOnNext/doOnErrorhooks withinAgentInvocationandToolExecutionto automatically capture events and errors without polluting the core logic.ContextPropagationTest, the child agent span is now correctly parented to the specific LLM call span that triggered it, rather than the parent agent span.BaseAgentTestto verify metric collection usingInMemoryMetricReader. 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.