Merge pull request #10562 from prometheus/beorn7/sparsehistogram

Histogram: Fix crash when compacting only empty buckets
This commit is contained in:
Björn Rabenstein 2022-04-07 16:52:36 +02:00 committed by GitHub
commit f009e1c8f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -471,7 +471,7 @@ func compactBuckets(buckets []float64, spans []Span, maxEmptyBuckets int) ([]flo
iSpan++ iSpan++
} }
} }
if maxEmptyBuckets == 0 { if maxEmptyBuckets == 0 || len(buckets) == 0 {
return buckets, spans return buckets, spans
} }

View File

@ -889,6 +889,22 @@ func TestFloatHistogramCompact(t *testing.T) {
NegativeBuckets: []float64{3.1, 3, 0, 0, 0, 1.234e5, 1000, 0, 3, 4}, NegativeBuckets: []float64{3.1, 3, 0, 0, 0, 1.234e5, 1000, 0, 3, 4},
}, },
}, },
{
"only empty buckets and maxEmptyBuckets greater zero",
&FloatHistogram{
PositiveSpans: []Span{{-4, 6}, {3, 3}},
PositiveBuckets: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0},
NegativeSpans: []Span{{0, 7}},
NegativeBuckets: []float64{0, 0, 0, 0, 0, 0, 0},
},
3,
&FloatHistogram{
PositiveSpans: []Span{},
PositiveBuckets: []float64{},
NegativeSpans: []Span{},
NegativeBuckets: []float64{},
},
},
} }
for _, c := range cases { for _, c := range cases {