Skip to content

[Service Bus] Fix trace context not propagated on first sendMessage() (#44958)#49600

Open
ksalazar-91 wants to merge 3 commits into
Azure:mainfrom
ksalazar-91:ksalazar-91/fix-servicebus-tracing-44958
Open

[Service Bus] Fix trace context not propagated on first sendMessage() (#44958)#49600
ksalazar-91 wants to merge 3 commits into
Azure:mainfrom
ksalazar-91:ksalazar-91/fix-servicebus-tracing-44958

Conversation

@ksalazar-91

Copy link
Copy Markdown

Summary

The first call to ServiceBusSenderClient.sendMessage() (and ServiceBusSenderAsyncClient.sendMessage()) did not recognize the caller's current OpenTelemetry trace context. The ServiceBus.send span and the outgoing message's traceparent started a new, disconnected trace instead of being a child of the caller's active span. Subsequent sends were correct.

Root cause

The send span was started lazily (inside Mono.defer) downstream of the first AMQP connection/link establishment. On the first send that work runs on a background AMQP thread, where the caller's thread-local OpenTelemetry context is not available, so the span fell back to Context.current() (empty) and began a new trace. Once the link was cached, later sends started the span on the caller thread and parented correctly.

Fix (Service Bus only)

The single-message send path (sendFluxInternal) now starts the producer message span and the ServiceBus.send span on the subscribing (caller) thread, before the connection thread hop — mirroring the structure already used by the batch send path and by Event Hubs. A non-instrumenting overload of sendBatchInternal (instrument=false) is used by this path to avoid a duplicate span.

  • No azure-core / azure-core-tracing-opentelemetry changes.
  • No public API changes.
  • Batch send paths (sendMessages) are unchanged.

Validation

  • 988 unit tests pass; Checkstyle + SpotBugs pass.
  • 6 live tracing integration tests pass against a real namespace, including a new regression test TracingIntegrationTests.sendMessageHasParentSpanOnFirstCall that asserts the first sendMessage() inherits the caller's trace id (span + injected traceparent).
  • Reproduced the bug and verified the fix end-to-end against a live Service Bus namespace.

Fixes #44958

…Azure#44958)

The first call to ServiceBusSenderClient.sendMessage() (and the async
client) did not recognize the caller's current OpenTelemetry trace
context: the ServiceBus.send span and the outgoing message's traceparent
started a new, disconnected trace. This happened because the span was
started lazily downstream of the first AMQP connection/link
establishment, which runs on a background thread where the caller's
thread-local context is not available.

The single-message send path now starts the producer message span and
the ServiceBus.send span on the subscribing (caller) thread, before the
connection thread hop, mirroring the structure already used by the batch
send path and Event Hubs. A non-instrumenting overload of
sendBatchInternal avoids a duplicate span.

Adds a live regression test (sendMessageHasParentSpanOnFirstCall) and a
CHANGELOG entry.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 23, 2026 02:45
@github-actions github-actions Bot added Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. Service Bus labels Jun 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution @ksalazar-91! We will review the pull request and get back to you soon.

Copilot AI 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.

Pull request overview

Fixes Service Bus tracing so the first sendMessage() call correctly parents ServiceBus.send / ServiceBus.message spans (and injected traceparent) to the caller’s current OpenTelemetry context, avoiding a new/disconnected trace on initial link establishment.

Changes:

  • Refactored the single-message send path to start producer/message and send instrumentation at subscription time (before the first AMQP thread hop), and to avoid double-instrumentation.
  • Added a live tracing regression test ensuring the first sendMessage() inherits the caller’s trace id and injects the expected traceparent.
  • Documented the fix in the Service Bus changelog.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
sdk/servicebus/azure-messaging-servicebus/src/main/java/com/azure/messaging/servicebus/ServiceBusSenderAsyncClient.java Adjusts single-message send pipeline so tracing spans are created on the subscribing thread and avoids duplicate ServiceBus.send spans.
sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/TracingIntegrationTests.java Adds a regression test validating correct parent trace propagation on the first sendMessage() call and traceparent injection.
sdk/servicebus/azure-messaging-servicebus/CHANGELOG.md Records the tracing context propagation bug fix for sendMessage() first-call behavior.

Comment on lines +170 to +177
List<ReadableSpan> send = findSpans(spans, "ServiceBus.send");
assertEquals(expectedTraceId, send.get(0).getSpanContext().getTraceId());
assertEquals(expectedTraceId, send.get(0).getParentSpanContext().getTraceId());

List<ReadableSpan> messageSpans = findSpans(spans, "ServiceBus.message");
assertMessageSpan(messageSpans.get(0), message);
assertEquals(expectedTraceId, messageSpans.get(0).getSpanContext().getTraceId());

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.

Good suggestion - addressed in 3f5fdd7. The test now asserts exactly one ServiceBus.send span and exactly one ServiceBus.message span for a single sendMessage() call, so an accidental double-instrumentation regression would be caught.

karenSalazar91 and others added 2 commits June 22, 2026 20:12
…cebus-tracing-44958

# Conflicts:
#	sdk/servicebus/azure-messaging-servicebus/CHANGELOG.md
Add exact-count assertions (one ServiceBus.send and one ServiceBus.message
span per single sendMessage()) so an accidental double-instrumentation
regression is caught, per PR review feedback.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. Service Bus

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Current trace context not recognized on first call to ServiceBusSenderClient.sendMessage()

3 participants