Skip to content

fix(otlp): put downscaled exponential histogram counts in the right buckets - #20079

Merged
ArthurSens merged 1 commit into
mainfrom
krajo/otlp-fix-downscale-empty-bucket
Oct 21, 2026
Merged

ArthurSens merged 1 commit into
mainfrom
krajo/otlp-fix-downscale-empty-bucket

Conversation

@krajorama

Copy link
Copy Markdown
Member

When the OTLP endpoint receives an exponential histogram with a scale above 8, it downscales it to schema 8 by merging neighbouring buckets. If a merged bucket added up to zero, the next merged bucket built from two or more input buckets was split across the wrong output buckets. Examples (offset 0, one level of downscaling):

input buckets before expected
[0,0,5,3] idx1=5, idx2=3 idx1=0, idx2=8
[1,0,0,0,5,3] idx1=1, idx2=5, idx3=3 idx1=1, idx2=0, idx3=8

The count == 0 branch in convertBucketsLayout did not advance bucketIdx, so the next input bucket in the same group was treated as the start of a new group. The total count stayed correct, so validation didn't catch it and the data was silently wrong. OTel SDKs default to a max scale of 20, so downscaling is common. Without downscaling, the gap calculation made up for the stale index, so those results were correct. The bug has been there since the OTLP endpoint was added.

The fix keeps the index of the last appended bucket in a separate variable and computes gaps from it, so empty merged buckets can be skipped without losing track of position. A randomized comparison (500k inputs, scale-down 0–3, both adjustOffset modes, not committed) checked that every bucket now lands where it should. It also checked that the spans and deltas are byte-for-byte the same as before for every input the old code got right, which covers all cases without downscaling. BenchmarkConvertBucketLayout shows no significant change.

The same code exists in opentelemetry-collector-contrib/pkg/translator/prometheusremotewrite.

[BUGFIX] OTLP: Fix bucket counts being put in the wrong buckets when downscaling exponential histograms with scale above 8 that contain empty buckets.

🤖 Generated with Claude Code

…uckets

When an OTel exponential histogram with scale above 8 was downscaled and
a merged bucket summed to zero, the next merged bucket built from two or
more input buckets was split across the wrong output buckets. The empty
bucket branch in convertBucketsLayout did not advance bucketIdx, so the
next input bucket of the same group was treated as the start of a new
group. The total count was unaffected, so validation did not catch it.

Track the index of the last appended bucket separately and compute gaps
from it, so empty merged buckets are skipped without losing position.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Signed-off-by: György Krajcsovits <[email protected]>
@krajorama

Copy link
Copy Markdown
Member Author

Found while reviewing open-telemetry/opentelemetry-collector-contrib#51735

@bwplotka bwplotka left a comment

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.

Thanks!

@ArthurSens
ArthurSens merged commit d74ddb5 into main Oct 21, 2026
38 checks passed
@ArthurSens
ArthurSens deleted the krajo/otlp-fix-downscale-empty-bucket branch October 21, 2026 17:10
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