-
Notifications
You must be signed in to change notification settings - Fork 330
Add _dd.p.ksr propagated tag for Knuth sampling rate #10802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gh-worker-dd-mergequeue-cf854d
merged 16 commits into
master
from
brian.marks/add-ksr-tag
Mar 20, 2026
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
684a837
Add _dd.p.ksr propagated tag for Knuth sampling rate
bm1549 636f028
fix: remove unused imports and update OT tests for _dd.p.ksr propagation
bm1549 457c471
fix: add t.ksr to expected tracestate in OT compatibility tests
bm1549 b006773
Fix forbidden API usage in formatKnuthSamplingRate
bm1549 1ce9e46
fix: correct _dd.p.ksr ordering in OT x-datadog-tags expectations
bm1549 7670947
fix: add _dd.p.ksr to expected meta maps in DDAgentApiTest
bm1549 b46e9de
fix: update propagation tests to include _dd.p.ksr in expected headers
bm1549 13b7d5e
fix: add ksr and tid handling to OpenTracing31Test inject extract
bm1549 e413676
Address PR feedback: fix locale bug, move ksr formatting to Propagati…
bm1549 f1e92a0
ci: retry - transient JBoss 503 failure in smoke tests
bm1549 81f00be
Merge branch 'master' into brian.marks/add-ksr-tag
bm1549 e0ce92a
Merge branch 'master' into brian.marks/add-ksr-tag
bm1549 97d7e52
Address PR feedback: rename datadogTags to expectedDataTags, store ks…
bm1549 6776ab3
Optimize formatKnuthSamplingRate: replace String.format with char-arr…
bm1549 e73c492
Eliminate String.format entirely from formatKnuthSamplingRate
bm1549 bcc0957
Optimize KSR update path with static rate→TagValue cache
bm1549 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...e/src/jmh/java/datadog/trace/core/propagation/ptags/KnuthSamplingRateFormatBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package datadog.trace.core.propagation.ptags; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
|
|
||
| import datadog.trace.core.propagation.PropagationTags; | ||
| import java.util.Locale; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Threads; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.infra.Blackhole; | ||
|
|
||
| /** | ||
| * Benchmarks for formatting the Knuth sampling rate (_dd.p.ksr tag value). | ||
| * | ||
| * <p>The format requirement is %.6g semantics: 6 significant figures, no trailing zeros, using | ||
| * fixed notation for values in [1e-4, 1] and scientific notation for smaller values. | ||
| * | ||
| * <p>Run with: | ||
| * | ||
| * <pre> | ||
| * ./gradlew :dd-trace-core:jmhJar | ||
| * java -jar dd-trace-core/build/libs/dd-trace-core-*-jmh.jar KnuthSamplingRateFormatBenchmark \ | ||
| * -t 8 -prof gc | ||
| * </pre> | ||
| * | ||
| * <p>Use {@code -t 8} (or higher) to surface GC pressure from multi-threaded allocation. Use {@code | ||
| * -prof gc} to see alloc rate (bytes/op) per benchmark — that's the primary signal for whether the | ||
| * hot path is allocation-free. | ||
| */ | ||
| @State(Scope.Thread) | ||
| @Warmup(iterations = 3, time = 10, timeUnit = SECONDS) | ||
| @Measurement(iterations = 5, time = 10, timeUnit = SECONDS) | ||
| @BenchmarkMode(Mode.Throughput) | ||
| @OutputTimeUnit(SECONDS) | ||
| @Threads(8) | ||
| @Fork(value = 1) | ||
| public class KnuthSamplingRateFormatBenchmark { | ||
|
|
||
| /** | ||
| * Representative sampling rates. Most real-world rates are in [0.001, 1.0]. The 0.0001 value | ||
| * exercises the edge of the fixed-notation range. | ||
| */ | ||
| @Param({"0.5", "0.1", "0.01", "0.001", "0.0001", "0.123456789", "0.999999"}) | ||
| double rate; | ||
|
|
||
| PTagsFactory.PTags ptags; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setUp() { | ||
| ptags = (PTagsFactory.PTags) PropagationTags.factory().empty(); | ||
| ptags.updateKnuthSamplingRate(rate); | ||
| } | ||
|
|
||
| /** Baseline: old implementation using String.format + substring trimming. */ | ||
| @Benchmark | ||
| public void stringFormat(Blackhole bh) { | ||
| bh.consume(stringFormatImpl(rate)); | ||
| } | ||
|
|
||
| /** Custom formatter: char-array arithmetic, no Formatter allocation. */ | ||
| @Benchmark | ||
| public void customFormat(Blackhole bh) { | ||
| bh.consume(PTagsFactory.PTags.formatKnuthSamplingRate(rate)); | ||
| } | ||
|
|
||
| /** | ||
| * Cached TagValue: the full getKnuthSamplingRateTagValue() hot-path after caching. Should be | ||
| * near-zero allocation (volatile read only). | ||
| */ | ||
| @Benchmark | ||
| public void cachedTagValue(Blackhole bh) { | ||
| bh.consume(ptags.getKnuthSamplingRateTagValue()); | ||
| } | ||
|
|
||
| /** | ||
| * Models the per-trace allocation cost: resets the instance cache (simulating a new PTags), then | ||
| * calls updateKnuthSamplingRate. This is what every trace root pays. With the static cache | ||
| * applied, this should also be near-zero allocation after warmup. | ||
| */ | ||
| @Benchmark | ||
| public void updateRateFreshTrace(Blackhole bh) { | ||
| ptags.updateKnuthSamplingRate(Double.NaN); // reset instance cache, like a new PTags | ||
| ptags.updateKnuthSamplingRate(rate); | ||
| bh.consume(ptags.getKnuthSamplingRateTagValue()); | ||
| } | ||
|
|
||
| // ---- old implementation for comparison ---- | ||
|
|
||
| static String stringFormatImpl(double rate) { | ||
| String formatted = String.format(Locale.ROOT, "%.6g", rate); | ||
| int dotIndex = formatted.indexOf('.'); | ||
| if (dotIndex >= 0) { | ||
| int end = formatted.length(); | ||
| while (end > dotIndex + 1 && formatted.charAt(end - 1) == '0') { | ||
| end--; | ||
| } | ||
| if (formatted.charAt(end - 1) == '.') { | ||
| end--; | ||
| } | ||
| formatted = formatted.substring(0, end); | ||
| } | ||
| return formatted; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.