Skip to content

Commit eb59a7b

Browse files
authored
Histogram: Fix bug with negative schemas (#1054)
* Histogram: Expose bug with negative schema Signed-off-by: beorn7 <[email protected]> * Histogram: Fix bug with negative schemas Signed-off-by: beorn7 <[email protected]>
1 parent b237230 commit eb59a7b

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

prometheus/histogram.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,8 @@ func (hc *histogramCounts) observe(v float64, bucket int, doSparse bool) {
595595
if frac == 0.5 {
596596
sparseKey--
597597
}
598-
sparseKey /= 1 << -sparseSchema
598+
div := 1 << -sparseSchema
599+
sparseKey = (sparseKey + div - 1) / div
599600
}
600601
switch {
601602
case v > sparseZeroThreshold:

prometheus/histogram_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ func TestBuckets(t *testing.T) {
355355
}
356356

357357
got = ExponentialBucketsRange(1, 100, 10)
358-
want = []float64{1.0, 1.6681005372000588, 2.782559402207125,
358+
want = []float64{
359+
1.0, 1.6681005372000588, 2.782559402207125,
359360
4.641588833612779, 7.742636826811273, 12.915496650148842,
360361
21.544346900318846, 35.93813663804629, 59.94842503189414,
361362
100.00000000000007,
@@ -469,7 +470,6 @@ func TestHistogramExemplar(t *testing.T) {
469470
}
470471

471472
func TestSparseHistogram(t *testing.T) {
472-
473473
scenarios := []struct {
474474
name string
475475
observations []float64 // With simulated interval of 1m.
@@ -498,6 +498,27 @@ func TestSparseHistogram(t *testing.T) {
498498
factor: 1.2,
499499
want: `sample_count:6 sample_sum:7.4 sb_schema:2 sb_zero_threshold:2.938735877055719e-39 sb_zero_count:1 sb_positive:<span:<offset:0 length:5 > delta:1 delta:-1 delta:2 delta:-2 delta:2 > `,
500500
},
501+
{
502+
name: "factor 4 results in schema -1",
503+
observations: []float64{
504+
0.5, 1, // Bucket 0: (0.25, 1]
505+
1.5, 2, 3, 3.5, // Bucket 1: (1, 4]
506+
5, 6, 7, // Bucket 2: (4, 16]
507+
33.33, // Bucket 3: (16, 64]
508+
},
509+
factor: 4,
510+
want: `sample_count:10 sample_sum:62.83 sb_schema:-1 sb_zero_threshold:2.938735877055719e-39 sb_zero_count:0 sb_positive:<span:<offset:0 length:4 > delta:2 delta:2 delta:-1 delta:-2 > `,
511+
},
512+
{
513+
name: "factor 17 results in schema -2",
514+
observations: []float64{
515+
0.5, 1, // Bucket 0: (0.0625, 1]
516+
1.5, 2, 3, 3.5, 5, 6, 7, // Bucket 1: (1, 16]
517+
33.33, // Bucket 2: (16, 256]
518+
},
519+
factor: 17,
520+
want: `sample_count:10 sample_sum:62.83 sb_schema:-2 sb_zero_threshold:2.938735877055719e-39 sb_zero_count:0 sb_positive:<span:<offset:0 length:3 > delta:2 delta:5 delta:-6 > `,
521+
},
501522
{
502523
name: "negative buckets",
503524
observations: []float64{0, -1, -1.2, -1.4, -1.8, -2},
@@ -662,7 +683,6 @@ func TestSparseHistogram(t *testing.T) {
662683
}
663684
})
664685
}
665-
666686
}
667687

668688
func TestSparseHistogramConcurrency(t *testing.T) {

0 commit comments

Comments
 (0)