Skip to content

Only remove a Kafka header before injecting it if it's already there - #13181

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 5 commits into
masterfrom
dougqh/kafka-inject-dedup-probe
Nov 23, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 5 commits into
masterfrom
dougqh/kafka-inject-dedup-probe

Conversation

@dougqh

@dougqh dougqh commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

What Does This Do

When Kafka producers inject trace context, each header is now removed first only if it's already on the record. Before, every injected header went through headers.remove(key), which iterates with an iterator it allocates, though on an outgoing record the header is usually not there yet. Now lastHeader(key), which loops by index and doesn't allocate, probes first.

Behavior doesn't change:

  • Duplicate keys still collapse. The Datadog and W3C codecs both write ot-baggage- headers (span baggage, the end-to-end start time). A second write still replaces the first, leaving one header.
  • Read-only headers still throw. add throws IllegalStateException as remove did, so the producer's retry with a writable copy of the record still runs.

Motivation

A JFR profile of kafka-producer-perf-test under the agent showed ArrayList.iterator() at 4.5% of allocation, most of it from this setter, which runs for every injected header on every send.

KafkaInjectBenchmark (new) injects a span into new RecordHeaders (JDK 17, 3 forks, per inject):

spanBaggage before ns after ns before B after B
false 358.1 ± 2.1 342.3 ± 4.2 1728 1536
true 504.8 ± 8.9 508.0 ± 9.7 2400 2144

That's 192 B less per send, and 256 B with span baggage, where the replacing write still removes. The machine was busy (load 7–15), so treat the times as approximate; they're flat or slightly better.

Additional Notes

  • Why not add-only: dropping the removal on records that start with no headers would have saved the scan too, but it would leave duplicate ot-baggage- headers, since two codecs write them.
  • The value copy stays. value.getBytes(UTF_8) still copies each value. Removing it needs a way for codecs to write header values straight into bytes.
  • Tests: TextMapInjectAdapterTest, in both client modules, covers adding, replacing, setting the same key twice and read-only headers. It passes against the old setter too.

🤖 Generated with Claude Code

dougqh and others added 3 commits November 3, 2026 10:30
Removing iterates the headers with an iterator it allocates, once per
injected header, though the header is usually not there yet. Probe with
lastHeader, which doesn't allocate, and remove only on a hit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh dougqh added tag: performance Performance related changes type: refactoring inst: kafka Kafka instrumentation tag: ai generated Largely based on code generated by an AI or LLM labels Nov 3, 2026
@dd-octo-sts

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

Copy link
Copy Markdown
Contributor

🟢 Java Benchmark SLOs — All performance SLOs passed

Suite Status
Startup 🟢 pass

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.83 s 14.75 s [-0.2%; +1.4%] (no difference)
startup:insecure-bank:tracing:Agent 13.54 s 13.70 s [-1.9%; -0.5%] (maybe better)
startup:petclinic:appsec:Agent 17.77 s 17.60 s [+0.1%; +1.7%] (maybe worse)
startup:petclinic:iast:Agent 17.42 s 17.62 s [-2.0%; -0.3%] (maybe better)
startup:petclinic:profiling:Agent 17.50 s 17.45 s [-0.9%; +1.5%] (no difference)
startup:petclinic:sca:Agent 17.14 s 17.67 s [-7.2%; +1.2%] (no difference)
startup:petclinic:tracing:Agent 16.67 s 16.80 s [-1.6%; +0.1%] (no difference)

Commit: 5ed73cd1 · 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.

@datadog-datadog-prod-us1

datadog-datadog-prod-us1 Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
• Patch Coverage: 100.00%
• Overall Coverage: 68.82% (+7.84%)

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

@dougqh
dougqh marked this pull request as ready for review November 3, 2026 16:25
@dougqh
dougqh requested review from a team as code owners November 3, 2026 16:25
@dougqh
dougqh requested review from ValentinZakharov and jandro996 and removed request for a team November 3, 2026 16:25
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T16:27:34.929069Z 722d664 Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@datadog-datadog-prod-us1 datadog-datadog-prod-us1 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

Both Kafka adapters retain duplicate-header replacement and read-only exceptions, preserving the producer’s retry with writable headers.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 722d664

@pr-commenter

pr-commenter Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Kafka / consumer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch master dougqh/kafka-inject-dedup-probe
git_commit_date 1795467848 1795467911
git_commit_sha 5df97cf 5ed73cd
See matching parameters
Baseline Candidate
ci_job_date 1795469063 1795469063
ci_job_id 2292342313 2292342313
ci_pipeline_id 155916264 155916264
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-i2wgyluq 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-i2wgyluq 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 same

@AlexeyKuznetsov-DD AlexeyKuznetsov-DD left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, left optional comments.

Each class is only used through its SETTER.

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

dougqh commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-11-20 14:17:23 UTC ℹ️ Start processing command /merge


2026-11-20 14:17:29 UTC ℹ️ MergeQueue: Pull request is not mergeable yet

It will be processed automatically as soon as GitHub reports it as mergeable. View in MergeQueue UI.

  • Run /code blockers to see what is blocking it.
  • Run /remove to cancel it.

2026-11-20 19:12:09 UTC ⚠️ MergeQueue: This merge request was unqueued

devflow unqueued this merge request: It did not become mergeable within the expected time

@pr-commenter

pr-commenter Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Kafka / producer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch master dougqh/kafka-inject-dedup-probe
git_commit_date 1795467848 1795467911
git_commit_sha 5df97cf 5ed73cd
See matching parameters
Baseline Candidate
ci_job_date 1795469000 1795469000
ci_job_id 2292342309 2292342309
ci_pipeline_id 155916264 155916264
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-rwlt0ok1 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-rwlt0ok1 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

@dougqh

dougqh commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-11-20 20:14:53 UTC ℹ️ Start processing command /merge


2026-11-20 20:15:04 UTC ℹ️ MergeQueue: Pull request is not mergeable yet

It will be processed automatically as soon as GitHub reports it as mergeable. View in MergeQueue UI.

  • Run /code blockers to see what is blocking it.
  • Run /remove to cancel it.

2026-11-21 00:18:13 UTC ⚠️ MergeQueue: This merge request was unqueued

devflow unqueued this merge request: It did not become mergeable within the expected time

@dougqh

dougqh commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-11-21 05:19:33 UTC ℹ️ Start processing command /merge


2026-11-21 05:19:43 UTC ℹ️ MergeQueue: Pull request is not mergeable yet

It will be processed automatically as soon as GitHub reports it as mergeable. View in MergeQueue UI.

  • Run /code blockers to see what is blocking it.
  • Run /remove to cancel it.

2026-11-21 09:23:07 UTC ⚠️ MergeQueue: This merge request was unqueued

devflow unqueued this merge request: It did not become mergeable within the expected time

@dougqh

dougqh commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-11-23 20:24:17 UTC ℹ️ Start processing command /merge


2026-11-23 20:24:25 UTC ℹ️ MergeQueue: Pull request is not mergeable yet

It will be processed automatically as soon as GitHub reports it as mergeable. View in MergeQueue UI.

  • Run /code blockers to see what is blocking it.
  • Run /remove to cancel it.

2026-11-23 22:00:33 UTC ℹ️ MergeQueue: merge request added to the queue

The expected merge time in master is approximately 1h (p90).


2026-11-23 22:46:19 UTC ℹ️ MergeQueue: This merge request was merged

@dougqh
dougqh force-pushed the dougqh/kafka-inject-dedup-probe branch from b063214 to 5ed73cd Compare November 23, 2026 21:05

@datadog-datadog-prod-us1 datadog-datadog-prod-us1 Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bits Code Review: PASS

More details

Static review supports preservation of duplicate-header replacement and read-only-header exceptions in both Kafka client adapters.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 5ed73cd

@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit 0289093 into master Nov 23, 2026
687 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the dougqh/kafka-inject-dedup-probe branch November 23, 2026 22:46
@github-actions github-actions Bot added this to the 1.70.0 milestone Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inst: kafka Kafka instrumentation 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.

2 participants