Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Histogram: Fix bug with negative schemas #1054

Merged
merged 2 commits into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion prometheus/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ func (hc *histogramCounts) observe(v float64, bucket int, doSparse bool) {
if frac == 0.5 {
sparseKey--
}
sparseKey /= 1 << -sparseSchema
div := 1 << -sparseSchema
sparseKey = (sparseKey + div - 1) / div
}
switch {
case v > sparseZeroThreshold:
Expand Down
26 changes: 23 additions & 3 deletions prometheus/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ func TestBuckets(t *testing.T) {
}

got = ExponentialBucketsRange(1, 100, 10)
want = []float64{1.0, 1.6681005372000588, 2.782559402207125,
want = []float64{
1.0, 1.6681005372000588, 2.782559402207125,
4.641588833612779, 7.742636826811273, 12.915496650148842,
21.544346900318846, 35.93813663804629, 59.94842503189414,
100.00000000000007,
Expand Down Expand Up @@ -469,7 +470,6 @@ func TestHistogramExemplar(t *testing.T) {
}

func TestSparseHistogram(t *testing.T) {

scenarios := []struct {
name string
observations []float64 // With simulated interval of 1m.
Expand Down Expand Up @@ -498,6 +498,27 @@ func TestSparseHistogram(t *testing.T) {
factor: 1.2,
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 > `,
},
{
name: "factor 4 results in schema -1",
observations: []float64{
0.5, 1, // Bucket 0: (0.25, 1]
1.5, 2, 3, 3.5, // Bucket 1: (1, 4]
5, 6, 7, // Bucket 2: (4, 16]
33.33, // Bucket 3: (16, 64]
},
factor: 4,
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 > `,
},
{
name: "factor 17 results in schema -2",
observations: []float64{
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: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 > `,
},
{
name: "negative buckets",
observations: []float64{0, -1, -1.2, -1.4, -1.8, -2},
Expand Down Expand Up @@ -662,7 +683,6 @@ func TestSparseHistogram(t *testing.T) {
}
})
}

}

func TestSparseHistogramConcurrency(t *testing.T) {
Expand Down