Skip to content

Dispatch extraction on header constants instead of comparing names - #13613

Draft
dougqh wants to merge 6 commits into
dougqh/kafka-carrier-cachefrom
dougqh/header-schema
Draft

dougqh wants to merge 6 commits into
dougqh/kafka-carrier-cachefrom
dougqh/header-schema

Conversation

@dougqh

@dougqh dougqh commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What Does This Do

Adds HeaderSchema, an enum of the headers the tracer knows by name, and dispatches extraction on those constants instead of comparing header names. Extracted contexts are unchanged.

Stacked on #13606.

Motivation

A 30-minute CPU profile of a Kafka consumer had ~6% of the consumer thread's tracer time in header-name comparisons: each record's headers were compared, case-insensitively, against several names by the span id cache, the baggage extractor and the Datadog and W3C interpreters, about 30 equalsIgnoreCase calls per record. #13605 already matches each key once against the propagators' declared names; this carries the result of that match along as a constant, so later code switches on it.

Additional Notes

  • HeaderSchema (components/context): Datadog, W3C, baggage, B3, Haystack, X-Ray and DSM headers, plus the forwarding / user agent / client IP headers every style reads. Each constant has its lower-case name and ASCII bytes. Its ordinal() is a dense index: carrier-specific forms of a name (a Netty AsciiString, a gRPC Metadata.Key) belong in arrays indexed by ordinal next to their instrumentations, which keeps the enum dependency-free.
  • CarrierKeys records the constant of every declared name that is a known header, and returns it from header(String) / headerAscii(ByteBuffer) (with an identity shortcut, since carriers that matched a key's bytes pass the declared name itself).
  • The composite propagator's cache records each pair's constant (IndexedCarrier.header).
  • KeyClassifier.accept(HeaderSchema, key, value), defaulting to the by-name accept. ContextInterpreter implements it: a style's own headers go straight to its handling, the shared forwarding and user agent headers to their handlers, and other known headers to the common tail (client IP, header tags, mapped baggage). A null constant, header-name logging, or a style that doesn't classify the header takes the by-name path, so the two paths can't disagree.
  • Datadog, W3C and Haystack classify their headers (x-amzn-trace-id, read only with AWS propagation, stays by name); None, with no headers of its own, sends every known header to the shared handling. B3 and X-Ray stay by name: their accept wraps the shared handling and the common tail in a try/catch, which the generic dispatch would move outside it.
  • ExtractionCache's span id check, the single-extractor path and the baggage extractor use the constants too.
  • Tests: HeaderSchemaTest; CarrierKeysTest / CarrierCacheTest for constants; ExtractorKeysForkedTest checks, for every style (with its own keys and with every known header) and for the compound Datadog + W3C extractor reading the carrier in place, that dispatching on constants extracts the same context as by name. Propagation and baggage tests pass unchanged (1,431, including the compound-extraction golden test, which goes through the composite propagator and so through the constant path).
  • The carrier cache stores each pair's constant as a byte (ordinal() + 1, 0 for none), not a second reference array, keeping the per-extraction cost to a byte[8].
  • Numbers: neutral on CPU, so the case for this PR is structural.

🤖 Generated with Claude Code

@dougqh dougqh added comp: core Tracer core tag: performance Performance related changes type: refactoring tag: ai generated Largely based on code generated by an AI or LLM labels Nov 21, 2026
@datadog-official

datadog-official Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
• Patch Coverage: 87.19%
• Overall Coverage: 59.65% (+0.05%)

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 2976862 | Docs | Give us feedback!

@dd-octo-sts

dd-octo-sts Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

🟡 Java Benchmark SLOs — Performance SLO warning (near threshold)

Suite Status
Startup 🟡 warning

SLO thresholds are defined here based on automatically generated metrics. A warning is raised when results are within 5% of the threshold.

PR vs. master results
Scenario Candidate master Δ (95% CI of mean)
startup:insecure-bank:iast:Agent 14.04 s 14.01 s [-0.5%; +0.8%] (no difference)
startup:insecure-bank:tracing:Agent 13.00 s 13.02 s [-0.8%; +0.5%] (no difference)
startup:petclinic:appsec:Agent 17.97 s 17.66 s [+0.8%; +2.6%] (maybe worse)
startup:petclinic:iast:Agent 17.83 s 17.78 s [-0.5%; +1.1%] (no difference)
startup:petclinic:profiling:Agent 17.68 s 17.42 s [+0.3%; +2.7%] (maybe worse)
startup:petclinic:sca:Agent 17.97 s 17.88 s [-0.4%; +1.4%] (no difference)
startup:petclinic:tracing:Agent 16.89 s 16.92 s [-1.2%; +0.9%] (no difference)

Commit: 2976862a · CI Pipeline · Benchmarking Platform UI


Load and DaCapo benchmarks can be triggered manually in the GitLab pipeline. Results will appear in the Benchmarking Platform UI after completion.

@pr-commenter

pr-commenter Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Kafka / producer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch dougqh/kafka-carrier-cache dougqh/header-schema
git_commit_date 1795553620 1795553623
git_commit_sha 77290f9 2976862
See matching parameters
Baseline Candidate
ci_job_date 1795555096 1795555096
ci_job_id 2298335622 2298335622
ci_pipeline_id 156344360 156344360
cpu_model Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
jdkVersion 11.0.31 11.0.31
jmhVersion 1.36 1.36
jvm /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-11-openjdk-amd64/bin/java
jvmArgs -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=15002 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=15002 -Dhttp.nonProxyHosts=localhost *.localhost
kernel_version Linux runner-zfyrx7zua-project-304-concurrent-0-b7j85gkr 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux Linux runner-zfyrx7zua-project-304-concurrent-0-b7j85gkr 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
vmName OpenJDK 64-Bit Server VM OpenJDK 64-Bit Server VM
vmVersion 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 3 metrics, 0 unstable metrics.

See unchanged results
scenario Δ mean throughput
scenario:not-instrumented/KafkaProduceBenchmark.benchProduce same
scenario:only-tracing-dsm-disabled-benchmarks/KafkaProduceBenchmark.benchProduce same
scenario:only-tracing-dsm-enabled-benchmarks/KafkaProduceBenchmark.benchProduce same

@pr-commenter

pr-commenter Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Kafka / consumer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch dougqh/kafka-carrier-cache dougqh/header-schema
git_commit_date 1795553620 1795553623
git_commit_sha 77290f9 2976862
See matching parameters
Baseline Candidate
ci_job_date 1795555048 1795555048
ci_job_id 2298335628 2298335628
ci_pipeline_id 156344360 156344360
cpu_model Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
jdkVersion 11.0.31 11.0.31
jmhVersion 1.36 1.36
jvm /usr/lib/jvm/java-11-openjdk-amd64/bin/java /usr/lib/jvm/java-11-openjdk-amd64/bin/java
jvmArgs -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=15002 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=15002 -Dhttp.nonProxyHosts=localhost *.localhost
kernel_version Linux runner-zfyrx7zua-project-304-concurrent-0-gxqh2ljj 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux Linux runner-zfyrx7zua-project-304-concurrent-0-gxqh2ljj 6.8.0-1031-aws #33~22.04.1-Ubuntu SMP Thu Jun 26 14:22:30 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
vmName OpenJDK 64-Bit Server VM OpenJDK 64-Bit Server VM
vmVersion 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu 11.0.31+11-post-1ubuntu1-22.04.2-Ubuntu

Summary

Found 0 performance improvements and 0 performance regressions! Performance is the same for 3 metrics, 0 unstable metrics.

See unchanged results
scenario Δ mean throughput
scenario:not-instrumented/KafkaConsumerBenchmark.benchConsume same
scenario:only-tracing-dsm-disabled-benchmarks/KafkaConsumerBenchmark.benchConsume same
scenario:only-tracing-dsm-enabled-benchmarks/KafkaConsumerBenchmark.benchConsume unsure
[-9267.705op/s; -1266.995op/s] or [-4.972%; -0.680%]

@dougqh
dougqh force-pushed the dougqh/kafka-carrier-cache branch from fe5dfd1 to 8131802 Compare November 21, 2026 19:14
@dougqh
dougqh force-pushed the dougqh/header-schema branch 2 times, most recently from d5708a4 to 93a92d8 Compare November 21, 2026 19:39
@dougqh
dougqh force-pushed the dougqh/kafka-carrier-cache branch from 8131802 to 5bc558d Compare November 21, 2026 19:39
@dougqh
dougqh force-pushed the dougqh/header-schema branch from 93a92d8 to a75febe Compare November 23, 2026 21:17
@dougqh
dougqh force-pushed the dougqh/kafka-carrier-cache branch 2 times, most recently from 79174d1 to c7a93cf Compare November 23, 2026 23:36
@dougqh
dougqh force-pushed the dougqh/header-schema branch 2 times, most recently from c282506 to 50847b7 Compare November 23, 2026 23:47
@dougqh
dougqh force-pushed the dougqh/kafka-carrier-cache branch from c7a93cf to 6deb973 Compare November 23, 2026 23:47
@dougqh
dougqh force-pushed the dougqh/header-schema branch from 50847b7 to 62bdb8d Compare November 24, 2026 02:33
@dougqh
dougqh force-pushed the dougqh/kafka-carrier-cache branch 2 times, most recently from 39711e1 to aa6224b Compare November 24, 2026 20:10
@dougqh
dougqh force-pushed the dougqh/header-schema branch from a4d245a to 04908fe Compare November 24, 2026 20:10
dougqh and others added 6 commits November 24, 2026 15:53
An enum of the carrier keys propagators read (Datadog, W3C, baggage, B3,
Haystack, X-Ray, DSM, and the forwarding / user agent / client IP headers
every style reads), each with its lower-case name and ASCII bytes. Its
ordinal is a dense index for per-header arrays, which keeps carrier-specific
forms (a Netty AsciiString, a gRPC Metadata.Key) next to their
instrumentations.

CarrierKeys records the constant of every declared name that is a known
header and returns it from header(String) / headerAscii(ByteBuffer), so
code handling several headers can switch on it instead of comparing names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The composite propagator's cache records each key's HeaderSchema constant
(IndexedCarrier.header), and passes it along:

- KeyClassifier gains accept(HeaderSchema, key, value), defaulting to the
  by-name accept.
- ContextInterpreter's implementation sends a style's own headers straight
  to its handling, the shared forwarding / user agent headers to their
  handlers, and other known headers to the common tail (client IP, header
  tags, mapped baggage). A null constant, header-name logging, or a style
  that doesn't classify the header takes the by-name path.
- The Datadog and W3C interpreters classify their headers this way; the
  other styles keep the by-name path for now.
- ExtractionCache's span id check, the single-extractor path and the
  baggage extractor use the constants too.

A profile of a Kafka consumer had ~6% of the consumer thread's tracer time
in these name comparisons (about 30 equalsIgnoreCase calls per record).
ExtractorKeysForkedTest checks that, for every style and for the compound
extractor, dispatching on constants extracts the same context as by name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The carrier cache kept a HeaderSchema[] beside its pairs, a second
reference array per extraction (about 48 bytes for the usual 8 pairs).
With fewer than 127 constants, ordinal() + 1 fits in a byte, so it's a
byte[] instead (24 bytes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Haystack classifies its trace / span / parent id headers like Datadog and
W3C; None has no headers of its own, so every known header gets the shared
handling directly. B3 and X-Ray stay by name: their accept wraps the shared
handling and the common tail in its try/catch, which the generic dispatch
would move outside it. ExtractorKeysForkedTest's per-style equivalence
check covers the two new styles.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Same as CarrierKeys.contains: keys are usually lower case already, so
String.equals (intrinsified, with an identity check) catches them before
the case-insensitive comparison.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…racter

CarrierKeys.header(String) and contains(String) compared each declared name
with equals and then, on a miss, ignoring case. Most keys a carrier offers are
no declared name, so every key as long as a declared name paid a
case-insensitive comparison against each of them: about half of the header
key matching on a Kafka consumer's main thread (regionMatchesCI under
CarrierKeys.header).

As the byte path already does, the String path now compares exactly first
and only compares ignoring case if the key has an upper-case character at
all. Declared names and prefixes are stored lower case, so a key without one
can only match exactly. (Java's case-insensitive comparison also folds a few
non-ASCII characters onto ASCII letters; header names are ASCII tokens, and
the byte path already matches no non-ASCII key.)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh
dougqh force-pushed the dougqh/kafka-carrier-cache branch from aa6224b to 77290f9 Compare November 24, 2026 21:00
@dougqh
dougqh force-pushed the dougqh/header-schema branch from 04908fe to 2976862 Compare November 24, 2026 21:00

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: core Tracer core tag: ai generated Largely based on code generated by an AI or LLM tag: performance Performance related changes type: refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant