Skip to content

Add _dd.p.ksr propagated tag for Knuth sampling rate#8287

Merged
bm1549 merged 8 commits intomasterfrom
brian.marks/add-ksr-tag
Mar 25, 2026
Merged

Add _dd.p.ksr propagated tag for Knuth sampling rate#8287
bm1549 merged 8 commits intomasterfrom
brian.marks/add-ksr-tag

Conversation

@bm1549
Copy link
Copy Markdown
Contributor

@bm1549 bm1549 commented Mar 11, 2026

Summary of changes

Add _dd.p.ksr (Knuth Sampling Rate) propagated tag to spans when sampling is applied via agent rates or trace sampling rules, per the Transmit Knuth Sampling Rate to Backend RFC.

Reason for change

The backend needs to know the exact sampling rate applied by the tracer to correctly compute effective rates during resampling (e.g., tracer 0.5 × backend 0.5 = effective 0.25). This tag enables that by propagating the rate via x-datadog-tags and W3C tracestate.

Implementation details

  • Set _dd.p.ksr in TraceContext.SetSamplingPriority() for AgentRate, LocalTraceSamplingRule, RemoteAdaptiveSamplingRule, and RemoteUserSamplingRule mechanisms
  • Use TryAddTag to preserve the original rate (consistent with AppliedSamplingRate ??= rate semantics)
  • Format with "0.######" (up to 6 decimal digits, no trailing zeros, no scientific notation) per RFC spec
  • Added .IsOptional("_dd.p.ksr") to SpanTagAssertion.cs so integration test tag validators accept the new tag

Test coverage

  • Unit tests in TraceContextTests_KnuthSamplingRate.cs:
    • KSR set for agent rate sampling
    • KSR set for trace sampling rules (local, remote adaptive, remote user)
    • KSR NOT set for manual, AppSec, rate limiter, or single span mechanisms
    • KSR preserved on subsequent sampling calls (TryAddTag semantics)
    • Formatting with up to 6 decimal digits (boundary values including small rates like 0.00001)
  • System tests in system-tests #6466

Other details

Related PRs across tracers:

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The Knuth sampling rate tag (_dd.p.ksr) was using TryAddTag which has
first-write semantics, preserving any existing upstream value. This is
inconsistent with all other tracers which unconditionally overwrite ksr
with the locally-computed value. Switch to SetTag (replaceIfExists: true)
so the local sampling rate always takes precedence.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@pr-commenter
Copy link
Copy Markdown

pr-commenter bot commented Mar 11, 2026

Benchmarks

Benchmark execution time: 2026-03-24 21:30:40

Comparing candidate commit b760abc in PR branch brian.marks/add-ksr-tag with baseline commit 1bb5b79 in branch master.

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

Explanation

This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:

  • 🟩 = significantly better candidate vs. baseline
  • 🟥 = significantly worse candidate vs. baseline

We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.

If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.

Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.

More details about the CI and significant changes

You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.

CIs of the difference of means are often centered around 0%, because often changes are not that big:

---------------------------------(------|---^--------)-------------------------------->
                              -0.6%    0%  0.3%     +1.2%
                                 |          |        |
         lower bound of the CI --'          |        |
sample mean (center of the CI) -------------'        |
         upper bound of the CI ----------------------'

As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).

For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:

----------------------------------------|---------|---(---------^---------)---------->
                                       0%        1%  1.3%      2.2%      3.1%
                                                  |   |         |         |
       significant impact threshold --------------'   |         |         |
                      lower bound of CI --------------'         |         |
       sample mean (center of the CI) --------------------------'         |
                      upper bound of CI ----------------------------------'

scenario:Benchmarks.Trace.AgentWriterBenchmark.WriteAndFlushEnrichedTraces netcoreapp3.1

  • 🟩 execution_time [-90.381ms; -90.144ms] or [-44.746%; -44.628%]

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

  • 🟥 execution_time [+14.746ms; +19.668ms] or [+7.287%; +9.719%]

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

  • 🟥 execution_time [+14.596ms; +20.655ms] or [+7.404%; +10.478%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net472

  • 🟩 throughput [+126.523op/s; +152.196op/s] or [+12.205%; +14.681%]

scenario:Benchmarks.Trace.CIVisibilityProtocolWriterBenchmark.WriteAndFlushEnrichedTraces net6.0

  • 🟥 throughput [-203.082op/s; -119.828op/s] or [-12.668%; -7.475%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearch net472

  • 🟥 throughput [-23355.272op/s; -20969.466op/s] or [-7.211%; -6.475%]

scenario:Benchmarks.Trace.ElasticsearchBenchmark.CallElasticsearch net6.0

  • 🟥 throughput [-39022.859op/s; -33159.269op/s] or [-6.008%; -5.106%]

scenario:Benchmarks.Trace.HttpClientBenchmark.SendAsync net6.0

  • 🟥 throughput [-10462.161op/s; -8812.468op/s] or [-6.782%; -5.712%]

scenario:Benchmarks.Trace.ILoggerBenchmark.EnrichedLog net6.0

  • 🟥 execution_time [+14.924ms; +15.528ms] or [+7.437%; +7.738%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatAspectBenchmark net6.0

  • 🟥 allocated_mem [+18.311KB; +18.353KB] or [+7.114%; +7.130%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatAspectBenchmark netcoreapp3.1

  • 🟥 allocated_mem [+19.491KB; +19.525KB] or [+7.615%; +7.629%]

scenario:Benchmarks.Trace.Iast.StringAspectsBenchmark.StringConcatBenchmark net6.0

  • 🟥 throughput [-4619.180op/s; -2742.101op/s] or [-19.368%; -11.497%]

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

  • 🟩 execution_time [-18.251ms; -14.674ms] or [-9.128%; -7.339%]

scenario:Benchmarks.Trace.SingleSpanAspNetCoreBenchmark.SingleSpanAspNetCore net6.0

  • 🟥 execution_time [+109.805ms; +111.589ms] or [+125.794%; +127.837%]

scenario:Benchmarks.Trace.SingleSpanAspNetCoreBenchmark.SingleSpanAspNetCore netcoreapp3.1

  • 🟥 throughput [-15757301.929op/s; -14914304.792op/s] or [-6.528%; -6.179%]

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

  • 🟥 execution_time [+20.034ms; +24.122ms] or [+10.150%; +12.220%]

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

  • 🟩 execution_time [-21.580ms; -17.031ms] or [-9.864%; -7.785%]

@dd-trace-dotnet-ci-bot
Copy link
Copy Markdown

dd-trace-dotnet-ci-bot bot commented Mar 11, 2026

Execution-Time Benchmarks Report ⏱️

Execution-time results for samples comparing This PR (8287) 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.51 ± (68.58 - 68.93) ms68.42 ± (68.48 - 68.74) ms-0.1%
.NET Framework 4.8 - Bailout
duration72.51 ± (72.50 - 72.78) ms72.53 ± (72.35 - 72.64) ms+0.0%✅⬆️
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1043.80 ± (1045.05 - 1052.04) ms1042.82 ± (1045.63 - 1053.26) ms-0.1%
.NET Core 3.1 - Baseline
process.internal_duration_ms21.83 ± (21.80 - 21.86) ms21.80 ± (21.77 - 21.83) ms-0.1%
process.time_to_main_ms79.65 ± (79.47 - 79.83) ms79.83 ± (79.64 - 80.02) ms+0.2%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.90 ± (10.90 - 10.90) MB10.91 ± (10.91 - 10.92) MB+0.1%✅⬆️
runtime.dotnet.threads.count12 ± (12 - 12)12 ± (12 - 12)+0.0%
.NET Core 3.1 - Bailout
process.internal_duration_ms21.70 ± (21.68 - 21.72) ms21.75 ± (21.72 - 21.77) ms+0.2%✅⬆️
process.time_to_main_ms80.86 ± (80.72 - 80.99) ms80.43 ± (80.30 - 80.55) ms-0.5%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.93 ± (10.93 - 10.94) MB10.95 ± (10.95 - 10.96) MB+0.2%✅⬆️
runtime.dotnet.threads.count13 ± (13 - 13)13 ± (13 - 13)+0.0%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms217.51 ± (216.15 - 218.87) ms217.48 ± (216.17 - 218.80) ms-0.0%
process.time_to_main_ms518.24 ± (517.03 - 519.44) ms518.98 ± (517.95 - 520.01) ms+0.1%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed48.28 ± (48.25 - 48.31) MB48.26 ± (48.23 - 48.29) MB-0.0%
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)-0.2%
.NET 6 - Baseline
process.internal_duration_ms20.59 ± (20.57 - 20.62) ms20.51 ± (20.48 - 20.54) ms-0.4%
process.time_to_main_ms69.06 ± (68.92 - 69.21) ms68.91 ± (68.80 - 69.02) ms-0.2%
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.62 ± (10.62 - 10.63) MB10.61 ± (10.61 - 10.62) MB-0.1%
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 6 - Bailout
process.internal_duration_ms20.43 ± (20.41 - 20.46) ms20.45 ± (20.42 - 20.47) ms+0.1%✅⬆️
process.time_to_main_ms69.61 ± (69.49 - 69.73) ms70.13 ± (70.01 - 70.24) ms+0.7%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed10.67 ± (10.67 - 10.68) MB10.73 ± (10.73 - 10.73) MB+0.5%✅⬆️
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms206.44 ± (205.07 - 207.81) ms209.97 ± (208.92 - 211.03) ms+1.7%✅⬆️
process.time_to_main_ms518.43 ± (517.07 - 519.80) ms521.97 ± (520.78 - 523.17) ms+0.7%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed50.07 ± (50.03 - 50.11) MB50.15 ± (50.12 - 50.18) MB+0.2%✅⬆️
runtime.dotnet.threads.count29 ± (29 - 29)29 ± (29 - 29)+0.5%✅⬆️
.NET 8 - Baseline
process.internal_duration_ms18.77 ± (18.74 - 18.79) ms18.80 ± (18.78 - 18.83) ms+0.2%✅⬆️
process.time_to_main_ms68.20 ± (68.06 - 68.34) ms68.20 ± (68.09 - 68.32) ms+0.0%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.68 ± (7.67 - 7.68) MB7.69 ± (7.68 - 7.69) MB+0.1%✅⬆️
runtime.dotnet.threads.count10 ± (10 - 10)10 ± (10 - 10)+0.0%
.NET 8 - Bailout
process.internal_duration_ms18.78 ± (18.75 - 18.82) ms18.87 ± (18.84 - 18.90) ms+0.5%✅⬆️
process.time_to_main_ms69.16 ± (69.06 - 69.27) ms69.59 ± (69.48 - 69.70) ms+0.6%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed7.74 ± (7.74 - 7.75) MB7.72 ± (7.71 - 7.73) MB-0.3%
runtime.dotnet.threads.count11 ± (11 - 11)11 ± (11 - 11)+0.0%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms153.90 ± (153.15 - 154.66) ms153.70 ± (153.12 - 154.28) ms-0.1%
process.time_to_main_ms475.25 ± (474.46 - 476.05) ms482.02 ± (480.63 - 483.41) ms+1.4%✅⬆️
runtime.dotnet.exceptions.count0 ± (0 - 0)0 ± (0 - 0)+0.0%
runtime.dotnet.mem.committed36.93 ± (36.91 - 36.95) MB36.96 ± (36.94 - 36.99) MB+0.1%✅⬆️
runtime.dotnet.threads.count28 ± (28 - 28)28 ± (28 - 28)+0.1%✅⬆️

HttpMessageHandler

Metric Master (Mean ± 95% CI) Current (Mean ± 95% CI) Change Status
.NET Framework 4.8 - Baseline
duration214.49 ± (214.32 - 215.64) ms208.57 ± (208.19 - 209.07) ms-2.8%
.NET Framework 4.8 - Bailout
duration219.00 ± (218.88 - 220.22) ms212.81 ± (212.93 - 213.94) ms-2.8%
.NET Framework 4.8 - CallTarget+Inlining+NGEN
duration1250.89 ± (1247.88 - 1255.72) ms1224.16 ± (1223.59 - 1232.26) ms-2.1%
.NET Core 3.1 - Baseline
process.internal_duration_ms210.96 ± (210.39 - 211.54) ms204.66 ± (204.13 - 205.18) ms-3.0%
process.time_to_main_ms90.46 ± (90.19 - 90.72) ms87.99 ± (87.77 - 88.21) ms-2.7%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed15.87 ± (15.85 - 15.88) MB15.88 ± (15.86 - 15.90) MB+0.1%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (20 - 20)-0.2%
.NET Core 3.1 - Bailout
process.internal_duration_ms211.03 ± (210.38 - 211.68) ms205.12 ± (204.60 - 205.63) ms-2.8%
process.time_to_main_ms91.88 ± (91.64 - 92.11) ms89.78 ± (89.55 - 90.02) ms-2.3%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed15.91 ± (15.90 - 15.93) MB15.95 ± (15.93 - 15.97) MB+0.2%✅⬆️
runtime.dotnet.threads.count21 ± (21 - 21)21 ± (21 - 21)-0.2%
.NET Core 3.1 - CallTarget+Inlining+NGEN
process.internal_duration_ms421.13 ± (419.76 - 422.50) ms412.76 ± (411.59 - 413.94) ms-2.0%
process.time_to_main_ms566.51 ± (565.04 - 567.97) ms555.84 ± (554.49 - 557.19) ms-1.9%
runtime.dotnet.exceptions.count3 ± (3 - 3)3 ± (3 - 3)+0.0%
runtime.dotnet.mem.committed59.13 ± (59.09 - 59.17) MB59.14 ± (59.10 - 59.19) MB+0.0%✅⬆️
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)-0.2%
.NET 6 - Baseline
process.internal_duration_ms217.27 ± (216.65 - 217.88) ms210.91 ± (210.32 - 211.51) ms-2.9%
process.time_to_main_ms78.72 ± (78.54 - 78.91) ms76.51 ± (76.31 - 76.71) ms-2.8%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.15 ± (16.14 - 16.17) MB16.23 ± (16.21 - 16.24) MB+0.4%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (19 - 20)-0.2%
.NET 6 - Bailout
process.internal_duration_ms215.85 ± (215.26 - 216.43) ms209.13 ± (208.58 - 209.67) ms-3.1%
process.time_to_main_ms79.80 ± (79.62 - 79.98) ms77.61 ± (77.41 - 77.81) ms-2.7%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed16.17 ± (16.15 - 16.18) MB16.19 ± (16.18 - 16.21) MB+0.2%✅⬆️
runtime.dotnet.threads.count21 ± (20 - 21)20 ± (20 - 20)-0.9%
.NET 6 - CallTarget+Inlining+NGEN
process.internal_duration_ms448.96 ± (447.26 - 450.66) ms439.32 ± (437.66 - 440.98) ms-2.1%
process.time_to_main_ms572.15 ± (571.03 - 573.28) ms562.96 ± (561.65 - 564.27) ms-1.6%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed60.90 ± (60.85 - 60.96) MB61.07 ± (61.03 - 61.11) MB+0.3%✅⬆️
runtime.dotnet.threads.count31 ± (31 - 31)31 ± (31 - 31)-0.8%
.NET 8 - Baseline
process.internal_duration_ms213.69 ± (213.07 - 214.32) ms208.88 ± (208.27 - 209.48) ms-2.3%
process.time_to_main_ms77.45 ± (77.24 - 77.65) ms76.24 ± (76.02 - 76.45) ms-1.6%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.47 ± (11.46 - 11.49) MB11.59 ± (11.57 - 11.60) MB+1.0%✅⬆️
runtime.dotnet.threads.count19 ± (19 - 19)19 ± (19 - 19)-0.8%
.NET 8 - Bailout
process.internal_duration_ms213.83 ± (213.10 - 214.56) ms206.74 ± (206.15 - 207.32) ms-3.3%
process.time_to_main_ms78.59 ± (78.40 - 78.79) ms76.74 ± (76.58 - 76.90) ms-2.4%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed11.51 ± (11.49 - 11.53) MB11.60 ± (11.59 - 11.62) MB+0.8%✅⬆️
runtime.dotnet.threads.count20 ± (20 - 20)20 ± (20 - 20)-0.8%
.NET 8 - CallTarget+Inlining+NGEN
process.internal_duration_ms459.63 ± (454.30 - 464.97) ms389.27 ± (382.30 - 396.24) ms-15.3%
process.time_to_main_ms527.26 ± (526.20 - 528.31) ms517.54 ± (516.61 - 518.47) ms-1.8%
runtime.dotnet.exceptions.count4 ± (4 - 4)4 ± (4 - 4)+0.0%
runtime.dotnet.mem.committed50.61 ± (50.51 - 50.71) MB49.51 ± (49.36 - 49.66) MB-2.2%
runtime.dotnet.threads.count30 ± (30 - 30)30 ± (30 - 30)-0.3%
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 (8287) - mean (69ms)  : 67, 71
    master - mean (69ms)  : 66, 71

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

    section CallTarget+Inlining+NGEN
    This PR (8287) - mean (1,049ms)  : 995, 1104
    master - mean (1,049ms)  : 999, 1098

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 (8287) - mean (107ms)  : 104, 110
    master - mean (107ms)  : 104, 111

    section Bailout
    This PR (8287) - mean (108ms)  : 106, 109
    master - mean (108ms)  : 106, 110

    section CallTarget+Inlining+NGEN
    This PR (8287) - mean (773ms)  : 754, 792
    master - mean (772ms)  : 757, 788

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

    section Bailout
    This PR (8287) - mean (96ms)  : 94, 98
    master - mean (95ms)  : 94, 97

    section CallTarget+Inlining+NGEN
    This PR (8287) - mean (769ms)  : 747, 791
    master - mean (758ms)  : 735, 781

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

    section Bailout
    This PR (8287) - mean (95ms)  : 93, 97
    master - mean (95ms)  : 93, 96

    section CallTarget+Inlining+NGEN
    This PR (8287) - mean (669ms)  : 642, 695
    master - mean (661ms)  : 639, 684

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 (8287) - mean (209ms)  : 203, 214
    master - mean (215ms)  : 205, 224

    section Bailout
    This PR (8287) - mean (213ms)  : 206, 221
    master - mean (220ms)  : 209, 230

    section CallTarget+Inlining+NGEN
    This PR (8287) - mean (1,228ms)  : 1165, 1290
    master - mean (1,252ms)  : 1193, 1311

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 (8287) - mean (302ms)  : 291, 313
    master - mean (311ms)  : 299, 324

    section Bailout
    This PR (8287) - mean (305ms)  : 292, 317
    master - mean (312ms)  : 300, 325

    section CallTarget+Inlining+NGEN
    This PR (8287) - mean (1,006ms)  : 980, 1032
    master - mean (1,029ms)  : 998, 1060

Loading
HttpMessageHandler (.NET 6)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 6)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8287) - mean (296ms)  : 284, 309
    master - mean (305ms)  : 294, 317

    section Bailout
    This PR (8287) - mean (295ms)  : 286, 304
    master - mean (305ms)  : 294, 316

    section CallTarget+Inlining+NGEN
    This PR (8287) - mean (1,036ms)  : 1007, 1066
    master - mean (1,057ms)  : 1018, 1096

Loading
HttpMessageHandler (.NET 8)
gantt
    title Execution time (ms) HttpMessageHandler (.NET 8)
    dateFormat  x
    axisFormat %Q
    todayMarker off
    section Baseline
    This PR (8287) - mean (295ms)  : 286, 305
    master - mean (302ms)  : 291, 313

    section Bailout
    This PR (8287) - mean (294ms)  : 286, 303
    master - mean (303ms)  : 290, 316

    section CallTarget+Inlining+NGEN
    This PR (8287) - mean (942ms)  : 845, 1039
    master - mean (1,021ms)  : 920, 1122

Loading

bm1549 and others added 2 commits March 10, 2026 23:28
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The new _dd.p.ksr propagated tag added in the ksr PR was not listed as
an optional tag in DefaultTagAssertions, causing ValidateIntegrationSpans
to fail with "Expected to have no remaining tags" in AspNetMvc4Tests and
other Windows IIS integration tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bm1549 bm1549 added the docker_image_artifacts Use to label PRs for which you would need a Docker Image created for. label Mar 12, 2026
@bm1549 bm1549 marked this pull request as ready for review March 13, 2026 18:04
@bm1549 bm1549 requested a review from a team as a code owner March 13, 2026 18:04
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 16eaa8a188

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

bm1549 added a commit to DataDog/dd-trace-rs that referenced this pull request Mar 19, 2026
# What does this PR do?

Adds `_dd.p.ksr` (Knuth Sampling Rate) as a propagated tag set when
agent-based or rule-based sampling decisions are made. The tag is stored
in span `meta` (string type) with up to 6 significant digits and no
trailing zeros.

`format_sampling_rate` now returns `Option<String>` and guards against
invalid inputs (negative, >1.0, NaN, infinity), returning `None` instead
of producing garbage output.

# Motivation

To enable consistent sampling across tracers and backend retention
filters, the backend needs to know the sampling rate applied by the
tracer. Without transmitting the tracer's rate via `_dd.p.ksr`, backend
resampling cannot correctly compute effective rates in multi-stage
sampling scenarios.

See RFC: "Transmit Knuth sampling rate to backend"

# Additional Notes

Key files changed:
- `datadog-opentelemetry/src/core/constants.rs` — Added
`SAMPLING_KNUTH_RATE_TAG_KEY` constant
- `datadog-opentelemetry/src/sampling/datadog_sampler.rs` — Added
`format_sampling_rate()` helper (returns `Option<String>`, defensive
against invalid rates) and set ksr in agent/rule sampling paths
- Updated 2 snapshot JSON files

Related PRs across tracers:
- Java: DataDog/dd-trace-java#10802
- .NET: DataDog/dd-trace-dotnet#8287
- Ruby: DataDog/dd-trace-rb#5436
- Node.js: DataDog/dd-trace-js#7741
- PHP: DataDog/dd-trace-php#3701
- C++: DataDog/dd-trace-cpp#288
- System tests: DataDog/system-tests#6466

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
bm1549 added a commit to DataDog/dd-trace-go that referenced this pull request Mar 19, 2026
### What does this PR do?

Fixes `_dd.p.ksr` (Knuth Sampling Rate) to only be set on spans when the
agent has provided sampling rates via `readRatesJSON()`. Previously, ksr
was unconditionally set in `prioritySampler.apply()`, including when the
rate was the initial client-side default (1.0) before any agent response
arrived.

Also refactors `prioritySampler` to consolidate lock acquisitions:
extracts `getRateLocked()` so `apply()` acquires `ps.mu.RLock` only once
to read both the rate and `agentRatesLoaded`.

### Motivation

Cross-language consistency: Python, Java, PHP, and other tracers only
set ksr when actual agent rates or sampling rules are applied, not for
the default fallback. This aligns Go with that behavior.

See RFC: "Transmit Knuth sampling rate to backend"

### Additional Notes

- Added `agentRatesLoaded` bool field to `prioritySampler`, set to
`true` in `readRatesJSON()`
- `apply()` now gates ksr behind `agentRatesLoaded` check
- Extracted `getRateLocked()` to avoid double lock acquisition in
`apply()`
- Rule-based sampling path (`applyTraceRuleSampling` in span.go)
unchanged — correctly always sets ksr
- Tests added: `ksr-not-set-without-agent-rates` and
`ksr-set-after-agent-rates-received`

Related PRs across tracers:
- Java: DataDog/dd-trace-java#10802
- .NET: DataDog/dd-trace-dotnet#8287
- Ruby: DataDog/dd-trace-rb#5436
- Node.js: DataDog/dd-trace-js#7741
- PHP: DataDog/dd-trace-php#3701
- Rust: DataDog/dd-trace-rs#180
- C++: DataDog/dd-trace-cpp#288
- System tests: DataDog/system-tests#6466

### Reviewer's Checklist

- [x] Changed code has unit tests for its functionality at or near 100%
coverage.
- [x] [System-Tests](https://github.com/DataDog/system-tests/) covering
this feature have been added and enabled with the va.b.c-dev version
tag.
- [ ] There is a benchmark for any new code, or changes to existing
code.
- [x] If this interacts with the agent in a new way, a system test has
been added.
- [x] New code is free of linting errors. You can check this by running
`make lint` locally.
- [x] New code doesn't break existing tests. You can check this by
running `make test` locally.
- [ ] Add an appropriate team label so this PR gets put in the right
place for the release notes.
- [ ] All generated files are up to date. You can check this by running
`make generate` locally.
- [ ] Non-trivial go.mod changes, e.g. adding new modules, are reviewed
by @DataDog/dd-trace-go-guild. Make sure all nested modules are up to
date by running `make fix-modules` locally.

Unsure? Have a question? Request a review!

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: Dario Castañé <dario.castane@datadoghq.com>
Co-authored-by: Mikayla Toffler <46911781+mtoffl01@users.noreply.github.com>
Address Codex review feedback: change SetTag to TryAddTag for the
_dd.p.ksr propagated tag so it follows the same first-write-wins
semantics as AppliedSamplingRate and SamplingMechanism. This prevents
inconsistency where _dd.p.ksr could reflect a newer rate while
_dd.agent_psr/_dd.rule_psr still come from the original rate.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Change format specifier from "G6" (6 significant digits, allows
scientific notation) to "0.######" (up to 6 decimal digits) per RFC.
This fixes 0.00001 being formatted as "1E-05" instead of "0.00001".

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bm1549 bm1549 enabled auto-merge (squash) March 23, 2026 23:52
or Sampling.SamplingMechanism.RemoteUserSamplingRule)
{
// format with up to 6 decimal digits, no trailing zeros (per RFC)
Tags.TryAddTag(Trace.Tags.Propagated.KnuthSamplingRate, samplingRate.ToString("0.######", CultureInfo.InvariantCulture));
Copy link
Copy Markdown
Member

@lucaspimentel lucaspimentel Mar 24, 2026

Choose a reason for hiding this comment

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

I wonder if it's worth hard-coding the string for common sampling rates like "1" to avoid these heap allocations for every trace. Or caching the string in the rule itself (which allows caching the string for different rates, for example if one rules has "1" and another has "0.5").

Maybe something we can do in a follow-up PR. Thoughts, @andrewlock?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, nice idea 🤔 Although I wonder if we should even be adding this here, and shouldn't be just adding it at serialization time. We should avoid adding tags like this wherever possible 😬

@bm1549 bm1549 merged commit f6c4461 into master Mar 25, 2026
140 of 141 checks passed
@bm1549 bm1549 deleted the brian.marks/add-ksr-tag branch March 25, 2026 14:38
@github-actions github-actions bot added this to the vNext-v3 milestone Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI Generated Largely based on code generated by an AI or LLM. This label is the same across all dd-trace-* repos docker_image_artifacts Use to label PRs for which you would need a Docker Image created for.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants