Skip to content

fix(pubsub): make process span current during receiveMessage()#13276

Open
irock wants to merge 1 commit into
googleapis:mainfrom
irock:irock/fix-context
Open

fix(pubsub): make process span current during receiveMessage()#13276
irock wants to merge 1 commit into
googleapis:mainfrom
irock:irock/fix-context

Conversation

@irock

@irock irock commented May 26, 2026

Copy link
Copy Markdown

The subscribe process span is created but never set as the current context before calling MessageReceiver.receiveMessage(). This means user code inside the callback cannot create child spans under the process span, and downstream calls are not parented correctly.

Wrap the receiveMessage() calls in MessageDispatcher with Span.makeCurrent() so Context.current() contains the process span during user code execution. Add a getter for the process span on PubsubMessageWrapper and a test verifying child span parenting.

edit:

Assuming topic A & B, and a subscriber A on topic A, the intention is that this change will result in spans to have a hierarchy similar to the the following:

parent
└── topic A publish
    └── subscription A subscriber
        └── subscription A user code
            └── topic B publish

What we are currently seeing is the following:

parent
└── topic A publish
    ├── subscription A subscriber
    └── subscription A user code
      └── topic B publish

The subscribe process span was created but never set as the current
context before calling MessageReceiver.receiveMessage(). This meant
user code inside the callback could not create child spans under the
process span, and downstream calls were not parented correctly.

Wrap the receiveMessage() calls in MessageDispatcher with
Span.makeCurrent() so Context.current() contains the process span
during user code execution. Add a getter for the process span on
PubsubMessageWrapper and a test verifying child span parenting.

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

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.

Code Review

This pull request integrates OpenTelemetry tracing into the message dispatching process. Specifically, it ensures that the subscription process span is set as the current scope when receiving and processing Pub/Sub messages. A new unit test has also been added to verify this behavior. The review feedback suggests simplifying the try-with-resources statement by using null instead of Scope.noop(), which is more idiomatic in Java and avoids unnecessary API dependencies.

new AckReplyConsumerImpl(ackReplySettableApiFuture);
receiver.receiveMessage(message, ackReplyConsumer);
final Span processSpan = messageWrapper.getSubscribeProcessSpan();
try (Scope ignored = processSpan != null ? processSpan.makeCurrent() : Scope.noop()) {

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.

medium

In Java, a try-with-resources statement gracefully handles a null resource by doing nothing (no close() is called and no exception is thrown). We can simplify this by using null instead of Scope.noop(), which avoids the dependency on the Scope.noop() API and is more idiomatic.

Suggested change
try (Scope ignored = processSpan != null ? processSpan.makeCurrent() : Scope.noop()) {
try (Scope ignored = processSpan != null ? processSpan.makeCurrent() : null) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I believe the library is targeting Java 8, where a try-with-resources with a null resource will throw.

@irock
irock marked this pull request as ready for review May 26, 2026 21:52
@irock
irock requested review from a team as code owners May 26, 2026 21:52
@blakeli0
blakeli0 requested a review from michaelpri10 May 28, 2026 16:04
@irock

irock commented May 28, 2026

Copy link
Copy Markdown
Author

Added some more context to the description.

@michaelpri10

Copy link
Copy Markdown
Contributor

/gcbrun

@irock

irock commented Jun 9, 2026

Copy link
Copy Markdown
Author

Thanks @michaelpri10 ! Is there anything else I need to do to get the PR merged? The "multi-approvers" check is failing for some reason.

@michaelpri10
michaelpri10 requested a review from blakeli0 June 11, 2026 04:32
@michaelpri10

Copy link
Copy Markdown
Contributor

Added @blakeli0 as another reviewer to get multi-approval. After that, I think this PR should be ready to be merged.

@irock

irock commented Jun 17, 2026

Copy link
Copy Markdown
Author

@michaelpri10 Looks like one more approval is needed :)

@phantasia15

Copy link
Copy Markdown

Hi! Just checking in—is there any chance this PR could be merged soon? I'm looking forward to using it.

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.

4 participants