Skip to content

fix(metrics): reject usize::MAX as cardinality limit#3506

Merged
cijothomas merged 1 commit into
open-telemetry:mainfrom
cijothomas:cijothomas/revert-3290-validate-cardinality
May 8, 2026
Merged

fix(metrics): reject usize::MAX as cardinality limit#3506
cijothomas merged 1 commit into
open-telemetry:mainfrom
cijothomas:cijothomas/revert-3290-validate-cardinality

Conversation

@cijothomas
Copy link
Copy Markdown
Member

Reverts #3290 and instead rejects usize::MAX in StreamBuilder::build(). The original integer-overflow panic is fixed at the input boundary, keeping the SDK's no-allocation-on-hot-path design intact.

@cijothomas cijothomas requested a review from a team as a code owner May 8, 2026 19:06
@codecov
Copy link
Copy Markdown

codecov Bot commented May 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.8%. Comparing base (f744509) to head (45b647f).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #3506   +/-   ##
=====================================
  Coverage   82.8%   82.8%           
=====================================
  Files        130     130           
  Lines      27276   27282    +6     
=====================================
+ Hits       22609   22614    +5     
- Misses      4667    4668    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- These types are SDK implementation details and should be imported from `opentelemetry_sdk::trace` instead.
- Fix panics and exploding memory usage from large cardinality limit [#3290][3290]
- `StreamBuilder::build()` now rejects `usize::MAX` as a cardinality limit
with a validation error.
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.

Add this PR's link.

@cijothomas cijothomas added this pull request to the merge queue May 8, 2026
Merged via the queue into open-telemetry:main with commit 3ddb386 May 8, 2026
26 of 27 checks passed

// Test valid cardinality limits
let valid_limits = vec![1, 10, 100, 1000];
let valid_limits = vec![1, 10, 100, 1000, usize::MAX - 1];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Marking usize::MAX - 1 as valid here is misleading. It avoids the 1 + cardinality_limit overflow, but ValueMap::new() then calls HashMap::with_capacity(usize::MAX), which still fails at allocation. The PR fixes the arithmetic-overflow case correctly, but any sufficiently large limit will still fail loudly when the stream is first used.

Should we either tighten the validation to a sane upper bound, or decouple the initial HashMap capacity from the configured cardinality limit so a high limit doesn't force a huge upfront allocation?

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.

Exactly this. The fix that was reverted was not centered around the integer overflow. The fix was to avoid the huge (and wasteful) up-front allocation. By reverting my PR, a very real bug is being re-introduced.

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.

The discussion about disabling cardinality limit (modifying upfront allocation logic) is being tracked in this #3507. It would be worked upon after the 0.32 release.

@cijothomas cijothomas deleted the cijothomas/revert-3290-validate-cardinality branch May 12, 2026 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants