Skip to content

refactor: use RxJava's using operator instead of the defer + doFinally pattern#1327

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

refactor: use RxJava's using operator instead of the defer + doFinally pattern#1327
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_942123391

Conversation

@copybara-service

Copy link
Copy Markdown

refactor: use RxJava's using operator instead of the defer + doFinally pattern

Key Changes:

  • Improved Resource Management: Migrates traceFlowable and TracingTransformer in Tracing.java to use Flowable.using, Single.using, etc. This ensures that the OpenTelemetry Scope is strictly bound to the stream's lifecycle, providing better guarantees for closure during cancellation or errors compared to doFinally.
  • Fixes Lazy Context Activation: Replaced manual try-with-resources blocks in BaseLlmFlow.java with deferred execution (Maybe.defer) and context re-activation (Tracing.withContext). This ensures the context is active when the RxJava pipeline actually executes, rather than when it is assembled.
  • Refactored TracingObserver: Removed static factory methods in TracingObserver in favor of direct constructor calls within the lift operators.
  • Added Testing: Introduced testTraceFlowableLazy in ContextPropagationTest to verify that tracing is applied lazily at subscription time and that the scope is correctly managed.
  • Cleanup: Updated documentation and added @SuppressWarnings("MustBeClosedChecker") where the scope lifecycle is intentionally managed by RxJava's resource management operators.

…inally` pattern

### Key Changes:

*   **Improved Resource Management**: Migrates `traceFlowable` and `TracingTransformer` in `Tracing.java` to use `Flowable.using`, `Single.using`, etc. This ensures that the OpenTelemetry `Scope` is strictly bound to the stream's lifecycle, providing better guarantees for closure during cancellation or errors compared to `doFinally`.
*   **Fixes Lazy Context Activation**: Replaced manual `try-with-resources` blocks in `BaseLlmFlow.java` with deferred execution (`Maybe.defer`) and context re-activation (`Tracing.withContext`). This ensures the context is active when the RxJava pipeline actually executes, rather than when it is assembled.
*   **Refactored TracingObserver**: Removed static factory methods in `TracingObserver` in favor of direct constructor calls within the `lift` operators.
*   **Added Testing**: Introduced `testTraceFlowableLazy` in `ContextPropagationTest` to verify that tracing is applied lazily at subscription time and that the scope is correctly managed.
*   **Cleanup**: Updated documentation and added `@SuppressWarnings("MustBeClosedChecker")` where the scope lifecycle is intentionally managed by RxJava's resource management operators.

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