Skip to content

feat: Add telemetry and metrics recording capabilities#1206

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

feat: Add telemetry and metrics recording capabilities#1206
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_917904663

Conversation

@copybara-service
Copy link
Copy Markdown

feat: Add telemetry and metrics recording capabilities

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.

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
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