Merge pull request #1279 from prometheus/beorn7/histogram

Fix bug in bucket key calculation
This commit is contained in:
Björn Rabenstein 2023-05-29 00:09:18 +02:00 committed by GitHub
commit 5e78d5f66b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 5 deletions

View File

@ -639,8 +639,8 @@ func (hc *histogramCounts) observe(v float64, bucket int, doSparse bool) {
if frac == 0.5 { if frac == 0.5 {
key-- key--
} }
div := 1 << -schema offset := (1 << -schema) - 1
key = (key + div - 1) / div key = (key + offset) >> -schema
} }
if isInf { if isInf {
key++ key++

View File

@ -500,23 +500,26 @@ func TestNativeHistogram(t *testing.T) {
{ {
name: "factor 4 results in schema -1", name: "factor 4 results in schema -1",
observations: []float64{ observations: []float64{
0.0156251, 0.0625, // Bucket -2: (0.015625, 0.0625)
0.1, 0.25, // Bucket -1: (0.0625, 0.25]
0.5, 1, // Bucket 0: (0.25, 1] 0.5, 1, // Bucket 0: (0.25, 1]
1.5, 2, 3, 3.5, // Bucket 1: (1, 4] 1.5, 2, 3, 3.5, // Bucket 1: (1, 4]
5, 6, 7, // Bucket 2: (4, 16] 5, 6, 7, // Bucket 2: (4, 16]
33.33, // Bucket 3: (16, 64] 33.33, // Bucket 3: (16, 64]
}, },
factor: 4, factor: 4,
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 `, 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 `,
}, },
{ {
name: "factor 17 results in schema -2", name: "factor 17 results in schema -2",
observations: []float64{ observations: []float64{
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] 1.5, 2, 3, 3.5, 5, 6, 7, // Bucket 1: (1, 16]
33.33, // Bucket 2: (16, 256] 33.33, // Bucket 2: (16, 256]
}, },
factor: 17, factor: 17,
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 `, 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", name: "negative buckets",