Skip to content

Record one correctly tagged Data Streams consume per Kafka Streams hop - #13587

Open
arjunguhaswe wants to merge 5 commits into
masterfrom
arjunguha/dsm2-797-kafka-streams-consume-checkpoints
Open

arjunguhaswe wants to merge 5 commits into
masterfrom
arjunguha/dsm2-797-kafka-streams-consume-checkpoints

Conversation

@arjunguhaswe

@arjunguhaswe arjunguhaswe commented Nov 20, 2026 •

Copy link
Copy Markdown

What Does This Do

Data Streams Monitoring (DSM) records consumes from a Kafka Streams app more than once per hop, and with the wrong tags. This PR records exactly one consume checkpoint per record per hop, tagged with the right topic, group (the application id) and kafka_cluster_id, and chained to the upstream produce.

Before, with released 1.69.0, each Streams stage emitted:

  1. a full kafka-clients consume;
  2. an identical second consume, whose parent is the first;
  3. a Streams consume with topic and group swapped, no topic (shown as queue: in the map), and no cluster id.

The downstream produce then chained off the swapped node.

Motivation

In an internal investigation of high DSM cardinality, this pattern was a common contributor wherever Kafka Streams is used. On Streams consume edges it adds about 1.3–2× pairs, so pathways get dropped by the per-service hash quota. It also corrupts the map: topics show up as consumer groups, and every Streams consume collapses into one topic-less node.

How it works

  1. Swapped arguments. KafkaStreamTaskInstrumentation called createWithGroup(type, direction, applicationId, topic), but the signature is (type, direction, topic, group). Fix swapped topic and group tags on Kafka Streams Data Streams metrics #13671 fixed the order on master; this PR keeps it and adds the cluster id (6).
  2. Double consume. Streams only unwrapped the kafka-clients-0.11 record collections. With kafka-clients ≥ 3.8, every Streams record also went through the 3.8 TracingIterator, which set a checkpoint and injected it into the headers. The Streams consume then chained off that injected header.
    • TracingIterableDelegator moves to kafka-common, so both kafka-clients modules are unwrapped. The Streams task now owns the single consume checkpoint.
    • From Streams 3.0, StreamThread.pollPhase() also reads every polled record (records(partition).stream().max(...), to track end offsets) before the tasks get them, whatever the kafka-clients version. That read set a second checkpoint with the same hash, so the Streams consume node counted every record twice. A new StreamThread advice marks the poll phase, and the kafka-clients record collections hand out untraced iterators while it runs.
  3. Header propagation. The Streams task always writes its consume pathway into the record headers, so the downstream produce chains to it.
  4. Topology-internal topics. Plain consumers and producers now skip only topics internal to a topology (repartition, changelog). Before, they also lost checkpoints on source and sink topics whenever a topology ran in the same JVM.
  5. Missing group on Streams ≥ 3.2. StreamsConfig is no longer a StreamTask constructor argument, so the application id was never captured. It is now read from the ProcessorContext constructor argument (Streams 2.6 and later).
  6. Cluster id. The Streams consume takes kafka_cluster_id from the unwrapped kafka-clients collection, so it matches the plain consume.
  7. kafka-clients-3.8 constructor advice. It called groupMetadata(), which throws InvalidGroupIdException for consumers without a group (Streams restore and global consumers). That aborted the advice, leaving those consumers with no consumer info and no cluster id. It is now only a guarded fallback.
  8. DSM transactions. kafka-clients no longer sees these records, so the Streams consume now extracts KAFKA_CONSUME_HEADERS transactions itself.

Testing

  • New kafka38Test suite in kafka-streams-0.11 (Streams + kafka-clients 3.8.1, JUnit 5 KafkaStreams38DataStreamsForkedTest, run by kafka38ForkedTest). It asserts:
    • no topic/group swap;
    • exactly one Streams consume per hop, with group = application id and the cluster id;
    • parentage produce → consume → produce → consume;
    • one checkpoint per record on every pathway node;
    • a DSM transaction in the headers is tracked once, on the Streams consume span.
  • Without the StreamThread advice, both new tests fail: the Streams consume node has 6 checkpoints for 3 records, and two spans carry the transaction.
  • Existing tests:
  • End to end, against a real agent in a local kind cluster, with an A/B pair of Streams pipelines (producer → two chained Streams apps → sink, Kafka 3.9.1):
    • Released 1.69.0 emits 10 DSM nodes.
    • This branch emits 6 nodes, one consume per hop, each with the correct group and cluster id. (Run before the pollPhase change, which doesn't change the node count.)

Verification

Verification done with A/B Test Apps run before the fix and after the fix.
image

Pathway Cardinality Shown to Be Reduced

image image

Additional Notes

  • Known limitation, unchanged: StreamingContext is JVM-global. Two Streams apps in one JVM that are chained through a topic still treat that topic as internal, so that hop is skipped.
  • Consume spans: kafka-clients kafka.consume spans no longer appear for the records of a Streams app's main consumer. Before, they did on Streams 3.0 and later (from pollPhase) and on kafka-clients ≥ 3.8. The restore and global consumers are unchanged.

Jira ticket: DSM2-797

🤖 Generated with Claude Code

@arjunguhaswe arjunguhaswe added type: bug fix Bug fix inst: kafka Kafka instrumentation comp: data streams Data Streams Monitoring labels Nov 20, 2026
@linear-code

linear-code Bot commented Nov 20, 2026

Copy link
Copy Markdown

DSM2-797

@datadog-datadog-prod-us1

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

Copy link
Copy Markdown
Contributor

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

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

@dd-octo-sts

dd-octo-sts Bot commented Nov 20, 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.00 s 13.95 s [-0.2%; +0.9%] (no difference)
startup:insecure-bank:tracing:Agent 12.96 s 13.02 s [-1.1%; +0.2%] (no difference)
startup:petclinic:appsec:Agent 17.67 s 17.62 s [-0.5%; +1.1%] (no difference)
startup:petclinic:iast:Agent 17.59 s 17.60 s [-1.1%; +0.9%] (no difference)
startup:petclinic:profiling:Agent 17.37 s 17.49 s [-1.7%; +0.4%] (no difference)
startup:petclinic:sca:Agent 17.83 s 17.70 s [-0.2%; +1.7%] (no difference)
startup:petclinic:tracing:Agent 16.35 s 16.77 s [-6.7%; +1.7%] (no difference)

Commit: 8d5b32da · 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 20, 2026 •

Copy link
Copy Markdown

Kafka / consumer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch master arjunguha/dsm2-797-kafka-streams-consume-checkpoints
git_commit_date 1795795090 1795989384
git_commit_sha a9f4fee e0f1386
See matching parameters
Baseline Candidate
ci_job_date 1795990412 1795990412
ci_job_id 2309106283 2309106283
ci_pipeline_id 156927852 156927852
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-4a0k4b2l 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-4a0k4b2l 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

@arjunguhaswe
arjunguhaswe marked this pull request as ready for review November 24, 2026 21:35
@arjunguhaswe
arjunguhaswe requested review from a team as code owners November 24, 2026 21:35
@arjunguhaswe
arjunguhaswe requested review from jordan-wong and sarahchen6 and removed request for a team November 24, 2026 21:35
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-24T21:40:26.479895Z 2172489 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.

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

Copy link
Copy Markdown

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: 2172489306

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

* id is no longer a StreamTask constructor argument, and the kafka-clients-3.8 record collections
* used to escape the unwrapping done by the streams instrumentation, adding consume checkpoints.
*/
class KafkaStreams38DataStreamsTest extends InstrumentationSpecification {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Write the new Streams regression test in Java/JUnit 5

This adds a new Groovy/Spock test, whereas the repository requires Java/JUnit 5 for new tests, including instrumentation tests, unless an explicit exception is granted. Keeping it beside existing Spock tests, as the commit message explains, does not itself grant that exception. Without the tag: override groovy enforcement label, .github/workflows/enforce-groovy-migration.yaml rejects this added file. Port the regression test to Java/JUnit 5 or obtain the explicit override.

AGENTS.md reference: AGENTS.md:L64-L64

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed!

@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: FAIL

Kafka Streams consumers on kafka-clients 3.8+ lose configured DSM transaction tracking because record unwrapping bypasses consume-header extraction without replacing it in the Streams processing advice.

Open Bits AI session

🤖 Bits Code Review · Commit 2172489

// Expecting a TracingList because TaskManager.addRecordsToTasks calls records(partition).
if (records instanceof TracingIterableDelegator) {
records = ((TracingIterableDelegator) records).getDelegate();
TracingIterableDelegator delegator = (TracingIterableDelegator) records;

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.

P1 Preserve DSM transaction extraction when unwrapping Streams records

For Kafka Streams using kafka-clients ≥3.8 with a configured KAFKA_CONSUME_HEADERS transaction extractor, unwrapping now bypasses TracingIterator.startNewRecordSpan() and its trackTransaction() call. Neither Streams processing advice replaces that extraction, so matching consumed records lose transaction tracking events and DSM transaction span tags. Preserve transaction extraction in the Streams processing path, including version-specific header access, while removing the duplicate consume checkpoints.

React: 👍 useful · 👎 wrong · 😕 unclear · 👀 right, but not acting on it

What each reaction means
  • 👍 Useful: correct, and you're fixing it or will.
  • 👎 Wrong: the finding is incorrect, for example the bug can't happen or the code was misread.
  • 😕 Unclear: you can't tell what it means or why it matters.
  • 👀 Right, not acting: correct, but intended, out of scope for this PR, a known risk, or too minor.

🤖 Bits Code Review · Open Bits AI session

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Addressed!

arjunguhaswe and others added 4 commits November 29, 2026 11:53
The streams task instrumentation built its consume checkpoint tags with
createWithGroup(type, direction, applicationId, topic), but the signature
is (type, direction, topic, group): the application id became the topic
and the source topic the group.

With kafka-clients >= 3.8 the streams instrumentation did not recognize
the kafka_clients38 TracingList, so Kafka Streams records also went
through the kafka-clients TracingIterator, which set a second consume
checkpoint (and wrote it into the headers) for source topics. The streams
consume then chained off that one, giving several consumes per hop.

- Pass the topic and the application id in the right order.
- Move TracingIterableDelegator to kafka-common so the streams
  instrumentation unwraps the record collections of both kafka-clients
  instrumentations. The streams task now owns the single consume
  checkpoint per record per hop.
- The streams task always writes the resulting pathway into the record
  headers, so the downstream produce (which extracts from the forwarded
  headers) chains to that consume, even when no topology was registered.
- Plain kafka-clients consumers and producers only skip topics internal
  to a topology (repartition, changelog); before, they also skipped
  source/sink topics of a topology running in the same JVM.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Follow-up to the previous commit, for Kafka Streams >= 3.2 and
kafka-clients >= 3.8:

- From Kafka Streams 3.2 on StreamsConfig is no longer a StreamTask
  constructor argument, so the application id was never captured and the
  consume checkpoint had no group. Read it from the processor context, the
  12th constructor argument from 2.6 on.
- Take the kafka_cluster_id for the streams consume checkpoint from the
  kafka-clients record collection that UnwrapIterableAdvice unwraps, so it
  matches the plain kafka-clients consume.
- kafka-clients-3.8 consumer constructor advice called groupMetadata(),
  which throws for consumers without a group (Streams restore and global
  consumers), aborting the advice: no consumer info, no cluster id.
- Add a kafka38Test suite (Kafka Streams + kafka-clients 3.8.1) asserting
  one consume checkpoint per record per hop, group = application id,
  cluster id, and parentage produce -> consume -> produce -> consume.

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

From Kafka Streams 3.0 on, StreamThread.pollPhase() reads every polled
record (records(partition).stream().max(...), to track end offsets)
before it hands the records to the tasks. That read went through the
kafka-clients TracingIterator, which set a second consume checkpoint with
the same hash as the streams one, so the streams consume node counted
every record twice, and opened a kafka-clients consume span.

- A StreamThread advice marks the poll phase (StreamsPollPhase in
  kafka-common). While it runs, the kafka-clients record collections of
  both kafka-clients modules hand out untraced iterators.
- The streams consume advice tracks the DSM transaction of each record
  (KAFKA_CONSUME_HEADERS) itself, as the kafka-clients TracingIterator no
  longer sees these records.
- Port the kafka38Test suite to Java/JUnit 5
  (KafkaStreams38DataStreamsForkedTest). It also asserts one checkpoint
  per record on every pathway node, and that one span tracks the
  transaction. Both fail without the StreamThread advice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Follow-ups from the SDK review of the previous commits:

- The Kafka Streams consume advices only build the DSM tags, payload
  size and context when DSM is on. The propagator injects nothing
  otherwise, and the kafka-clients paths already check first.
- StreamingContext keeps only the set of topology-internal topics, the
  one thing isInternalTopic reads since the source/sink checks went away.
- Correct the TracingIterator comment: the records of the Kafka Streams
  restore and global consumers still go through it.
- Test cleanups: config key constant, static imports, NoOpSink, named
  constants, and no redundant wait in the latestDepTest DSM test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@arjunguhaswe
arjunguhaswe force-pushed the arjunguha/dsm2-797-kafka-streams-consume-checkpoints branch from 2172489 to e0f1386 Compare November 29, 2026 21:56
@dd-octo-sts dd-octo-sts Bot added the tag: ai generated Largely based on code generated by an AI or LLM label Nov 29, 2026

@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

No reportable issue emerged in the checkpoint handoff between Kafka client iteration and Streams processing. This conclusion is limited to source inspection; runtime behavior was not exercised.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit e0f1386

@pr-commenter

pr-commenter Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Kafka / producer-benchmark

Parameters

Baseline Candidate
baseline_or_candidate baseline candidate
git_branch master arjunguha/dsm2-797-kafka-streams-consume-checkpoints
git_commit_date 1796033018 1796062226
git_commit_sha 5331c2c 8d5b32d
See matching parameters
Baseline Candidate
ci_job_date 1796063410 1796063410
ci_job_id 2311899286 2311899286
ci_pipeline_id 157042541 157042541
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-wns7bls8 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-wns7bls8 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

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: data streams Data Streams Monitoring inst: kafka Kafka instrumentation tag: ai generated Largely based on code generated by an AI or LLM type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant