Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [BUGFIX] Ingester: Fix labelset data race condition. #6573
* [BUGFIX] Compactor: Cleaner should not put deletion marker for blocks with no-compact marker. #6576
* [BUGFIX] Compactor: Cleaner would delete bucket index when there is no block in bucket store. #6577
* [BUGFIX] Querier: Fix marshal native histogram with empty bucket when protobuf codec is enabled. #6595

## 1.19.0 in progress

Expand Down
15 changes: 7 additions & 8 deletions pkg/querier/codec/protobuf_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,13 @@ func getMatrixSampleStreams(data *v1.QueryData) *[]tripperware.SampleStream {
if sampleStream.Histograms[j].H.ZeroCount > 0 {
bucketsLen = len(sampleStream.Histograms[j].H.NegativeBuckets) + len(sampleStream.Histograms[j].H.PositiveBuckets) + 1
}
buckets := make([]*tripperware.HistogramBucket, bucketsLen)
it := sampleStream.Histograms[j].H.AllBucketIterator()
getBuckets(buckets, it)
histograms[j] = tripperware.SampleHistogramPair{
TimestampMs: sampleStream.Histograms[j].T,
Histogram: tripperware.SampleHistogram{
Count: sampleStream.Histograms[j].H.Count,
Sum: sampleStream.Histograms[j].H.Sum,
Buckets: buckets,
Buckets: getBuckets(bucketsLen, it),
},
}
}
Expand Down Expand Up @@ -192,22 +190,21 @@ func getVectorSamples(data *v1.QueryData, cortexInternal bool) *[]tripperware.Sa
if sample.H.ZeroCount > 0 {
bucketsLen = len(sample.H.NegativeBuckets) + len(sample.H.PositiveBuckets) + 1
}
buckets := make([]*tripperware.HistogramBucket, bucketsLen)
it := sample.H.AllBucketIterator()
getBuckets(buckets, it)
vectorSamples[i].Histogram = &tripperware.SampleHistogramPair{
TimestampMs: sample.T,
Histogram: tripperware.SampleHistogram{
Count: sample.H.Count,
Sum: sample.H.Sum,
Buckets: buckets,
Buckets: getBuckets(bucketsLen, it),
},
}
}
return &vectorSamples
}

func getBuckets(bucketsList []*tripperware.HistogramBucket, it histogram.BucketIterator[float64]) {
func getBuckets(bucketsLen int, it histogram.BucketIterator[float64]) []*tripperware.HistogramBucket {
buckets := make([]*tripperware.HistogramBucket, bucketsLen)
bucketIdx := 0
for it.Next() {
bucket := it.At()
Expand All @@ -226,14 +223,16 @@ func getBuckets(bucketsList []*tripperware.HistogramBucket, it histogram.BucketI
boundaries = 0 // Inclusive only on upper end AKA left open.
}
}
bucketsList[bucketIdx] = &tripperware.HistogramBucket{
buckets[bucketIdx] = &tripperware.HistogramBucket{
Boundaries: int32(boundaries),
Lower: bucket.Lower,
Upper: bucket.Upper,
Count: bucket.Count,
}
bucketIdx += 1
}
buckets = buckets[:bucketIdx]
return buckets
}

func getStats(builtin *stats.BuiltinStats) *tripperware.PrometheusResponseSamplesStats {
Expand Down
92 changes: 92 additions & 0 deletions pkg/querier/codec/protobuf_codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ func TestProtobufCodec_Encode(t *testing.T) {
}
testProtoHistogram := cortexpb.FloatHistogramToHistogramProto(1000, testFloatHistogram)

floatHistogramWithEmptyBucket := &histogram.FloatHistogram{
CounterResetHint: 0,
Schema: 1,
ZeroThreshold: 0.01,
ZeroCount: 0,
Count: 0,
Sum: 1,
PositiveSpans: []histogram.Span{{Offset: 0, Length: 1}},
PositiveBuckets: []float64{0},
}

tests := []struct {
name string
data *v1.QueryData
Expand Down Expand Up @@ -287,6 +298,47 @@ func TestProtobufCodec_Encode(t *testing.T) {
},
},
},
{
name: "matrix with histogram and not cortex internal, empty bucket",
data: &v1.QueryData{
ResultType: parser.ValueTypeMatrix,
Result: promql.Matrix{
promql.Series{
Histograms: []promql.HPoint{{H: floatHistogramWithEmptyBucket, T: 1000}},
Metric: labels.FromStrings("__name__", "foo"),
},
},
},
expected: &tripperware.PrometheusResponse{
Status: tripperware.StatusSuccess,
Data: tripperware.PrometheusData{
ResultType: model.ValMatrix.String(),
Result: tripperware.PrometheusQueryResult{
Result: &tripperware.PrometheusQueryResult_Matrix{
Matrix: &tripperware.Matrix{
SampleStreams: []tripperware.SampleStream{
{
Labels: []cortexpb.LabelAdapter{
{Name: "__name__", Value: "foo"},
},
Histograms: []tripperware.SampleHistogramPair{
{
TimestampMs: 1000,
Histogram: tripperware.SampleHistogram{
Count: 0,
Sum: 1,
Buckets: []*tripperware.HistogramBucket{},
},
},
},
},
},
},
},
},
},
},
},
{
name: "vector with histogram and not cortex internal",
data: &v1.QueryData{
Expand Down Expand Up @@ -376,6 +428,46 @@ func TestProtobufCodec_Encode(t *testing.T) {
},
},
},
{
name: "vector with histogram with and not cortex internal, empty bucket",
data: &v1.QueryData{
ResultType: parser.ValueTypeVector,
Result: promql.Vector{
promql.Sample{
Metric: labels.FromStrings("__name__", "foo"),
T: 1000,
H: floatHistogramWithEmptyBucket,
},
},
},
expected: &tripperware.PrometheusResponse{
Status: tripperware.StatusSuccess,
Data: tripperware.PrometheusData{
ResultType: model.ValVector.String(),
Result: tripperware.PrometheusQueryResult{
Result: &tripperware.PrometheusQueryResult_Vector{
Vector: &tripperware.Vector{
Samples: []tripperware.Sample{
{
Labels: []cortexpb.LabelAdapter{
{Name: "__name__", Value: "foo"},
},
Histogram: &tripperware.SampleHistogramPair{
TimestampMs: 1000,
Histogram: tripperware.SampleHistogram{
Count: 0,
Sum: 1,
Buckets: []*tripperware.HistogramBucket{},
},
},
},
},
},
},
},
},
},
},
{
name: "vector with histogram and cortex internal",
cortexInternal: true,
Expand Down
Loading