Skip to content

fix(gax): Manage span scope in SpanTracer#12680

Open
blakeli0 wants to merge 1 commit intomainfrom
link-span-to-context
Open

fix(gax): Manage span scope in SpanTracer#12680
blakeli0 wants to merge 1 commit intomainfrom
link-span-to-context

Conversation

@blakeli0
Copy link
Copy Markdown
Contributor

@blakeli0 blakeli0 commented Apr 4, 2026

This PR adds a new feature that manages the lifecycle of OpenTelemetry Scope in SpanTracer.

The span is set to current context at the start of a request, and is cleared from current context at the end of a request.

@blakeli0 blakeli0 requested a review from a team as a code owner April 4, 2026 04:59
@blakeli0 blakeli0 requested a review from westarle April 4, 2026 05:00
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces OpenTelemetry Scope management to the SpanTracer class to track active spans during execution attempts. Review feedback indicates that storing the Scope as a class field is unsafe for asynchronous environments where methods may be called on different threads, potentially leading to incorrect context restoration. Additionally, it is recommended to use a try-finally block and null checks when closing the scope to ensure exception safety and proper resource cleanup.

spanBuilder.setAllAttributes(ObservabilityUtils.toOtelAttributes(currentAttemptAttributes));

this.attemptSpan = spanBuilder.startSpan();
this.scope = attemptSpan.makeCurrent();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Managing an OpenTelemetry Scope as a class field is risky in asynchronous environments. Scope is thread-local and must be closed on the same thread it was opened. In gax-java, ApiTracer methods like attemptStarted and endAttempt are frequently called on different threads (e.g., the request thread and the gRPC callback thread). Closing the scope on a different thread will lead to incorrect context restoration and potential state corruption in OpenTelemetry.

Comment on lines +256 to 258
scope.close();
attemptSpan.end();
attemptSpan = null;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

To ensure exception safety and adhere to the LIFO rule for resource management, scope.close() should be wrapped in a try-finally block. This guarantees that attemptSpan.end() is called even if an unexpected exception occurs during scope closure. Additionally, a null check for scope is recommended to prevent potential NPEs if makeCurrent() failed or wasn't called, and the field should be cleared after closing.

    try {
      if (scope != null) {
        scope.close();
      }
    } finally {
      scope = null;
      attemptSpan.end();
      attemptSpan = null;
    }
References
  1. When managing a collection of closeable resources (e.g., scopes), ensure they are closed in the reverse order of their creation (LIFO). The implementation must be exception-safe to prevent resource leaks, meaning all opened resources should be closed even if exceptions occur during their creation or closing.

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