Skip to content

Started perf optimizations - #7917

Open
irinascurtu wants to merge 1 commit into
otelfrom
perf
Open

Started perf optimizations#7917
irinascurtu wants to merge 1 commit into
otelfrom
perf

Conversation

@irinascurtu

@irinascurtu irinascurtu commented Aug 27, 2026

Copy link
Copy Markdown

Skip OTel enrichment work when IsAllDataRequested is false

IsAllDataRequested, is represented by https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/api.md#isrecording

https://github.com/open-telemetry/opentelemetry-dotnet/blob/main/src/OpenTelemetry.Api/README.md#:~:text=a%20null%20check.-,Populate,-activity%20with%20tags

ActivityFactory was only null-checking activities before doing enrichment work (promoting
headers to tags, recording exceptions, updating recoverability tags/DisplayName), even when a
listener sampled the activity as PropagationData-only (IsAllDataRequested == false). In that
case the work is wasted: nothing reads it.

This guards the expensive paths behind IsAllDataRequested, matching the pattern already used
by TryGetRecordingOutgoingPipelineActivity/TryGetRecordingIncomingPipelineActivity:

  • PromoteHeadersToTags (header -> tag mapping on incoming messages) - skipped when not
    requested. Context propagation (baggage/tracestate) stays unconditional since it's
    correctness, not enrichment.
  • RecordError - the exception event (stack trace) and legacy
    otel.status_code/otel.status_description tags are now guarded. SetStatus, the
    ErrorType tag, and the Logs-mode logging fallback stay unconditional.
  • UpdateActivityFromRecoverabilityAction - now a no-op when not requested.

No behavior change for fully-sampled or no-listener cases. Added a PropagationDataSampling
test class in ActivityFactoryTests.cs (and a sampling-result parameter on the shared
TestingActivityListener, default unchanged) to cover the previously untested PropagationData
path.

Copilot AI lite review requested due to automatic review settings August 27, 2026 12:45

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

This PR introduces performance optimizations in the OpenTelemetry tracing instrumentation by skipping tag/event work when activities are created for propagation-only sampling (i.e., when Activity.IsAllDataRequested is false), while still ensuring context propagation remains correct.

Changes:

  • Skip header-to-tag promotion when an activity is sampled as PropagationData.
  • Bail out of recoverability activity updates when full activity data isn’t requested.
  • Extend test infrastructure and add coverage for PropagationData sampling behavior.

Reviewed changes

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

File Description
src/NServiceBus.Core/OpenTelemetry/Tracing/ActivityFactory.cs Skips tag/event work for propagation-only activities; adjusts error recording and recoverability updates.
src/NServiceBus.Core/OpenTelemetry/Tracing/ActivityExtensions.cs Updates comment describing the IsAllDataRequested guard for “expensive” work.
src/NServiceBus.Core.Tests/OpenTelemetry/Helpers/TestingActivityListener.cs Allows configuring sampling result in tests (AllData vs PropagationData).
src/NServiceBus.Core.Tests/OpenTelemetry/ActivityFactoryTests.cs Adds tests validating PropagationData behavior (no header tag promotion, no exception events/legacy tags, no recoverability updates).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 226 to 230
if (!exception.Data.Contains(ExceptionRecordedFlag))
{
if (Options.ExceptionRecordingMode == ExceptionRecordingMode.Logs)
{
Logger.Error($"An exception occurred while executing '{activity.DisplayName}'.", exception);
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.

2 participants