Skip to content

Commit

Permalink
histogram: Fix bug in bucket key calculation
Browse files Browse the repository at this point in the history
The current code doesn't work fork negative schemas if the observed
value should go into a bucket with a non-positive key.

Signed-off-by: beorn7 <beorn@grafana.com>
  • Loading branch information
beorn7 committed May 25, 2023
1 parent 3094812 commit 77e97da
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions prometheus/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,8 +639,8 @@ func (hc *histogramCounts) observe(v float64, bucket int, doSparse bool) {
if frac == 0.5 {
key--
}
div := 1 << -schema
key = (key + div - 1) / div
offset := (1 << -schema) - 1
key = (key + offset) >> -schema
}
if isInf {
key++
Expand Down
6 changes: 3 additions & 3 deletions prometheus/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,13 @@ func TestNativeHistogram(t *testing.T) {
{
name: "factor 17 results in schema -2",
observations: []float64{
0.0156251, 0.0625, 0.1, 0.25, // Bucket -1: (0.015625, 0.25]
0.5, 1, // Bucket 0: (0.0625, 1]
0.0156251, 0.0625, // Bucket -1: (0.015625, 0.0625]
0.1, 0.25, 0.5, 1, // Bucket 0: (0.0625, 1]
1.5, 2, 3, 3.5, 5, 6, 7, // Bucket 1: (1, 16]
33.33, // Bucket 2: (16, 256]
},
factor: 17,
want: `sample_count:14 sample_sum:63.2581251 schema:-2 zero_threshold:2.938735877055719e-39 zero_count:0 positive_span:<offset:-1 length:4 > positive_delta:4 positive_delta:-2 positive_delta:5 positive_delta:-6 `,
want: `sample_count:14 sample_sum:63.2581251 schema:-2 zero_threshold:2.938735877055719e-39 zero_count:0 positive_span:<offset:-1 length:4 > positive_delta:2 positive_delta:2 positive_delta:3 positive_delta:-6 `,
},
{
name: "negative buckets",
Expand Down

0 comments on commit 77e97da

Please sign in to comment.