Skip to content

refactor: Simplifying the code around tracing#1296

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

refactor: Simplifying the code around tracing#1296
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_936674781

Conversation

@copybara-service

Copy link
Copy Markdown

refactor: Simplifying the code around tracing

This change simplifies telemetry and tracing logic in the ADK by migrating from manual resource management and helper methods to RxJava Transformers.

Key Changes

  • Introduced Telemetry Transformers: Created AgentInvocationTransformer and ToolExecutionTransformer in Instrumentation.java. These encapsulate the lifecycle of OpenTelemetry spans and metrics within RxJava's compose operator, replacing manual Flowable.using blocks in the business logic.
  • Refactored BaseAgent and Functions: Updated BaseAgent.run and tool execution logic in Functions.java to use these new transformers. This results in cleaner, more readable reactive pipelines.
  • Improved Tracing Lifecycle: Refactored TracerProvider in Tracing.java to use Flowable.using (and equivalents) instead of doOnSubscribe/doFinally. This ensures that spans and scopes are more reliably closed across various RxJava terminal events.
  • Cleaned up Instrumentation API:
    • Removed recordAgentInvocation and recordToolExecution static helpers in favor of the transformer pattern.
    • Added a makeCurrent() method to ClosableTelemetryScope to allow re-activating scopes within the reactive lifecycle.
  • Removed Unused Code: Deleted traceAgent from Tracing.java as it is no longer needed with the new transformer-based approach.

This change simplifies telemetry and tracing logic in the ADK by migrating from manual resource management and helper methods to **RxJava Transformers**.

### Key Changes

*   **Introduced Telemetry Transformers**: Created `AgentInvocationTransformer` and `ToolExecutionTransformer` in `Instrumentation.java`. These encapsulate the lifecycle of OpenTelemetry spans and metrics within RxJava's `compose` operator, replacing manual `Flowable.using` blocks in the business logic.
*   **Refactored `BaseAgent` and `Functions`**: Updated `BaseAgent.run` and tool execution logic in `Functions.java` to use these new transformers. This results in cleaner, more readable reactive pipelines.
*   **Improved Tracing Lifecycle**: Refactored `TracerProvider` in `Tracing.java` to use `Flowable.using` (and equivalents) instead of `doOnSubscribe`/`doFinally`. This ensures that spans and scopes are more reliably closed across various RxJava terminal events.
*   **Cleaned up `Instrumentation` API**:
    *   Removed `recordAgentInvocation` and `recordToolExecution` static helpers in favor of the transformer pattern.
    *   Added a `makeCurrent()` method to `ClosableTelemetryScope` to allow re-activating scopes within the reactive lifecycle.
*   **Removed Unused Code**: Deleted `traceAgent` from `Tracing.java` as it is no longer needed with the new transformer-based approach.

PiperOrigin-RevId: 936674781
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