Skip to content

Commit 5e78d5f

Browse files
authored
Merge pull request #1279 from prometheus/beorn7/histogram
Fix bug in bucket key calculation
2 parents b8cb86a + 77e97da commit 5e78d5f

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

prometheus/histogram.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,8 @@ func (hc *histogramCounts) observe(v float64, bucket int, doSparse bool) {
639639
if frac == 0.5 {
640640
key--
641641
}
642-
div := 1 << -schema
643-
key = (key + div - 1) / div
642+
offset := (1 << -schema) - 1
643+
key = (key + offset) >> -schema
644644
}
645645
if isInf {
646646
key++

prometheus/histogram_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -500,23 +500,26 @@ func TestNativeHistogram(t *testing.T) {
500500
{
501501
name: "factor 4 results in schema -1",
502502
observations: []float64{
503+
0.0156251, 0.0625, // Bucket -2: (0.015625, 0.0625)
504+
0.1, 0.25, // Bucket -1: (0.0625, 0.25]
503505
0.5, 1, // Bucket 0: (0.25, 1]
504506
1.5, 2, 3, 3.5, // Bucket 1: (1, 4]
505507
5, 6, 7, // Bucket 2: (4, 16]
506508
33.33, // Bucket 3: (16, 64]
507509
},
508510
factor: 4,
509-
want: `sample_count:10 sample_sum:62.83 schema:-1 zero_threshold:2.938735877055719e-39 zero_count:0 positive_span:<offset:0 length:4 > positive_delta:2 positive_delta:2 positive_delta:-1 positive_delta:-2 `,
511+
want: `sample_count:14 sample_sum:63.2581251 schema:-1 zero_threshold:2.938735877055719e-39 zero_count:0 positive_span:<offset:-2 length:6 > positive_delta:2 positive_delta:0 positive_delta:0 positive_delta:2 positive_delta:-1 positive_delta:-2 `,
510512
},
511513
{
512514
name: "factor 17 results in schema -2",
513515
observations: []float64{
514-
0.5, 1, // Bucket 0: (0.0625, 1]
516+
0.0156251, 0.0625, // Bucket -1: (0.015625, 0.0625]
517+
0.1, 0.25, 0.5, 1, // Bucket 0: (0.0625, 1]
515518
1.5, 2, 3, 3.5, 5, 6, 7, // Bucket 1: (1, 16]
516519
33.33, // Bucket 2: (16, 256]
517520
},
518521
factor: 17,
519-
want: `sample_count:10 sample_sum:62.83 schema:-2 zero_threshold:2.938735877055719e-39 zero_count:0 positive_span:<offset:0 length:3 > positive_delta:2 positive_delta:5 positive_delta:-6 `,
522+
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 `,
520523
},
521524
{
522525
name: "negative buckets",

0 commit comments

Comments
 (0)