fix(kafka): stop mutating caller-owned message.headers#8388
Merged
Conversation
Both kafkajs's producer wrap and the `@confluentinc/kafka-javascript`
KafkaJS-compat producer wrap mutated the caller's `message.headers` in
place. The kafkajs boundary seeded `headers: {}` on every message before
the plugin injected trace context; the confluent KafkaJS-compat path
handed `payload.messages` straight to the underlying client, which then
wrote `headers: null` on messages without headers. Either path leaves
the caller holding the side effects of someone else's instrumentation.
The boundary now hands kafkajs and the underlying confluent client a
shallow clone of the messages array via a single `cloneMessages`
helper. With injection enabled the helper seeds `headers: {}` on the
clone for the plugin to inject into; with injection disabled (after the
broker reported `KafkaJSProtocolError UNKNOWN` /
`KafkaJSError ERR_UNKNOWN`) the helper preserves the absence of
`headers` so brokers that reject any header field can recover.
Refs: #5270
Refs: #8253
2 tasks
Contributor
Overall package sizeSelf size: 5.81 MB Dependency sizes| name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.0.1 | 82.56 kB | 817.39 kB | | dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |🤖 This report was automatically generated by heaviest-objects-in-the-universe |
🎉 All green!❄️ No new flaky tests detected 🎯 Code Coverage (details) 🔗 Commit SHA: 0e57fad | Docs | Datadog PR Page | Give us feedback! |
BenchmarksBenchmark execution time: 2026-05-09 19:08:31 Comparing candidate commit 0e57fad in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 1733 metrics, 111 unstable metrics. |
tlhunter
approved these changes
May 11, 2026
Merged
rochdev
pushed a commit
that referenced
this pull request
May 13, 2026
Both kafkajs's producer wrap and the `@confluentinc/kafka-javascript`
KafkaJS-compat producer wrap mutated the caller's `message.headers` in
place. The kafkajs boundary seeded `headers: {}` on every message before
the plugin injected trace context; the confluent KafkaJS-compat path
handed `payload.messages` straight to the underlying client, which then
wrote `headers: null` on messages without headers. Either path leaves
the caller holding the side effects of someone else's instrumentation.
The boundary now hands kafkajs and the underlying confluent client a
shallow clone of the messages array via a single `cloneMessages`
helper. With injection enabled the helper seeds `headers: {}` on the
clone for the plugin to inject into; with injection disabled (after the
broker reported `KafkaJSProtocolError UNKNOWN` /
`KafkaJSError ERR_UNKNOWN`) the helper preserves the absence of
`headers` so brokers that reject any header field can recover.
Refs: #5270
Refs: #8253
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Both kafkajs's producer wrap and the
@confluentinc/kafka-javascriptKafkaJS-compat producer wrap mutated the caller'smessage.headersin place. Either path leaves the caller holding the side effects of someone else's instrumentation.headers: {}on every message before the plugin injected trace context.payload.messagesstraight to the underlying client, which then wroteheaders: nullon messages without headers.The boundary now hands kafkajs and the underlying confluent client a shallow clone of the messages array via a single
cloneMessages(messages, ensureHeaders)helper. With injection enabled the helper seedsheaders: {}on the clone for the plugin to inject into; with injection disabled (after the broker reportedKafkaJSProtocolError UNKNOWN/KafkaJSError ERR_UNKNOWN) the helper preserves the absence ofheadersso brokers that reject any header field can recover.Test plan
./node_modules/.bin/mocha packages/datadog-instrumentations/test/helpers/kafka.spec.js— unit tests forcloneMessages.PLUGINS=kafkajs SPEC=index npm run test:plugins— exercises the deep-frozen-input regression test and theKafkaJSProtocolError UNKNOWNrecovery path that pins the no-headers: {}seeding invariant.PLUGINS=confluentinc-kafka-javascript SPEC=dsm npm run test:plugins— pins the same invariant for the KafkaJS-compat path.Refs: #5270
Refs: #8253