Skip to content

Small perf tweaks for AspNetCoreDiagnosticObserver#7963

Merged
andrewlock merged 5 commits intomasterfrom
andrew/single-span/2-minor-fixes
Dec 24, 2025
Merged

Small perf tweaks for AspNetCoreDiagnosticObserver#7963
andrewlock merged 5 commits intomasterfrom
andrew/single-span/2-minor-fixes

Conversation

@andrewlock
Copy link
Member

@andrewlock andrewlock commented Dec 17, 2025

Summary of changes

Small perf tweaks for existing AspNetCoreDiagnosticObserver

Reason for change

While experimenting with single span observer, noticed some (safe) tweaks we could make to the existing implementation. There are some other improvements we could make as well, but those are a little riskier, so may be best to delay them.

Implementation details

  • Use HttpContext.Items[string] instead of HttpContext.Features.Get<T> as it's a bit faster
  • Make Tracer/Security/Iast inputs readonly, instead of using a null-coalesce with every access
  • Move check outside of AddHeaderTagsToSpan given common case is to avoid the function call

Test coverage

Covered by existing tests

Other details

https://datadoghq.atlassian.net/browse/LANGPLAT-842

Part of a stack

@andrewlock andrewlock added the area:tracer The core tracer library (Datadog.Trace, does not include OpenTracing, native code, or integrations) label Dec 17, 2025
@andrewlock andrewlock requested a review from a team as a code owner December 17, 2025 17:07
@andrewlock andrewlock added the area:automatic-instrumentation Automatic instrumentation managed C# code (Datadog.Trace.ClrProfiler.Managed) label Dec 17, 2025
@andrewlock andrewlock requested review from a team as code owners December 17, 2025 17:07
@andrewlock andrewlock added the type:performance Performance, speed, latency, resource usage (CPU, memory) label Dec 17, 2025
@andrewlock andrewlock requested a review from a team as a code owner December 17, 2025 17:07
@dd-trace-dotnet-ci-bot
Copy link

dd-trace-dotnet-ci-bot bot commented Dec 17, 2025

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (7963) 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
duration68.29 ± (68.30 - 68.55) ms68.34 ± (68.36 - 68.59) ms+0.1%✅⬆️
.NET Framework 4.8 - Bailout
duration72.15 ± (72.01 - 72.25) ms72.22 ± (72.20 - 72.47) ms+0.1%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1000.20 ± (1004.21 - 1012.29) ms1006.76 ± (1009.54 - 1017.15) ms+0.7%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms21.91 ± (21.88 - 21.94) ms22.09 ± (22.06 - 22.13) ms+0.8%✅⬆️
process.time_to_main_ms78.44 ± (78.30 - 78.58) ms78.74 ± (78.60 - 78.88) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.91 ± (10.90 - 10.91) MB10.93 ± (10.92 - 10.93) MB+0.2%✅⬆️
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms21.84 ± (21.82 - 21.87) ms21.91 ± (21.89 - 21.93) ms+0.3%✅⬆️
process.time_to_main_ms79.76 ± (79.67 - 79.85) ms79.74 ± (79.64 - 79.84) ms-0.0%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.94 ± (10.93 - 10.94) MB10.95 ± (10.94 - 10.95) MB+0.1%✅⬆️
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms207.58 ± (206.18 - 208.98) ms250.91 ± (247.17 - 254.65) ms+20.9%✅⬆️
process.time_to_main_ms471.07 ± (470.46 - 471.67) ms470.70 ± (470.20 - 471.21) ms-0.1%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed48.09 ± (48.07 - 48.12) MB48.21 ± (48.19 - 48.23) MB+0.2%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+1.1%✅⬆️
.NET 6 - Baseline
process.internal_duration_ms20.85 ± (20.81 - 20.89) ms20.54 ± (20.51 - 20.57) ms-1.5%
process.time_to_main_ms68.23 ± (68.10 - 68.36) ms67.86 ± (67.74 - 67.98) ms-0.5%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.60 ± (10.60 - 10.61) MB10.64 ± (10.63 - 10.64) MB+0.3%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms20.66 ± (20.63 - 20.69) ms20.59 ± (20.57 - 20.61) ms-0.3%
process.time_to_main_ms68.85 ± (68.81 - 68.89) ms69.06 ± (69.00 - 69.11) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.66 ± (10.65 - 10.66) MB10.68 ± (10.67 - 10.68) MB+0.2%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms200.86 ± (199.65 - 202.07) ms246.88 ± (246.09 - 247.67) ms+22.9%✅⬆️
process.time_to_main_ms439.93 ± (439.44 - 440.41) ms439.02 ± (438.52 - 439.51) ms-0.2%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed48.29 ± (48.23 - 48.35) MB48.68 ± (48.65 - 48.71) MB+0.8%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.3%✅⬆️
.NET 8 - Baseline
process.internal_duration_ms18.84 ± (18.81 - 18.86) ms18.87 ± (18.84 - 18.90) ms+0.2%✅⬆️
process.time_to_main_ms66.93 ± (66.83 - 67.03) ms66.92 ± (66.82 - 67.02) ms-0.0%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.67 ± (7.66 - 7.67) MB7.67 ± (7.67 - 7.68) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms18.78 ± (18.76 - 18.80) ms18.84 ± (18.82 - 18.86) ms+0.3%✅⬆️
process.time_to_main_ms68.05 ± (67.99 - 68.10) ms68.09 ± (68.04 - 68.14) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.71 ± (7.71 - 7.72) MB7.72 ± (7.72 - 7.73) MB+0.2%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms177.99 ± (177.15 - 178.83) ms179.16 ± (178.08 - 180.25) ms+0.7%✅⬆️
process.time_to_main_ms422.62 ± (422.04 - 423.20) ms424.77 ± (424.22 - 425.32) ms+0.5%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed36.28 ± (36.25 - 36.32) MB36.28 ± (36.25 - 36.31) MB-0.0%
runtime.dotnet.threads.count27 ± (27 - 27)27 ± (27 - 27)-0.1%

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration193.60 ± (193.69 - 194.48) ms193.25 ± (193.16 - 193.93) ms-0.2%
.NET Framework 4.8 - Bailout
duration196.70 ± (196.63 - 197.22) ms196.58 ± (196.50 - 197.09) ms-0.1%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1106.34 ± (1110.70 - 1119.22) ms1115.50 ± (1121.24 - 1131.21) ms+0.8%✅⬆️
.NET Core 3.1 - Baseline
process.internal_duration_ms187.99 ± (187.63 - 188.35) ms187.18 ± (186.81 - 187.55) ms-0.4%
process.time_to_main_ms80.45 ± (80.26 - 80.64) ms80.76 ± (80.56 - 80.96) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.08 ± (16.05 - 16.11) MB16.08 ± (16.05 - 16.11) MB+0.0%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (19 - 20)-0.3%
.NET Core 3.1 - Bailout
process.internal_duration_ms186.56 ± (186.25 - 186.86) ms187.54 ± (187.23 - 187.85) ms+0.5%✅⬆️
process.time_to_main_ms81.70 ± (81.58 - 81.82) ms82.32 ± (82.18 - 82.46) ms+0.7%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed16.19 ± (16.16 - 16.22) MB16.13 ± (16.10 - 16.16) MB-0.4%
runtime.dotnet.threads.count21 ± (20 - 21)21 ± (20 - 21)+0.3%✅⬆️
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms396.85 ± (394.45 - 399.26) ms432.03 ± (429.17 - 434.89) ms+8.9%✅⬆️
process.time_to_main_ms473.16 ± (472.64 - 473.67) ms473.54 ± (473.01 - 474.06) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed58.59 ± (58.45 - 58.73) MB58.73 ± (58.62 - 58.84) MB+0.2%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 30)29 ± (29 - 29)-0.2%
.NET 6 - Baseline
process.internal_duration_ms192.75 ± (192.33 - 193.16) ms192.01 ± (191.76 - 192.26) ms-0.4%
process.time_to_main_ms69.91 ± (69.75 - 70.07) ms69.99 ± (69.87 - 70.12) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.13 ± (16.01 - 16.25) MB16.24 ± (16.13 - 16.35) MB+0.7%✅⬆️
runtime.dotnet.threads.count18 ± (18 - 19)18 ± (18 - 19)-0.0%
.NET 6 - Bailout
process.internal_duration_ms191.29 ± (191.04 - 191.54) ms191.64 ± (191.24 - 192.05) ms+0.2%✅⬆️
process.time_to_main_ms70.75 ± (70.66 - 70.84) ms71.01 ± (70.90 - 71.12) ms+0.4%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.06 ± (15.91 - 16.22) MB16.43 ± (16.38 - 16.48) MB+2.3%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 19)20 ± (20 - 20)+4.7%✅⬆️
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms410.74 ± (408.61 - 412.87) ms454.85 ± (453.18 - 456.52) ms+10.7%✅⬆️
process.time_to_main_ms444.61 ± (444.06 - 445.15) ms445.71 ± (445.28 - 446.15) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed59.28 ± (59.16 - 59.40) MB58.19 ± (58.09 - 58.28) MB-1.8%
runtime.dotnet.threads.count30 ± (30 - 30)29 ± (29 - 30)-0.3%
.NET 8 - Baseline
process.internal_duration_ms191.01 ± (190.59 - 191.43) ms191.06 ± (190.65 - 191.47) ms+0.0%✅⬆️
process.time_to_main_ms69.65 ± (69.45 - 69.84) ms69.89 ± (69.68 - 70.10) ms+0.3%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.72 ± (11.69 - 11.74) MB11.76 ± (11.74 - 11.79) MB+0.4%✅⬆️
runtime.dotnet.threads.count18 ± (18 - 18)18 ± (18 - 18)+0.1%✅⬆️
.NET 8 - Bailout
process.internal_duration_ms190.96 ± (190.53 - 191.40) ms189.92 ± (189.65 - 190.19) ms-0.5%
process.time_to_main_ms70.98 ± (70.82 - 71.13) ms70.80 ± (70.70 - 70.90) ms-0.2%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.77 ± (11.75 - 11.80) MB11.87 ± (11.84 - 11.90) MB+0.8%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)-0.1%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms367.17 ± (365.42 - 368.93) ms364.67 ± (362.95 - 366.40) ms-0.7%
process.time_to_main_ms429.01 ± (428.36 - 429.66) ms429.70 ± (429.03 - 430.37) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed47.92 ± (47.90 - 47.95) MB47.97 ± (47.93 - 48.02) MB+0.1%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)+0.0%✅⬆️
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 (7963) - mean (68ms)  : 67, 70
    master - mean (68ms)  : 67, 70

    section Bailout
    This PR (7963) - mean (72ms)  : 71, 74
    master - mean (72ms)  : 71, 73

    section CallTarget+Inlining+NGEN
    This PR (7963) - mean (1,013ms)  : 959, 1067
    master - mean (1,008ms)  : 951, 1066

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 (7963) - mean (106ms)  : 104, 108
    master - mean (106ms)  : 103, 108

    section Bailout
    This PR (7963) - mean (107ms)  : 105, 108
    master - mean (107ms)  : 105, 108

    section CallTarget+Inlining+NGEN
    This PR (7963) - mean (746ms)  : crit, 690, 803
    master - mean (705ms)  : 676, 735

Loading
FakeDbCommand (.NET 6)
gantt
    title Execution time (ms) FakeDbCommand (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7963) - mean (93ms)  : 91, 95
    master - mean (94ms)  : 92, 96

    section Bailout
    This PR (7963) - mean (94ms)  : 93, 95
    master - mean (94ms)  : 93, 95

    section CallTarget+Inlining+NGEN
    This PR (7963) - mean (710ms)  : crit, 691, 728
    master - mean (669ms)  : 649, 689

Loading
FakeDbCommand (.NET 8)
gantt
    title Execution time (ms) FakeDbCommand (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7963) - mean (92ms)  : 90, 94
    master - mean (92ms)  : 90, 94

    section Bailout
    This PR (7963) - mean (93ms)  : 92, 94
    master - mean (93ms)  : 92, 94

    section CallTarget+Inlining+NGEN
    This PR (7963) - mean (632ms)  : 619, 646
    master - mean (628ms)  : 615, 641

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 (7963) - mean (194ms)  : 189, 198
    master - mean (194ms)  : 190, 199

    section Bailout
    This PR (7963) - mean (197ms)  : 194, 200
    master - mean (197ms)  : 194, 200

    section CallTarget+Inlining+NGEN
    This PR (7963) - mean (1,126ms)  : 1051, 1202
    master - mean (1,115ms)  : 1052, 1178

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 (7963) - mean (276ms)  : 271, 282
    master - mean (277ms)  : 272, 282

    section Bailout
    This PR (7963) - mean (278ms)  : 274, 283
    master - mean (277ms)  : 273, 280

    section CallTarget+Inlining+NGEN
    This PR (7963) - mean (935ms)  : 891, 979
    master - mean (907ms)  : 869, 944

Loading
HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7963) - mean (270ms)  : 266, 274
    master - mean (271ms)  : 265, 277

    section Bailout
    This PR (7963) - mean (271ms)  : 266, 276
    master - mean (270ms)  : 266, 274

    section CallTarget+Inlining+NGEN
    This PR (7963) - mean (934ms)  : 898, 970
    master - mean (888ms)  : 852, 925

Loading
HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (7963) - mean (271ms)  : 263, 278
    master - mean (270ms)  : 265, 275

    section Bailout
    This PR (7963) - mean (270ms)  : 267, 273
    master - mean (271ms)  : 266, 277

    section CallTarget+Inlining+NGEN
    This PR (7963) - mean (824ms)  : 803, 845
    master - mean (826ms)  : 802, 851

Loading

andrewlock added a commit that referenced this pull request Dec 23, 2025
## Summary of changes

Adds a `ValueStringBuilder` implementation, based on the one used
internally in the .NET runtime

## Reason for change

- We can stackalloc the `Span<char>`
- It's a bit faster than our existing `StringBuilderCache`
implementation, and in some places that matters.

## Implementation details

- Made in .NET 6 only for simplicity. We _could_ expose it earlier, but
I wanted this for the updated aspnetcore integration, so .NET6+ only for
now is good enough
- It's not without risks in its usage, so we have to be careful about
things like passing it around (i.e. avoid doing that completely, for
safety)
- Uses an array pool backed implementation (again, built into .NET 6)

## Test coverage

Imported the unit tests from the runtime too

## Other details

https://datadoghq.atlassian.net/browse/LANGPLAT-842

Part of a stack

- #7962 👈
- #7963
- #7964
- #7966
- #7965
Base automatically changed from andrew/single-span/1-valuestringbuilder to master December 23, 2025 10:21
@andrewlock andrewlock force-pushed the andrew/single-span/2-minor-fixes branch from bc14e1f to accaca2 Compare December 23, 2025 12:17
@pr-commenter
Copy link

pr-commenter bot commented Dec 23, 2025

Benchmarks

Benchmark execution time: 2025-12-23 15:49:05

Comparing candidate commit 383da45 in PR branch andrew/single-span/2-minor-fixes with baseline commit 053addc in branch master.

Found 11 performance improvements and 4 performance regressions! Performance is the same for 153 metrics, 18 unstable metrics.

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleMoreComplexBody net6.0

  • 🟥 execution_time [+16.919ms; +22.644ms] or [+8.533%; +11.420%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.AllCycleSimpleBody net472

  • 🟩 throughput [+164945.401op/s; +169100.220op/s] or [+20.206%; +20.715%]

scenario:Benchmarks.Trace.Asm.AppSecBodyBenchmark.ObjectExtractorSimpleBody netcoreapp3.1

  • 🟩 execution_time [-24.833ms; -18.975ms] or [-11.541%; -8.818%]

scenario:Benchmarks.Trace.Asm.AppSecEncoderBenchmark.EncodeLegacyArgs netcoreapp3.1

  • 🟩 execution_time [-22.625ms; -22.303ms] or [-11.128%; -10.970%]

scenario:Benchmarks.Trace.AspNetCoreBenchmark.SendRequest net6.0

  • 🟩 execution_time [-100.294ms; -90.924ms] or [-51.182%; -46.400%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net472

  • 🟩 execution_time [-28.021ms; -17.798ms] or [-12.698%; -8.065%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSlice netcoreapp3.1

  • 🟥 throughput [-155.208op/s; -110.443op/s] or [-31.364%; -22.318%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool net6.0

  • 🟩 throughput [+48.651op/s; +51.722op/s] or [+5.216%; +5.545%]

scenario:Benchmarks.Trace.CharSliceBenchmark.OptimizedCharSliceWithPool netcoreapp3.1

  • 🟩 throughput [+26.266op/s; +28.926op/s] or [+5.170%; +5.694%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearch net472

  • 🟩 throughput [+22637.649op/s; +24587.105op/s] or [+8.156%; +8.858%]

scenario:Benchmarks.Trace.Log4netBenchmark.EnrichedLog netcoreapp3.1

  • 🟩 execution_time [-43.847ms; -42.057ms] or [-21.907%; -21.012%]

scenario:Benchmarks.Trace.RedisBenchmark.SendReceive netcoreapp3.1

  • 🟥 throughput [-32170.027op/s; -23270.685op/s] or [-7.956%; -5.755%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishSpan net472

  • 🟩 throughput [+58118.892op/s; +59958.998op/s] or [+5.460%; +5.633%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishTwoScopes net472

  • 🟩 throughput [+24896.048op/s; +28485.195op/s] or [+5.703%; +6.525%]

scenario:Benchmarks.Trace.SpanBenchmark.StartFinishTwoScopes net6.0

  • 🟥 execution_time [+15.469ms; +19.739ms] or [+7.706%; +9.833%]

@datadog-official

This comment has been minimized.

@andrewlock andrewlock force-pushed the andrew/single-span/2-minor-fixes branch from accaca2 to 383da45 Compare December 23, 2025 14:50
// Iast hasn't yet, but doing it now is fine
// span origins is _not_ initialized yet, and we can't guarantee it will be
// so just be lazy instead
observers.Add(new AspNetCoreDiagnosticObserver(Tracer.Instance, Security.Instance, Iast.Iast.Instance, spanCodeOrigin: null));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This forces the creation of IAST and security instances even if not enabled. On the other hand, we are calling Security.Instance and Iast.Instance in many other places, so, in practical terms, we are going to create the instances anyway. Actually, we need to call Instance to read the IAST and ASM settings. Just wondering if at some point we could separate the security settings and access them without actually creating a (not needed mostly) instance of Security or IAST. We are creating instances just to read the settings. Still, a sensitive change (we need to be careful with RC, for instance)

Anyway, definitely, totally out of the scope of this PR. The performance optimization here is sound given the current architecture.

Copy link
Member Author

Choose a reason for hiding this comment

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

This forces the creation of IAST and security instances even if not enabled

So these are actually already created automatically 😅 e.g. we explicitly do _ = Security.Instance; just above.

Actually, we need to call Instance to read the IAST and ASM settings. Just wondering if at some point we could separate the security settings and access them without actually creating a (not needed mostly) instance of Security or IAST.

💯 This is something I'd definitely like to do, and I think is worthwhile, but for the exact reason you state "we need to be careful with RC, for instance", I suspect we will generally need to do a certain amount of initialization no matter what, so I don't think it's a massive issue. And like you say, outside of the scope for this PR, but definitely something for us to think about! 🙂

Copy link
Collaborator

@NachoEchevarria NachoEchevarria left a comment

Choose a reason for hiding this comment

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

Nice job! Thanks!

@andrewlock andrewlock merged commit ea3810b into master Dec 24, 2025
154 checks passed
@andrewlock andrewlock deleted the andrew/single-span/2-minor-fixes branch December 24, 2025 13:25
@github-actions github-actions bot added this to the vNext-v3 milestone Dec 24, 2025
andrewlock added a commit that referenced this pull request Jan 13, 2026
## Summary of changes

Adds a new `DD_TRACE_SINGLE_SPAN_ASPNETCORE_ENABLED` in which we omit
the MVC span

## Reason for change

- Addresses #4093 and partially #1147
- Omitting the MVC span has perf and some usability benefits in some
cases (See issues above). It _does_ mean a certain amount of less
information, particularly in pipeline re-execution scenarios

## Implementation details

- Add a new, disabled by default,
`DD_TRACE_SINGLE_SPAN_ASPNETCORE_ENABLED` for opting in to the new
behaviour.
- .NET 6+ only for various reasons
- Cloned the original observer, deleted everything related to the MVC
span, and then optimized further based on .NET 6+ and other improvements
we can make

## Test coverage

- Added in-process "integration" tests for the observer (more like unit
tests), which mirror the exisitng.
- Added "full" sample-based integration tests for basically all
scenarios we currently test (IIS in process/out of process / MVC /
minimal APIs / proxy spans etc)
- Added snapshots by copying existing, and then updating, so if you look
at the final commit, you can see the diff, which is basically just
"delete the child span"

## Other details

https://datadoghq.atlassian.net/browse/LANGPLAT-842

Part of a stack



- #7962
- #7963
- #7964 👈
- #7966
- #7965

I experimented with some other perf improvements, but they didn't
provide a significant improvement, so I reverted:
- Caching the resource name by saving it into the `IEndpointMetadata`
collection. The work to do that was too much, and you _can't_ cache in
some cases anyway so not worth it
- Splitting `WebTags` out to remove the IP headers unless we need them.
They're disabled by default, so it just increase the size of the tags
object, but wasn't a big enough improvement to make the complexity worth
it.
- Tried pooling the `RequestTrackingFeature` objects. This one _may_
actually be worth exploring again later, but I didn't see any
improvement in my testing, as they're just not very big and only last 1
request.

---------

Co-authored-by: Lucas Pimentel <lucas.pimentel@datadoghq.com>
andrewlock added a commit that referenced this pull request Jan 13, 2026
## Summary of changes

Creates an optimized version of `AspNetCoreResourceNameHelper` for use
by the single-span Diagnostic observer.

## Reason for change

We have the opportunity for a variety of optimizations thanks to not
creating the MVC span, and the fact that we're .NET 6+. We could
potentially include some or all of these optimizations in the existing
observer path, but for safety/simplicity I'd rather keep it isolated for
now, and we can look at expanding it more broadly later

## Implementation details

- Use the `ValueStringBuilder` to build a resource name
- Don't pre-grab MVC route values, wait to see if we have route params
from them first
- Write directly into the builder as `ToLowerInvariant`, instead of
doing an extra ToLower() at the end

## Test coverage

Added unit tests that use the existing data for the other
implementations, to confirm it's identical. It's also covered indirectly
by the integration tests

## Other details

https://datadoghq.atlassian.net/browse/LANGPLAT-842

Part of a stack


- #7962
- #7963
- #7964
- #7966 👈
- #7965
andrewlock added a commit that referenced this pull request Jan 13, 2026
…7965)

## Summary of changes

Adds a micro and macro benchmark for the new observer

## Reason for change

Want to verify that it has performance benefits

## Implementation details

- Added new scenario for macro
- Duplicated existing scenario for micro and run. I would like to have
had these as the same methods, but the "global" nature of diagnostic
observers makes this difficult I think.

## Test coverage

I'll manually trigger a run of the macro

## Other details

https://datadoghq.atlassian.net/browse/LANGPLAT-842

Part of a stack



- #7962
- #7963
- #7964
- #7966
- #7965 👈
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:automatic-instrumentation Automatic instrumentation managed C# code (Datadog.Trace.ClrProfiler.Managed) area:tracer The core tracer library (Datadog.Trace, does not include OpenTracing, native code, or integrations) type:performance Performance, speed, latency, resource usage (CPU, memory)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants