Skip to content

Fix NoSuchElementException from unchecked Optional.get() in Kafka consumer instrumentation#12007

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 2 commits into
masterfrom
piotr.wolski/fix-kafka-optional-get-npe
Jul 21, 2026
Merged

Fix NoSuchElementException from unchecked Optional.get() in Kafka consumer instrumentation#12007
gh-worker-dd-mergequeue-cf854d[bot] merged 2 commits into
masterfrom
piotr.wolski/fix-kafka-optional-get-npe

Conversation

@piochelepiotr

@piochelepiotr piochelepiotr commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • KafkaConsumerInstrumentationHelper (kafka-clients-3.8) called Optional.get() on the consumer group, metadata, and bootstrap servers without checking isPresent()/using orElse(), throwing NoSuchElementException when those values weren't captured (e.g. Optional.get errors seen in production telemetry, introduced in 1.64).
  • Fixed all three accessors (extractGroup, extractClusterId, extractBootstrapServers) to safely handle the empty case instead of throwing.
  • Added a KafkaConsumerInstrumentationHelperTest unit test covering null/empty-Optional cases for all three accessors, to prevent regressions.
  • Checked the older kafka-clients-0.11 module — it never wraps these values in Optional (plain nullable getters with != null checks), so it isn't affected.

Reported in https://dd.slack.com/archives/C047YAMGZ7T/p1784318891028629

Test plan

  • ./gradlew :dd-java-agent:instrumentation:kafka:kafka-clients-3.8:compileJava succeeds
  • ./gradlew :dd-java-agent:instrumentation:kafka:kafka-clients-3.8:spotlessApply clean
  • ./gradlew :dd-java-agent:instrumentation:kafka:kafka-clients-3.8:test passes, including new KafkaConsumerInstrumentationHelperTest

🤖 Generated with Claude Code

…sumer instrumentation

extractGroup, extractClusterId, and extractBootstrapServers called
Optional.get() without checking isPresent()/using orElse(), which threw
NoSuchElementException when the underlying consumer group, metadata, or
bootstrap servers were not captured. Introduced in 1.64 and observed in
production error telemetry.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@piochelepiotr piochelepiotr added tag: ai generated Largely based on code generated by an AI or LLM inst: kafka Kafka instrumentation type: bug fix Bug fix labels Jul 20, 2026
@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
Patch Coverage: 100.00%
Overall Coverage: 69.73% (+12.44%)

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

@piochelepiotr
piochelepiotr marked this pull request as ready for review July 20, 2026 17:23
@piochelepiotr
piochelepiotr requested review from a team as code owners July 20, 2026 17:23
@piochelepiotr
piochelepiotr requested review from jordan-wong and removed request for a team July 20, 2026 17:23
Covers the null/empty-Optional cases for extractGroup, extractClusterId,
and extractBootstrapServers to prevent regressions of the previously
fixed NoSuchElementException.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

@datadog-datadog-prod-us1-2 datadog-datadog-prod-us1-2 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.

Datadog Autotest: PASS

More details

All three Optional.get() calls in KafkaConsumerInstrumentationHelper are correctly replaced: extractGroup and extractBootstrapServers use orElse(null), and extractClusterId replaces the unchecked .get() + dead null-check with an isPresent() guard. 22 adversarial scenarios (null info, all-null fields, partial null, happy path) confirmed the fix eliminates NoSuchElementException without regressing the non-null path.

Was this helpful? React 👍 or 👎

📊 Validated against 22 scenarios · Open Bits AI session

🤖 Datadog Autotest · Commit e20ee61 · What is Autotest? · Any feedback? Reach out in #autotest

@dd-octo-sts

dd-octo-sts Bot commented Jul 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 13.98 s 13.99 s [-0.9%; +0.7%] (no difference)
startup:insecure-bank:tracing:Agent 12.93 s 13.01 s [-1.3%; +0.1%] (no difference)
startup:petclinic:appsec:Agent 16.97 s 16.76 s [+0.3%; +2.2%] (maybe worse)
startup:petclinic:iast:Agent 16.94 s 16.90 s [-0.5%; +1.1%] (no difference)
startup:petclinic:profiling:Agent 16.83 s 16.71 s [-0.5%; +2.0%] (no difference)
startup:petclinic:sca:Agent 16.84 s 16.74 s [-0.3%; +1.6%] (no difference)
startup:petclinic:tracing:Agent 16.06 s 16.08 s [-1.0%; +0.8%] (no difference)

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

@piochelepiotr
piochelepiotr added this pull request to the merge queue Jul 20, 2026
@dd-octo-sts

dd-octo-sts Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Jul 20, 2026

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-07-20 20:00:49 UTC ℹ️ Start processing command /merge


2026-07-20 20:00:54 UTC ℹ️ MergeQueue: pull request added to the queue

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


2026-07-20 20:12:49 UTCMergeQueue: The build pipeline contains failing jobs for this merge request

Build pipeline has failing jobs for f421774:

⚠️ Do NOT retry failed jobs directly (why?).

What to do next?

  • Investigate the failures and when ready, re-add your pull request to the queue!
  • If your PR checks are green, try to rebase/merge. It might be because the CI run is a bit old.
  • Any question, go check the FAQ.
Details

Since those jobs are not marked as being allowed to fail, the pipeline will most likely fail.
Therefore, and to allow other builds to be processed, this merge request has been rejected and the pipeline got canceled.

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 20, 2026
@PerfectSlayer

Copy link
Copy Markdown
Contributor

/merge

@gh-worker-devflow-routing-ef8351

gh-worker-devflow-routing-ef8351 Bot commented Jul 21, 2026

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-07-21 06:07:13 UTC ℹ️ Start processing command /merge


2026-07-21 06:07:18 UTC ℹ️ MergeQueue: pull request added to the queue

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


2026-07-21 07:12:30 UTC ℹ️ MergeQueue: This merge request was merged

@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit c66a0b5 into master Jul 21, 2026
591 of 592 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the piotr.wolski/fix-kafka-optional-get-npe branch July 21, 2026 07:12
@github-actions github-actions Bot added this to the 1.65.0 milestone Jul 21, 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 type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants