diff --git a/model/histogram/float_histogram.go b/model/histogram/float_histogram.go index 4eb3c0b2b..475eca013 100644 --- a/model/histogram/float_histogram.go +++ b/model/histogram/float_histogram.go @@ -430,7 +430,11 @@ func compactBuckets(buckets []float64, spans []Span, maxEmptyBuckets int) ([]flo // Start of span. if nEmpty == int(spans[iSpan].Length) { // The whole span is empty. + offset := spans[iSpan].Offset spans = append(spans[:iSpan], spans[iSpan+1:]...) + if len(spans) > iSpan { + spans[iSpan].Offset += offset + int32(nEmpty) + } continue } spans[iSpan].Length -= uint32(nEmpty) diff --git a/model/histogram/float_histogram_test.go b/model/histogram/float_histogram_test.go index cc8956a9d..d4e9f8cc5 100644 --- a/model/histogram/float_histogram_test.go +++ b/model/histogram/float_histogram_test.go @@ -794,7 +794,7 @@ func TestFloatHistogramCompact(t *testing.T) { }, }, { - "cut empty buckets at start or end of chunks, even in the middle", + "cut empty buckets at start or end of spans, even in the middle", &FloatHistogram{ PositiveSpans: []Span{{-4, 6}, {3, 6}}, PositiveBuckets: []float64{0, 0, 1, 3.3, 0, 0, 4.2, 0.1, 3.3, 0, 0, 0}, @@ -826,7 +826,7 @@ func TestFloatHistogramCompact(t *testing.T) { }, }, { - "cut empty buckets from the middle of a chunk", + "cut empty buckets from the middle of a span", &FloatHistogram{ PositiveSpans: []Span{{-4, 6}, {3, 3}}, PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3}, @@ -842,7 +842,19 @@ func TestFloatHistogramCompact(t *testing.T) { }, }, { - "cut empty buckets from the middle of a chunk, avoiding some due to maxEmptyBuckets", + "cut out a span containing only empty buckets", + &FloatHistogram{ + PositiveSpans: []Span{{-4, 3}, {2, 2}, {3, 4}}, + PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3}, + }, + 0, + &FloatHistogram{ + PositiveSpans: []Span{{-2, 1}, {7, 4}}, + PositiveBuckets: []float64{1, 3.3, 4.2, 0.1, 3.3}, + }, + }, + { + "cut empty buckets from the middle of a span, avoiding some due to maxEmptyBuckets", &FloatHistogram{ PositiveSpans: []Span{{-4, 6}, {3, 3}}, PositiveBuckets: []float64{0, 0, 1, 0, 0, 3.3, 4.2, 0.1, 3.3}, @@ -905,6 +917,22 @@ func TestFloatHistogramCompact(t *testing.T) { NegativeBuckets: []float64{}, }, }, + { + "multiple spans of only empty buckets", + &FloatHistogram{ + PositiveSpans: []Span{{-10, 2}, {2, 1}, {3, 3}}, + PositiveBuckets: []float64{0, 0, 0, 0, 2, 3}, + NegativeSpans: []Span{{-10, 2}, {2, 1}, {3, 3}}, + NegativeBuckets: []float64{2, 3, 0, 0, 0, 0}, + }, + 0, + &FloatHistogram{ + PositiveSpans: []Span{{-1, 2}}, + PositiveBuckets: []float64{2, 3}, + NegativeSpans: []Span{{-10, 2}}, + NegativeBuckets: []float64{2, 3}, + }, + }, } for _, c := range cases {