feat: Add telemetry and metrics recording capabilities#1206
Open
copybara-service[bot] wants to merge 1 commit into
Open
feat: Add telemetry and metrics recording capabilities#1206copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
The key changes include:
1. **New `Instrumentation.java` class:** This class provides a unified context manager for instrumenting agent invocations and tool executions. It uses OpenTelemetry to create trace spans, record exceptions, and manage the scope of telemetry contexts. It includes inner classes `AgentInvocation` and `ToolExecution`, both implementing `AutoCloseable` to automatically handle the lifecycle of spans and metric recording.
2. **New `Metrics.java` class:** This utility class is responsible for defining and recording various OpenTelemetry metrics (histograms) related to ADK components. These metrics cover:
* Agent invocation duration, request size, response size, and workflow steps.
* Tool execution duration, request size, and response size.
The class uses `GlobalOpenTelemetry` to get a `Meter` and defines static histogram instances. It includes methods to record values for these metrics, often including attributes like agent name, tool name, and error type.
3. **New Unit Tests:**
* `InstrumentationTest.java`: Contains unit tests for the `Instrumentation` class, verifying that spans are created correctly, contexts are managed, and metrics are recorded for both successful and error scenarios during agent invocations and tool executions. It uses `OpenTelemetryRule` for testing.
* `MetricsTest.java`: Contains unit tests for the `Metrics` class, ensuring that the various static methods correctly record histogram data with the expected values and attributes. It also uses `OpenTelemetryRule`.
PiperOrigin-RevId: 917904663
ef1350d to
8584c6e
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: Add telemetry and metrics recording capabilities
The key changes include:
New
Instrumentation.javaclass: This class provides a unified context manager for instrumenting agent invocations and tool executions. It uses OpenTelemetry to create trace spans, record exceptions, and manage the scope of telemetry contexts. It includes inner classesAgentInvocationandToolExecution, both implementingAutoCloseableto automatically handle the lifecycle of spans and metric recording.New
Metrics.javaclass: This utility class is responsible for defining and recording various OpenTelemetry metrics (histograms) related to ADK components. These metrics cover:The class uses
GlobalOpenTelemetryto get aMeterand defines static histogram instances. It includes methods to record values for these metrics, often including attributes like agent name, tool name, and error type.New Unit Tests:
InstrumentationTest.java: Contains unit tests for theInstrumentationclass, verifying that spans are created correctly, contexts are managed, and metrics are recorded for both successful and error scenarios during agent invocations and tool executions. It usesOpenTelemetryRulefor testing.MetricsTest.java: Contains unit tests for theMetricsclass, ensuring that the various static methods correctly record histogram data with the expected values and attributes. It also usesOpenTelemetryRule.