Skip to content

Allow changing trace continuation behavior for delayed messages - #7845

Merged
tmasternak merged 21 commits into
otelfrom
tracing-recoverability-action
Jul 29, 2026
Merged

Allow changing trace continuation behavior for delayed messages#7845
tmasternak merged 21 commits into
otelfrom
tracing-recoverability-action

Conversation

@ramonsmits

@ramonsmits ramonsmits commented Jul 10, 2026

Copy link
Copy Markdown
Member

Before this change, delayed messages (explicit delayed sends, saga timeouts, and recoverability delayed retries) have always started a new OpenTelemetry trace when received, with a link back to the original trace.

New options

This PR introduces three new settings under endpointConfiguration.Tracing():

  • DelayedDelivery.SendOperationTraceMode
  • DelayedDelivery.SagaTimeoutTraceMode
  • Recoverability.DelayedRetryTraceMode

Controlled entirely on the sending side

Similarly to SendTraceMode/PublishTraceMode settings, it's the sender that decides and stamps the StartNewTrace header before dispatch (OpenTelemetrySendBehavior for delayed sends/timeouts, PopulateRecoverabilityTraceMetadataBehavior for delayed retries).

Backward compatible by default

For backwards compatibility reasons, the per-message options (SendOptions.StartNewTraceOnReceive() / ContinueExistingTraceOnReceive()) still do not apply to delayed messages. To change the behavior, new settings have to be used.

@ramonsmits ramonsmits self-assigned this Jul 10, 2026
@irinascurtu
irinascurtu marked this pull request as ready for review July 13, 2026 11:49
Comment thread src/NServiceBus.Core/OpenTelemetry/Tracing/ActivityFactory.cs Outdated
Comment thread src/NServiceBus.Core/Recoverability/RecoverabilityRoutingConnector.cs Outdated
@tmasternak tmasternak assigned tmasternak and unassigned ramonsmits Jul 22, 2026
Comment thread src/NServiceBus.Core/OpenTelemetry/OpenTelemetryFeature.cs
Comment thread src/NServiceBus.Core/OpenTelemetry/InstrumentationOptions.cs Outdated
ramonsmits and others added 10 commits July 28, 2026 12:51
…e` in tests and implementation to enforce singleton usage.
…s for better trace clarity in recoverability pipeline.
…s to improve trace clarity in the recoverability pipeline.
…s, sagas, and recoverability) are happening before RoutingContext stage, and that StartNewTrace header is properly set before reaching that state.
…y set recoverability trace metadata for DelayedRetry
…layed-message trace mode

The dedicated "Recoverability" span/activity work (ActivityFactory.StartRecoverabilityActivity
and its wiring/tagging) has been split out to the recoverability-action-spans branch. This PR
now only concerns how trace mode is decided for delayed sends, saga timeouts, and delayed
retries via InstrumentationOptions.
@tmasternak
tmasternak force-pushed the tracing-recoverability-action branch from 39c64a0 to c9769e2 Compare July 28, 2026 12:09
… refactor `ActivityFactory` constructor for improved initialization consistency
…regression

Covers the three delayed-message origins this PR configures: explicit delayed
sends (SendOperationTraceMode), saga timeouts (SagaTimeoutTraceMode), and
recoverability delayed retries (Recoverability.DelayedRetryTraceMode). Each
area verifies the backward-compatible default (start a new linked trace) and
that the new option flips it to continuing the existing trace. The delayed-send
tests also cover per-message override precedence: an explicit StartNewTraceOnReceive
always wins, and a ContinueExistingTraceOnReceive request cannot defeat the
delayed-send backward-compatible default.

Also fixes When_retrying_messages.Should_correlate_delayed_retry_with_send, which
asserted the opposite (same-trace) behavior and never actually ran due to
Requires.DelayedDelivery() gating - switched to the in-memory transport's native
delayed delivery support so it exercises the real code path.

Restores PopulateRecoverabilityTraceMetadataBehavior's MoveToError handling
(dropped when DelayedRetry-only gating was introduced), which had silently
broken When_incoming_message_moved_to_error_queue.Should_add_start_new_trace_header.
MoveToError itself stays hardcoded to always start a new trace, matching
pre-existing behavior; it was not part of this PR's configurable scope after
MoveToErrorTraceMode was removed from InstrumentationOptions.
Comment thread src/NServiceBus.Core/OpenTelemetry/InstrumentationOptions.cs
tmasternak and others added 3 commits July 28, 2026 15:09
Replaces `TestEndpointContinuingTrace` with a unified `TestEndpoint` that dynamically configures `TraceMode` for delayed sends and saga timeouts. Simplifies `RetryingEndpoint` initialization. Removes redundant handler setup in trace tests.
Comment thread src/NServiceBus.Core/OpenTelemetry/InstrumentationOptions.cs Outdated
Comment thread src/NServiceBus.Core/OpenTelemetry/InstrumentationOptions.cs Outdated
Comment thread src/NServiceBus.Core/OpenTelemetry/InstrumentationOptions.cs Outdated
tmasternak and others added 4 commits July 28, 2026 15:27
Co-authored-by: Tomasz Masternak <tomasz.masternak@particular.net>
@tmasternak tmasternak changed the title Add tracing for recoverability pipeline using ActivityFactory Allow changing trace continuation behavior for delayed messages Jul 28, 2026
@tmasternak
tmasternak merged commit a457e83 into otel Jul 29, 2026
3 checks passed
@tmasternak
tmasternak deleted the tracing-recoverability-action branch July 29, 2026 08:52
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.

3 participants