Skip to content

Size client stats cardinality tables to the values seen, not the budget - #13508

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 7 commits into
masterfrom
dougqh/cardinality-handler-lazy
Nov 20, 2026
Merged

gh-worker-dd-mergequeue-cf854d[bot] merged 7 commits into
masterfrom
dougqh/cardinality-handler-lazy

Conversation

@dougqh

@dougqh dougqh commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What Does This Do

Client-side stats cardinality handlers now allocate their lookup tables only once they see a value, and size them to the number of values actually seen rather than to the full cardinality budget. A peer tag the service never emits no longer costs any table memory. A tag with a handful of values holds a small table instead of one sized for 512.

Motivation

Each handler allocated its current-cycle and prior-cycle tables up front at twice its budget. For peer tags (limit 512), that is four 1,024-slot arrays per handler, about 16 KB each. The tracer builds one handler per peer tag the agent advertises: 43 today, across all tracer languages.

A Java service sets only a few of those tags. About 19 of the 43 are never set by any Java instrumentation (for example active_record.db.vendor, msmq.queue.path and mongodb.db), and a typical service touches 5–15 of them. The values it does set are usually low-cardinality: a few database hosts, a few downstream services.

A quiet-phase heap dump of PetClinic at a 128m heap showed the client stats aggregator retaining about 700 KB, almost all of it these mostly empty tables. That cost is permanent: it is paid on every JVM with client-side stats, whether or not the tags ever appear.

Additional Notes

  • Lazy and grow-to-fit. Tables are null until the first value, start at 8 slots and double up to the previous fixed size. The budget and the 0.5 load factor are unchanged, so a handler at its full budget ends up exactly where it was before.

  • Reset resizes. Each reset sizes the next cycle's table for as many values as the completed cycle held. It reuses the older table's array when that is already the right size, so steady traffic does not allocate. A cycle with no values releases the tables entirely.

  • Separate masks. The current and prior tables can now differ in size, so each is probed with its own mask. Reusing an encoded value from the prior cycle still works across sizes, and a test covers it.

  • Both handlers change. PropertyCardinalityHandler and TagCardinalityHandler get the same change, which keeps their table code parallel. The property handlers (resource, service, span.kind, …) always see values, so for them only grow-to-fit matters, mostly for low-cardinality fields like span.kind and http.method.

  • Cost on the aggregator thread. register() gains a null check and a growth branch. Growth is amortized: a high-cardinality field like resource reallocates a few times in its first cycle, and later cycles start at the right size.

  • Measured (before/after heap dumps):

    Heap dumps of PetClinic at a 128m heap with the default v0.4 protocol. Each was taken at about 390 s of uptime, after 150 s of owners-query load, so past the agent's 5-minute type-pool clear. Retained sizes are MAT dominator-tree sizes, one dump per jar:

    Jar ClientStatsAggregator retained
    master 697 KB
    master + Serialize client stats payloads into a chunked buffer #13097 697 KB
    master + Serialize client stats payloads into a chunked buffer #13097 + Let an idle trace writer release its payload buffer's surplus chunks #13507 + this PR 7 KB

    Serialize client stats payloads into a chunked buffer #13097 and Let an idle trace writer release its payload buffer's surplus chunks #13507 don't touch the cardinality handlers, so the whole 697 KB → 7 KB drop comes from this PR.

🤖 Generated with Claude Code

dougqh and others added 2 commits November 17, 2026 15:57
Each cardinality handler allocated its current and prior tables up front
at twice its budget. A peer-tag handler (limit 512) held four
1,024-slot arrays, about 16 KB, and the tracer builds one per peer tag
the agent advertises (43 today, across all languages). Most never see a
value in a Java service, so a quiet-phase heap dump of PetClinic showed
the client stats aggregator retaining about 700 KB of empty tables.

Tables are now null until a handler's first value, start at 8 slots and
double up to the old size, so the budget and the 0.5 load factor are
unchanged. Each reset sizes the next cycle's table for as many values as
the completed cycle held, reusing the older table when it's already that
size, and a cycle with no values releases the tables. The current and
prior tables can now differ in size, so each is probed with its own mask.
Lookups and budget behavior, including prior-cycle reuse, are unchanged.

Both PropertyCardinalityHandler and TagCardinalityHandler get the same
change, keeping their table code parallel.

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

Covers no table before the first value, growth that keeps every value
findable, growth stopping at the budget, reset sizing the next table for
the last cycle and releasing it after an empty cycle, and prior-cycle
reuse when the current and prior tables differ in size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh dougqh added type: feature Enhancements and improvements comp: core Tracer core tag: performance Performance related changes tag: ai generated Largely based on code generated by an AI or LLM labels Nov 17, 2026
dougqh and others added 4 commits November 17, 2026 16:01
…ister

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

Both Javadocs still said the hash gives the initial probe slot in both
tables, which stopped being true once the current and prior tables could
differ in size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both handlers carried the same minimum size, budget-sized maximum and
0.5-load-factor capacity math. The policy now lives in one place; the
probe code stays parallel in each handler because their tables hold
different key types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh
dougqh marked this pull request as ready for review November 17, 2026 21:14
@dougqh
dougqh requested a review from a team as a code owner November 17, 2026 21:14
@dougqh
dougqh requested review from jordan-wong and removed request for a team November 17, 2026 21:14
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 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-17T21:18:54.913777Z 96f9924 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: 96f9924d1b

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

Bits Code Review: FAIL

Growing a property cardinality table can make previously accepted CharSequence values unfindable, causing repeated resource or operation names to be replaced by tracer_blocked_value.

Open Bits AI session

🤖 Bits Code Review · Commit 96f9924

@datadog-datadog-prod-us1-2

datadog-datadog-prod-us1-2 Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Code Coverage (details)
• Patch Coverage: 94.53%
• Overall Coverage: 60.51% (-0.02%)

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

@dd-octo-sts

dd-octo-sts Bot commented Nov 17, 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.10 s 14.04 s [-0.4%; +1.3%] (no difference)
startup:insecure-bank:tracing:Agent 12.93 s 13.00 s [-1.3%; +0.1%] (no difference)
startup:petclinic:appsec:Agent 17.27 s 17.06 s [+0.2%; +2.2%] (maybe worse)
startup:petclinic:iast:Agent 16.89 s 17.16 s [-2.5%; -0.7%] (maybe better)
startup:petclinic:profiling:Agent 16.22 s 16.77 s [-7.5%; +0.9%] (no difference)
startup:petclinic:sca:Agent 17.17 s 17.13 s [-0.7%; +1.1%] (no difference)
startup:petclinic:tracing:Agent 16.27 s 16.40 s [-1.7%; +0.1%] (no difference)

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

@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.

Pre-approving. With couple of minor notes and questions and one minor issue found by Codex.

Lookups probed with the incoming value's own hash, while growth rehashed
stored values by their String hash, so a StringBuilder or CharBuffer that
doesn't hash by content couldn't be found after the table grew: it used up
budget again or came back blocked. Hash every value by content. Also test
CardinalityTables' sizing corner cases, rename freeSlot to findFreeSlot,
update Config's cardinality-limit Javadocs for lazy tables, and explain the
double reset in a test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dougqh
dougqh requested a review from a team as a code owner November 20, 2026 14:08

@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.

Bits Code Review: PASS

More details

Lazy table growth and cycle resets preserve cardinality limits and prior-cycle value reuse, including when table sizes differ. No actionable defects remained in the reviewed revision.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Bits Code Review · Commit 18cd193

@dougqh

dougqh commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Time and allocation before/after, to go with the retained-memory numbers above.

Base 302cb208d0 (merge base), head 18cd193a4d. Temurin 17.0.20.1, -f 3 -wi 3 -w 10s -i 5 -r 10s -prof gc, 8 threads, on a 5-vCPU box. Errors are 99.9% CIs.

Benchmark Base ops/s Head ops/s Δ Base B/op Head B/op Δ
HighCardinalityResourceMetrics 17.01M ± 0.80M 18.77M ± 1.11M +10.3% (CIs overlap) 326.9 326.0 −0.3%
HighCardinalityPeerMetrics 18.90M ± 0.57M 18.96M ± 1.36M +0.3% 326.2 325.7 −0.2%

No cost on the app threads: throughput is no worse and allocation is flat.

These benchmarks time the publish on the app thread, and the inbox stays full. Both sides drop on the same order, about 1.2–1.5B inbox-full drops per fork, with no aggregate drops. Dropping there is intended, since it keeps the app thread's cost down. But the scores are publish attempts, not metrics processed, so they say little about the handler work on the background thread. The retained-memory saving above covers that side.

@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:53:47 UTC ℹ️ Start processing command /merge


2026-11-20 14:54:00 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 15:18:24 UTC ℹ️ MergeQueue: merge request added to the queue

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


2026-11-20 16:23:55 UTC ℹ️ MergeQueue: This merge request was merged

@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot merged commit e9ad6fb into master Nov 20, 2026
687 checks passed
@gh-worker-dd-mergequeue-cf854d
gh-worker-dd-mergequeue-cf854d Bot deleted the dougqh/cardinality-handler-lazy branch November 20, 2026 16:23
@github-actions github-actions Bot added this to the 1.70.0 milestone Nov 20, 2026
@dougqh

dougqh commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

For the record, the third pair, ClientStatsAggregatorBenchmark (same settings, 1 thread), was flat: 0.6759 ± 0.0048 µs/op base vs 0.6767 ± 0.0066 head (+0.1%), with allocation about zero on both sides. Like the other two, it measures the producer with a full inbox, since this benchmark never starts the aggregator worker.

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: performance Performance related changes type: feature Enhancements and improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants