Skip to content

Conversation

@andrewlock
Copy link
Member

Summary of changes

Fixes InvalidOperationException in baggage handling

Reason for change

#7620 added support for event hubs to Azure Functions, but we've subsequently seen errors related to baggage:

Error : Error creating or populating scope.
System.InvalidOperationException
   at Datadog.Trace.Baggage.MergeInto(Baggage destination)
   at Datadog.Trace.ClrProfiler.AutoInstrumentation.Azure.Functions.AzureFunctionsCommon.CreateIsolatedFunctionScope[T](Tracer tracer, T context)

We can reproduce this if you pass the same baggage instance to itself in MergeInto, so we guard against that here.

Why are we merging baggage into ourself, you may well ask. Good question... I haven't investigated... that's a problem for @pablomartinezbernardo and @lucaspimentel 😄

Implementation details

Check that you're not merging into yourself

Test coverage

Added a unit test, confirmed it repros, then fixed it

@andrewlock andrewlock requested a review from a team as a code owner November 13, 2025 17:09
@andrewlock andrewlock added type:bug area:tracer The core tracer library (Datadog.Trace, does not include OpenTracing, native code, or integrations) area:serverless identified-by:telemetry labels Nov 13, 2025
@dd-trace-dotnet-ci-bot
Copy link

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (7810) and master.

✅ No regressions detected - check the details below

Full Metrics Comparison

FakeDbCommand

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration75.32 ± (75.49 - 76.23) ms76.38 ± (76.19 - 77.22) ms+1.4%✅⬆️
.NET Framework 4.8 - Bailout
duration78.93 ± (78.84 - 79.32) ms82.91 ± (82.65 - 83.76) ms+5.0%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1113.68 ± (1118.11 - 1128.98) ms1099.83 ± (1103.06 - 1113.04) ms-1.2%
.NET Core 3.1 - Baseline
process.internal_duration_ms22.85 ± (22.77 - 22.94) ms22.89 ± (22.81 - 22.97) ms+0.1%✅⬆️
process.time_to_main_ms86.64 ± (86.27 - 87.00) ms87.42 ± (86.97 - 87.87) ms+0.9%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.91 ± (10.91 - 10.92) MB10.92 ± (10.91 - 10.92) MB+0.0%✅⬆️
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms22.96 ± (22.88 - 23.03) ms22.95 ± (22.87 - 23.02) ms-0.1%
process.time_to_main_ms89.11 ± (88.75 - 89.48) ms88.39 ± (88.06 - 88.72) ms-0.8%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.95 ± (10.94 - 10.95) MB10.96 ± (10.96 - 10.96) MB+0.1%✅⬆️
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms218.63 ± (217.22 - 220.04) ms218.60 ± (217.24 - 219.97) ms-0.0%
process.time_to_main_ms548.80 ± (547.58 - 550.02) ms547.31 ± (546.15 - 548.48) ms-0.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed52.48 ± (52.46 - 52.50) MB52.61 ± (52.59 - 52.63) MB+0.3%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)-0.0%
.NET 6 - Baseline
process.internal_duration_ms21.68 ± (21.62 - 21.74) ms21.76 ± (21.69 - 21.83) ms+0.4%✅⬆️
process.time_to_main_ms75.70 ± (75.38 - 76.02) ms76.64 ± (76.30 - 76.97) ms+1.2%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.59 ± (10.58 - 10.59) MB10.62 ± (10.62 - 10.63) MB+0.3%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms21.65 ± (21.57 - 21.74) ms21.89 ± (21.82 - 21.96) ms+1.1%✅⬆️
process.time_to_main_ms77.11 ± (76.77 - 77.46) ms78.14 ± (77.77 - 78.51) ms+1.3%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.65 ± (10.65 - 10.66) MB10.66 ± (10.66 - 10.67) MB+0.1%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms206.78 ± (205.52 - 208.05) ms204.75 ± (203.37 - 206.13) ms-1.0%
process.time_to_main_ms513.55 ± (512.54 - 514.56) ms512.96 ± (512.04 - 513.88) ms-0.1%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed51.68 ± (51.64 - 51.71) MB51.78 ± (51.75 - 51.81) MB+0.2%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.0%✅⬆️
.NET 8 - Baseline
process.internal_duration_ms19.89 ± (19.83 - 19.96) ms19.91 ± (19.85 - 19.98) ms+0.1%✅⬆️
process.time_to_main_ms75.48 ± (75.14 - 75.82) ms75.24 ± (74.90 - 75.59) ms-0.3%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.64 ± (7.63 - 7.64) MB7.66 ± (7.65 - 7.66) MB+0.2%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms19.62 ± (19.57 - 19.68) ms19.90 ± (19.83 - 19.97) ms+1.4%✅⬆️
process.time_to_main_ms75.41 ± (75.12 - 75.69) ms76.29 ± (75.91 - 76.67) ms+1.2%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.68 ± (7.67 - 7.69) MB7.72 ± (7.71 - 7.73) MB+0.6%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms191.98 ± (191.10 - 192.85) ms190.30 ± (189.26 - 191.34) ms-0.9%
process.time_to_main_ms494.50 ± (493.35 - 495.65) ms489.19 ± (488.10 - 490.28) ms-1.1%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed38.89 ± (38.86 - 38.93) MB39.01 ± (38.97 - 39.05) MB+0.3%✅⬆️
runtime.dotnet.threads.count27 ± (27 - 27)27 ± (27 - 27)-0.0%

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration192.24 ± (192.21 - 192.99) ms197.51 ± (197.16 - 198.33) ms+2.7%✅⬆️
.NET Framework 4.8 - Bailout
duration194.95 ± (194.79 - 195.19) ms198.12 ± (198.04 - 198.89) ms+1.6%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1159.72 ± (1162.21 - 1170.75) ms1162.79 ± (1163.95 - 1172.18) ms+0.3%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms188.05 ± (187.61 - 188.49) ms188.42 ± (188.09 - 188.74) ms+0.2%✅⬆️
process.time_to_main_ms80.48 ± (80.28 - 80.67) ms80.64 ± (80.45 - 80.82) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.09 ± (16.06 - 16.12) MB16.10 ± (16.07 - 16.13) MB+0.1%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (20 - 20)-0.1%
.NET Core 3.1 - Bailout
process.internal_duration_ms186.99 ± (186.58 - 187.40) ms188.78 ± (188.37 - 189.19) ms+1.0%✅⬆️
process.time_to_main_ms81.82 ± (81.58 - 82.06) ms82.23 ± (82.07 - 82.40) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.18 ± (16.16 - 16.21) MB16.11 ± (16.09 - 16.14) MB-0.4%
runtime.dotnet.threads.count21 ± (21 - 21)21 ± (21 - 21)-0.1%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms396.60 ± (393.96 - 399.24) ms397.26 ± (394.63 - 399.88) ms+0.2%✅⬆️
process.time_to_main_ms516.01 ± (515.32 - 516.70) ms519.55 ± (518.74 - 520.36) ms+0.7%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed63.00 ± (62.84 - 63.16) MB63.19 ± (63.05 - 63.34) MB+0.3%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 30)+0.1%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms191.69 ± (191.33 - 192.04) ms191.83 ± (191.57 - 192.10) ms+0.1%✅⬆️
process.time_to_main_ms69.52 ± (69.36 - 69.67) ms69.49 ± (69.35 - 69.62) ms-0.0%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed15.92 ± (15.77 - 16.08) MB16.09 ± (15.96 - 16.22) MB+1.1%✅⬆️
runtime.dotnet.threads.count18 ± (18 - 19)19 ± (19 - 19)+1.5%✅⬆️
.NET 6 - Bailout
process.internal_duration_ms190.56 ± (190.26 - 190.86) ms191.08 ± (190.81 - 191.36) ms+0.3%✅⬆️
process.time_to_main_ms70.49 ± (70.38 - 70.60) ms70.45 ± (70.36 - 70.55) ms-0.1%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.05 ± (15.90 - 16.20) MB16.17 ± (16.04 - 16.30) MB+0.7%✅⬆️
runtime.dotnet.threads.count20 ± (19 - 20)19 ± (19 - 20)-2.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms411.64 ± (408.68 - 414.61) ms412.95 ± (409.59 - 416.32) ms+0.3%✅⬆️
process.time_to_main_ms487.44 ± (486.79 - 488.09) ms487.46 ± (486.89 - 488.02) ms+0.0%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed62.26 ± (62.12 - 62.39) MB61.90 ± (61.75 - 62.04) MB-0.6%
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)-0.1%
.NET 8 - Baseline
process.internal_duration_ms189.30 ± (189.03 - 189.57) ms190.03 ± (189.66 - 190.41) ms+0.4%✅⬆️
process.time_to_main_ms69.03 ± (68.83 - 69.24) ms69.06 ± (68.84 - 69.28) ms+0.0%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.82 ± (11.78 - 11.86) MB11.75 ± (11.73 - 11.78) MB-0.6%
runtime.dotnet.threads.count18 ± (18 - 18)18 ± (18 - 18)+0.6%✅⬆️
.NET 8 - Bailout
process.internal_duration_ms188.88 ± (188.60 - 189.16) ms189.00 ± (188.82 - 189.18) ms+0.1%✅⬆️
process.time_to_main_ms69.78 ± (69.70 - 69.87) ms69.94 ± (69.83 - 70.05) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.87 ± (11.83 - 11.91) MB11.86 ± (11.83 - 11.90) MB-0.0%
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)+0.1%✅⬆️
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms359.40 ± (357.90 - 360.90) ms360.07 ± (358.56 - 361.58) ms+0.2%✅⬆️
process.time_to_main_ms463.50 ± (462.76 - 464.24) ms460.48 ± (459.88 - 461.08) ms-0.7%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed50.31 ± (50.27 - 50.35) MB50.50 ± (50.47 - 50.52) MB+0.4%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)-0.2%
Comparison explanation

Execution-time benchmarks measure the whole time it takes to execute a program, and are intended to measure the one-off costs. Cases where the execution time results for the PR are worse than latest master results are highlighted in **red**. The following thresholds were used for comparing the execution times:

  • Welch test with statistical test for significance of 5%
  • Only results indicating a difference greater than 5% and 5 ms are considered.

Note that these results are based on a single point-in-time result for each branch. For full results, see the dashboard.

Graphs show the p99 interval based on the mean and StdDev of the test run, as well as the mean value of the run (shown as a diamond below the graph).

Duration charts
FakeDbCommand (.NET Framework 4.8)
gantt
    title Execution time (ms) FakeDbCommand (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7810) - mean (77ms)  : 69, 85
    master - mean (76ms)  : 71, 81

    section Bailout
    This PR (7810) - mean (83ms)  : 75, 92
    master - mean (79ms)  : 76, 82

    section CallTarget+Inlining+NGEN
    This PR (7810) - mean (1,108ms)  : 1035, 1181
    master - mean (1,124ms)  : 1044, 1203

Loading
FakeDbCommand (.NET Core 3.1)
gantt
    title Execution time (ms) FakeDbCommand (.NET Core 3.1)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7810) - mean (118ms)  : 109, 126
    master - mean (117ms)  : 110, 123

    section Bailout
    This PR (7810) - mean (119ms)  : 113, 125
    master - mean (119ms)  : 112, 127

    section CallTarget+Inlining+NGEN
    This PR (7810) - mean (796ms)  : 751, 841
    master - mean (801ms)  : 754, 847

Loading
FakeDbCommand (.NET 6)
gantt
    title Execution time (ms) FakeDbCommand (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7810) - mean (105ms)  : 100, 111
    master - mean (104ms)  : 98, 110

    section Bailout
    This PR (7810) - mean (107ms)  : 99, 115
    master - mean (105ms)  : 100, 111

    section CallTarget+Inlining+NGEN
    This PR (7810) - mean (749ms)  : 708, 791
    master - mean (750ms)  : 717, 783

Loading
FakeDbCommand (.NET 8)
gantt
    title Execution time (ms) FakeDbCommand (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7810) - mean (104ms)  : 96, 112
    master - mean (104ms)  : 96, 111

    section Bailout
    This PR (7810) - mean (105ms)  : 98, 112
    master - mean (103ms)  : 98, 108

    section CallTarget+Inlining+NGEN
    This PR (7810) - mean (709ms)  : 676, 741
    master - mean (716ms)  : 688, 744

Loading
HttpMessageHandler (.NET Framework 4.8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET Framework 4.8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7810) - mean (198ms)  : 189, 207
    master - mean (193ms)  : 189, 196

    section Bailout
    This PR (7810) - mean (198ms)  : 194, 202
    master - mean (195ms)  : 193, 197

    section CallTarget+Inlining+NGEN
    This PR (7810) - mean (1,168ms)  : 1110, 1226
    master - mean (1,166ms)  : 1106, 1227

Loading
HttpMessageHandler (.NET Core 3.1)
gantt
    title Execution time (ms) HttpMessageHandler (.NET Core 3.1)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7810) - mean (277ms)  : 271, 283
    master - mean (277ms)  : 271, 282

    section Bailout
    This PR (7810) - mean (279ms)  : 273, 286
    master - mean (277ms)  : 271, 283

    section CallTarget+Inlining+NGEN
    This PR (7810) - mean (948ms)  : 904, 993
    master - mean (942ms)  : 895, 988

Loading
HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7810) - mean (269ms)  : 265, 274
    master - mean (270ms)  : 264, 276

    section Bailout
    This PR (7810) - mean (270ms)  : 267, 273
    master - mean (269ms)  : 264, 274

    section CallTarget+Inlining+NGEN
    This PR (7810) - mean (931ms)  : 878, 984
    master - mean (930ms)  : 877, 983

Loading
HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7810) - mean (269ms)  : 263, 275
    master - mean (268ms)  : 263, 272

    section Bailout
    This PR (7810) - mean (268ms)  : 265, 271
    master - mean (268ms)  : 264, 272

    section CallTarget+Inlining+NGEN
    This PR (7810) - mean (852ms)  : 824, 881
    master - mean (855ms)  : 829, 880

Loading

@andrewlock andrewlock merged commit 6d184c7 into master Nov 14, 2025
158 checks passed
@andrewlock andrewlock deleted the andrew/fix-recurrsive-merge branch November 14, 2025 07:20
@github-actions github-actions bot added this to the vNext-v3 milestone Nov 14, 2025
pablomartinezbernardo added a commit that referenced this pull request Nov 18, 2025
## Summary of changes

Respect baggage in Azure messaging instrumentations. Related to #7810 

## Reason for change

Baggage was not being extracted from headers

## Implementation details

## Test coverage

Unit test `AzureFunctionsCommon.ExtractPropagatedContextFromMessaging`
with baggage merging. `CreateIsolatedFunctionScope` would have been more
complex to test.

## Other details
<!-- Fixes #{issue} -->


<!--  ⚠️ Note:

Where possible, please obtain 2 approvals prior to merging. Unless
CODEOWNERS specifies otherwise, for external teams it is typically best
to have one review from a team member, and one review from apm-dotnet.
Trivial changes do not require 2 reviews.

MergeQueue is NOT enabled in this repository. If you have write access
to the repo, the PR has 1-2 approvals (see above), and all of the
required checks have passed, you can use the Squash and Merge button to
merge the PR. If you don't have write access, or you need help, reach
out in the #apm-dotnet channel in Slack.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:serverless area:tracer The core tracer library (Datadog.Trace, does not include OpenTracing, native code, or integrations) identified-by:telemetry type:bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants