Skip to content

chore: Ensure consistent DT header insertion/replacement logic across all wrappers#3512

Merged
tippmar-nr merged 4 commits intomainfrom
chore/dt-header-replacement-consistency
Apr 2, 2026
Merged

chore: Ensure consistent DT header insertion/replacement logic across all wrappers#3512
tippmar-nr merged 4 commits intomainfrom
chore/dt-header-replacement-consistency

Conversation

@tippmar-nr
Copy link
Copy Markdown
Member

Description

Validates and updates all distributed trace header insertion/replacement logic to ensure consistent behavior everywhere. Only SQS, RestSharp and the (very old) Wcf3 ChannelFactory wrapper were implemented inconsistently.

Adds unit and integration tests that ensure the replacement logic works correctly everywhere.

See the related Jira ticket and the Google doc referenced in it for a report on the entire inventory of DT header insertion across the .NET agent.

One change that's important to note is in the RestSharp AppendHeaders wrapper - header replacement was moved to the onComplete delegate to ensure anything added by the real AppendHeaders call would be correctly replaced if there was a collision. This was potentially a bug in the original implementation, but would have been very unlikely to occur.

Some wrapper implementations could not be tested easily or did not need a test because of the way the underlying library handles headers:

  • NServiceBus — uses dictionary indexer headers[key] = value which inherently replaces. Headers are managed internally
    by the NServiceBus pipeline, so pre-populating DT headers before the agent's hook would require a custom pipeline
    behavior.
  • MassTransit (current and legacy) — uses SendHeaders.Set(key, value) which is MassTransit's own API that replaces by
    design. Pre-populating would require a custom ISendObserver or IFilter.
  • WCF NetTcp — the Message.Headers are not directly accessible before the agent's BeforeSendRequest inspector runs in
    the WCF client programming model, so there's no natural way for pre-existing DT headers to appear. The fix (FindHeader + RemoveAt + Add) was validated by code review.

Author Checklist

  • Unit tests, Integration tests, and Unbounded tests completed
  • Performance testing completed with satisfactory results (if required)

Reviewer Checklist

  • Perform code review
  • Pull request was adequately tested (new/existing tests, performance tests)

@tippmar-nr tippmar-nr requested a review from a team as a code owner April 1, 2026 19:29
Copy link
Copy Markdown
Member

@nr-ahemsath nr-ahemsath left a comment

Choose a reason for hiding this comment

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

Overall this looks great, I just have a minor concern about magic string values being hardcoded in both test code and exerciser code; this may be a fundamental limitation of how our tests are structured, but it least should be documented very clearly that those magic values have to match exactly in order for the tests to actually be verifying what we want them to.

Move existing-headers produce+consume before cancellation-token
consumers to prevent race where cancellation consumer could grab the
larger existing-headers message. Widen byte count assertion range to
accommodate non-deterministic span ordering from FirstOrDefault.
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.81%. Comparing base (24819c4) to head (5f5fd3b).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3512      +/-   ##
==========================================
+ Coverage   81.79%   81.81%   +0.01%     
==========================================
  Files         508      508              
  Lines       34220    34228       +8     
  Branches     4040     4040              
==========================================
+ Hits        27990    28003      +13     
+ Misses       5265     5257       -8     
- Partials      965      968       +3     
Flag Coverage Δ
Agent 82.80% <100.00%> (+0.02%) ⬆️
Profiler 71.75% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ions/NewRelic.Agent.Extensions/AwsSdk/SqsHelper.cs 91.42% <100.00%> (+1.10%) ⬆️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tippmar-nr tippmar-nr merged commit d6634c8 into main Apr 2, 2026
117 checks passed
@tippmar-nr tippmar-nr deleted the chore/dt-header-replacement-consistency branch April 2, 2026 17:48
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.

5 participants