Skip to content

Reject malformed B3 span ids without exceptions (quick fix) - #13355

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 5 commits into
masterfrom
dougqh/b3-malformed-ids-skip-telemetry
Nov 18, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 5 commits into
masterfrom
dougqh/b3-malformed-ids-skip-telemetry

Conversation

@dougqh

@dougqh dougqh commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What Does This Do

Stops a malformed B3 span ID from costing an exception on every request and from being reported to instrumentation telemetry.

  • A non-throwing hex parse: LongStringUtils.parseUnsignedLongHexOrZero(s, start, len, lowerCaseOnly) is a twin of parseUnsignedLongHex that returns 0 for anything the throwing parser rejects: null, bad bounds, empty input, an illegal or (with lowerCaseOnly) uppercase character, or more than 16 significant digits. It's one pass with no allocation, in the same style as IntStringUtils.parseNonNegativeInt, which returns -1.
  • B3 span IDs use it: setSpanId now returns false for a malformed ID, and both B3 interpreters then call invalidateContext() and return false, just as the exception path did. The rejection is logged at debug level without an exception, with EXCLUDE_TELEMETRY, as DatadogHttpCodec does for invalid IDs.
  • A literal zero is still accepted: 0 is also what a malformed ID parses to, so a zero result counts as malformed only when the input isn't all zeros. A 0000000000000000 span ID behaves exactly as before.
  • Trace IDs and sampling priorities still use the throwing parsers. Their NumberFormatException is now logged with EXCLUDE_TELEMETRY (first commit). Any other exception from header handling may be a tracer bug and is still reported.

Motivation

On 1.68, B3 extraction failures were about 651K instrumentation-telemetry events from 10 orgs in 7 days. All of them were NumberFormatException from a span ID with more than 16 significant hex digits, logged at debug ("Exception when extracting context"), and debug logs that carry an exception are still sent to telemetry:

java.lang.NumberFormatException
  at datadog.trace.api.internal.util.LongStringUtils.numberFormatOutOfLongRange(LongStringUtils.java:137)
  at datadog.trace.api.internal.util.LongStringUtils.parseUnsignedLongHex(LongStringUtils.java:46)
  at datadog.trace.api.DDSpanId.fromHex(DDSpanId.java:36)
  at datadog.trace.agent.core.propagation.B3HttpCodec$B3BaseContextInterpreter.setSpanId(B3HttpCodec.java:187)

The B3 spec requires a span ID of "16 lower-hex characters", so these IDs are malformed and rejecting them is correct. They come from what callers send, not from tracer failures. Each one also cost an exception, including a stack trace and a String.format message, on the request path.

Testing

  • LongStringUtilsTest (new): the non-throwing parse returns exactly what parseUnsignedLongHex returns wherever that parser succeeds, and 0 wherever it throws (null, bounds, empty, case, 17 significant digits, illegal characters, a sub-range). One more case pins significant-digit counting at a non-zero offset.
  • B3HttpExtractorTest:
    • For an oversized multi-header span ID, an oversized single-header span ID, and a non-hex span ID, extraction gives the same result as before (no extracted context, only the header tags), logs no exception, and logs one debug rejection with EXCLUDE_TELEMETRY.
    • A literal zero span ID is still extracted, in both header formats.
  • Against master's B3HttpCodec: the behavior assertions and the zero-span-ID cases pass; only the no-exception/marker assertions fail. That's the evidence that extraction behavior is unchanged.
  • With this change: all 1,385 tests in datadog.trace.core.propagation.* pass, as does DDSpanIdTest, and spotbugsMain is clean for dd-trace-api and dd-trace-core.
  • Benchmark: B3SpanIdExtractBenchmark (new, JMH) extracts B3 headers with a valid, oversized (128-bit) or non-hex span ID, in both header styles. Results are below.

Benchmark

JDK 17, 4 threads, 2 forks × 5 × 5 s, -prof gc, master (98c28c6c01) vs this branch on the same machine. Debug logging is off, as in production.

Malformed span ID: 5–6× faster, about 1.1–1.3 KB less allocation per extraction (the exception and its stack trace).

style span ID master ns/op this PR ns/op master B/op this PR B/op
b3multi oversized 1395.6 ± 59.7 275.0 ± 27.7 2424 1160
b3single oversized 1298.4 ± 45.4 224.5 ± 8.4 2204 928
b3multi non-hex 1260.8 ± 32.5 233.8 ± 2.5 2184 1128
b3single non-hex 1201.3 ± 45.0 189.5 ± 47.1 1936 880

Valid span ID: no change. A 5-fork re-run (same settings) shows the same floor and the same median fork in both arms, and identical allocation.

style master per-fork ns/op this PR per-fork ns/op median fork (master / PR) B/op (master / PR)
b3multi 304, 343, 353, 347, 342 491, 387, 342, 297, 336 343 / 342 1730 / 1730
b3single 295, 307, 239, 231, 233 234, 232, 259, 233, 232 233 / 233 1504 / 1504

The forks are multimodal, so the JMH means (b3multi 337.6 ± 13.4 vs 370.6 ± 51.1, b3single 261.1 ± 25.3 vs 237.8 ± 8.2) move in opposite directions depending on which arm drew an outlier fork.

Additional Notes

  • Zero span IDs: the B3 spec doesn't say whether an all-zero ID is valid. B3 and Datadog accept it today, while W3C (whose spec forbids it) rejects it, and DDSpanId.ZERO is documented as "not allowed". This PR deliberately keeps today's behavior. Whether B3 should reject zero is a separate decision.
  • A pre-existing edge in parseUnsignedLongHex: its 16-digit overflow guard computes len - firstNonZeroCharacter(s, start), and that helper returns an absolute index. At a non-zero start with len > 16, the guard can miss and the value silently overflows. B3 always parses from index 0. The new parser counts within the range. I haven't changed the throwing parser here.
  • Follow-up: W3CHttpCodec has the same pattern at a larger scale ("Exception when building context", about 6.2M events from 190 orgs in 7 days on 1.68). The new parser can serve it too; I'll do that separately.

🤖 Generated with Claude Code

A B3 span id or sampling priority a caller sends malformed, such as a span
id with more than 16 hex digits, makes extraction throw
NumberFormatException. The B3 interpreters handle it by invalidating the
context and logging at debug level, but the logged exception still went
to instrumentation telemetry: about 651K events from 10 orgs in 7 days on
1.68.

Log NumberFormatException with EXCLUDE_TELEMETRY, as DatadogHttpCodec
already does for invalid ids. Other exceptions from header handling may
be tracer bugs and are still reported. Extraction behavior is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh dougqh added type: bug fix Bug fix comp: core Tracer core tag: ai generated Largely based on code generated by an AI or LLM labels Nov 10, 2026
@datadog-datadog-prod-us1

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

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
• Patch Coverage: 88.10%
• Overall Coverage: 59.71% (+0.01%)

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

@dd-octo-sts

dd-octo-sts Bot commented Nov 10, 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.06 s 13.96 s [-0.1%; +1.5%] (no difference)
startup:insecure-bank:tracing:Agent 12.85 s 12.93 s [-1.2%; +0.0%] (no difference)
startup:petclinic:appsec:Agent 17.19 s 17.00 s [+0.2%; +2.0%] (maybe worse)
startup:petclinic:iast:Agent 17.06 s 17.09 s [-1.0%; +0.6%] (no difference)
startup:petclinic:profiling:Agent 16.86 s 16.15 s [+0.1%; +8.8%] (maybe worse)
startup:petclinic:sca:Agent 17.17 s 16.98 s [+0.3%; +2.0%] (maybe worse)
startup:petclinic:tracing:Agent 16.23 s 16.26 s [-1.1%; +0.7%] (no difference)

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

Add LongStringUtils.parseUnsignedLongHexOrZero, a non-throwing twin of
parseUnsignedLongHex that yields 0 for anything the throwing parse
rejects, and use it for B3 span ids. A malformed span id no longer costs
an exception per request: setSpanId returns false and the interpreter
invalidates the context, as the exception path did.

Zero is also what a malformed id parses to, so a zero result counts as
malformed only when the input isn't all zeros; a literal zero span id is
still accepted, as before. Whether B3 should reject it, as W3C does, is a
separate question: the B3 spec does not say.

The new parser counts significant digits within the parsed range. The
throwing parser counts from the start of the string, so at a non-zero
offset its 16-digit overflow guard can miss.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh dougqh changed the title Keep malformed B3 ids out of telemetry Reject malformed B3 span ids without exceptions Nov 10, 2026
@dougqh
dougqh marked this pull request as ready for review November 10, 2026 20:36
@dougqh
dougqh requested review from a team as code owners November 10, 2026 20:36
@dougqh
dougqh requested review from AlexeyKuznetsov-DD and Mascarpone and removed request for a team November 10, 2026 20:36
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 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-10T20:39:58.067261Z d51277d 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: d51277d866

ℹ️ 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".

@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

Malformed B3 span IDs retain context-invalidation behavior without constructing parsing exceptions, while literal zero span IDs remain accepted.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit d51277d

@dougqh dougqh changed the title Reject malformed B3 span ids without exceptions Reject malformed B3 span ids without exceptions (quick fix) Nov 10, 2026
@dougqh dougqh added the tag: telemetry error reported Reported by error telemetry label Nov 10, 2026
Measures extraction with a valid, oversized or non-hex span id in
both B3 styles, so the cost of rejecting malformed ids is visible.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
dougqh added a commit that referenced this pull request Nov 12, 2026
The same LongStringUtils.parseUnsignedLongHexOrZero, with its test, as
#13355 adds on master, so the W3C traceparent parse on this stack can use
it. Drop this commit when rebasing onto a master that has #13355.

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

Copy link
Copy Markdown
Contributor

--- found by Codex

Two non-blocking edge cases I found in the span ID parser:

  1. Fullwidth zeros change which IDs B3 accepts. The old parser accepted 15 ASCII zeros followed by 0 (U+FF10) as span ID zero. The new zero check rejects that form. Conversely, 01000000000000000 was rejected before and now parses as 0x1000000000000000 because the new parser treats the leading fullwidth zero as padding. Both inputs are outside the B3 spec. Could we preserve the previous behavior or document and test the difference? B3 zero check
  2. The offset overflow test can pass even if the overflow guard fails. The current input, x10000000000000000, wraps to zero if parsed without the guard. That is the same value the test expects for rejection. Changing the input to x10000000000000001 would make a broken parser return one and fail the test. Test assertion

@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 couple of minor comments and couple of findings flagged by codex.

Count only an ASCII '0' as padding when checking for more than 16 digits, as
the throwing parse does, and accept a span id of any zero digits as zero, as
DDSpanId.fromHex did. Test both, and pick an overflow input that a parser
without the guard can't wrap to the expected zero. Also drop the quotes the
new TableTest values don't need.

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

dougqh commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, both were real. Fixed in 1796274:

  1. Fullwidth zeros: the old behavior is restored in both directions. The overflow check now counts only an ASCII '0' as padding, as parseUnsignedLongHex does, so 01000000000000000 is rejected again. The zero check accepts any string of zero digits as DDSpanId.fromHex read them, so fifteen 0s followed by 0 is a zero span id again. Both forms are now tested, in LongStringUtilsTest and in B3HttpExtractorTest, and the tests fail on the previous parser.
  2. Offset overflow test: the input is now x10000000000000001, so a parser without the guard would return 1 and fail the test.

@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

The PR avoids exceptions and telemetry for malformed B3 span IDs and adds extraction benchmarks. This update improves Unicode compatibility and overflow coverage, but one edge remains: a fullwidth zero followed by 16 ASCII zeros is accepted as span ID zero instead of invalidating the context.

Open Bits AI session

🤖 Bits Code Review · Commit 1796274

@dougqh

dougqh commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

/merge

@gh-worker-devflow-routing-ef8351

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

Copy link
Copy Markdown

View all feedbacks in Devflow UI.

2026-11-18 18:56:50 UTC ℹ️ Start processing command /merge


2026-11-18 18:56:57 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-18 21:22:28 UTC ℹ️ MergeQueue: merge request added to the queue

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


2026-11-18 22:20:18 UTC ℹ️ MergeQueue: This merge request was merged

A fullwidth zero followed by 16 ASCII zeros has 17 significant digits, which
the old parser rejected, but the zero check accepted it. Count only ASCII '0'
as padding there as well.

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

@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

The PR avoids parsing exceptions and instrumentation telemetry for malformed B3 span IDs while preserving zero-ID behavior, with extraction benchmarks included. The latest change fixes acceptance of oversized Unicode-zero IDs by enforcing the parser’s length rules; the previously reported finding is resolved, and no issues remain open.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 1922a65

@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit cae2a1b into master Nov 18, 2026
687 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the dougqh/b3-malformed-ids-skip-telemetry branch November 18, 2026 22:20
@github-actions github-actions Bot added this to the 1.70.0 milestone Nov 18, 2026
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: telemetry error reported Reported by error telemetry type: bug fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants