From 15583af9bb5d7059444cccdbd06e8565ee6d36b7 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Thu, 7 Apr 2022 16:24:48 +0200 Subject: [PATCH] Histogram: Fix crash when compacting only empty buckets Signed-off-by: beorn7 --- model/histogram/float_histogram.go | 2 +- model/histogram/float_histogram_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/model/histogram/float_histogram.go b/model/histogram/float_histogram.go index 2d774c2f6..4eb3c0b2b 100644 --- a/model/histogram/float_histogram.go +++ b/model/histogram/float_histogram.go @@ -471,7 +471,7 @@ func compactBuckets(buckets []float64, spans []Span, maxEmptyBuckets int) ([]flo iSpan++ } } - if maxEmptyBuckets == 0 { + if maxEmptyBuckets == 0 || len(buckets) == 0 { return buckets, spans } diff --git a/model/histogram/float_histogram_test.go b/model/histogram/float_histogram_test.go index 8395ce27b..cc8956a9d 100644 --- a/model/histogram/float_histogram_test.go +++ b/model/histogram/float_histogram_test.go @@ -889,6 +889,22 @@ func TestFloatHistogramCompact(t *testing.T) { 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 {