Skip to content

Commit 0c146ef

Browse files
authored
fix(queryrange): Handle native histogram responses in minTime() sort ordering for split_by_interval merge (#7555)
Signed-off-by: Paurush Garg <paurushg@amazon.com>
1 parent 976bc35 commit 0c146ef

2 files changed

Lines changed: 112 additions & 4 deletions

File tree

pkg/querier/tripperware/merge.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ func (a byFirstTime) Less(i, j int) bool { return a[i].minTime() < a[j].minTime(
2525
func (resp *PrometheusResponse) minTime() int64 {
2626
data := resp.GetData()
2727
res := data.GetResult()
28-
// minTime should only be called when the response is fron range query.
28+
// minTime should only be called when the response is from range query.
2929
matrix := res.GetMatrix()
3030
sampleStreams := matrix.GetSampleStreams()
3131
if len(sampleStreams) == 0 {
3232
return -1
3333
}
34-
if len(sampleStreams[0].Samples) == 0 {
35-
return -1
34+
if len(sampleStreams[0].Samples) > 0 {
35+
return sampleStreams[0].Samples[0].TimestampMs
36+
}
37+
if len(sampleStreams[0].Histograms) > 0 {
38+
return sampleStreams[0].Histograms[0].GetTimestampMs()
3639
}
37-
return sampleStreams[0].Samples[0].TimestampMs
40+
return -1
3841
}
3942

4043
// MergeResponse merges multiple Response into one.

pkg/querier/tripperware/merge_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,3 +681,108 @@ func Test_sortPlanForQuery(t *testing.T) {
681681
})
682682
}
683683
}
684+
685+
func TestMinTime(t *testing.T) {
686+
t.Parallel()
687+
for _, tc := range []struct {
688+
name string
689+
resp *PrometheusResponse
690+
expected int64
691+
}{
692+
{
693+
name: "empty matrix",
694+
resp: &PrometheusResponse{
695+
Data: PrometheusData{
696+
ResultType: "matrix",
697+
Result: PrometheusQueryResult{
698+
Result: &PrometheusQueryResult_Matrix{
699+
Matrix: &Matrix{SampleStreams: []SampleStream{}},
700+
},
701+
},
702+
},
703+
},
704+
expected: -1,
705+
},
706+
{
707+
name: "float samples only",
708+
resp: &PrometheusResponse{
709+
Data: PrometheusData{
710+
ResultType: "matrix",
711+
Result: PrometheusQueryResult{
712+
Result: &PrometheusQueryResult_Matrix{
713+
Matrix: &Matrix{SampleStreams: []SampleStream{
714+
{
715+
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(map[string]string{"foo": "bar"})),
716+
Samples: []cortexpb.Sample{{TimestampMs: 1000}, {TimestampMs: 2000}},
717+
},
718+
}},
719+
},
720+
},
721+
},
722+
},
723+
expected: 1000,
724+
},
725+
{
726+
name: "histograms only",
727+
resp: &PrometheusResponse{
728+
Data: PrometheusData{
729+
ResultType: "matrix",
730+
Result: PrometheusQueryResult{
731+
Result: &PrometheusQueryResult_Matrix{
732+
Matrix: &Matrix{SampleStreams: []SampleStream{
733+
{
734+
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(map[string]string{"foo": "bar"})),
735+
Histograms: []SampleHistogramPair{{TimestampMs: 3000, Histogram: testHistogram1}, {TimestampMs: 4000, Histogram: testHistogram1}},
736+
},
737+
}},
738+
},
739+
},
740+
},
741+
},
742+
expected: 3000,
743+
},
744+
{
745+
name: "both samples and histograms - samples first",
746+
resp: &PrometheusResponse{
747+
Data: PrometheusData{
748+
ResultType: "matrix",
749+
Result: PrometheusQueryResult{
750+
Result: &PrometheusQueryResult_Matrix{
751+
Matrix: &Matrix{SampleStreams: []SampleStream{
752+
{
753+
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(map[string]string{"foo": "bar"})),
754+
Samples: []cortexpb.Sample{{TimestampMs: 1000}},
755+
Histograms: []SampleHistogramPair{{TimestampMs: 5000, Histogram: testHistogram1}},
756+
},
757+
}},
758+
},
759+
},
760+
},
761+
},
762+
expected: 1000,
763+
},
764+
{
765+
name: "stream with empty samples and empty histograms",
766+
resp: &PrometheusResponse{
767+
Data: PrometheusData{
768+
ResultType: "matrix",
769+
Result: PrometheusQueryResult{
770+
Result: &PrometheusQueryResult_Matrix{
771+
Matrix: &Matrix{SampleStreams: []SampleStream{
772+
{
773+
Labels: cortexpb.FromLabelsToLabelAdapters(labels.FromMap(map[string]string{"foo": "bar"})),
774+
},
775+
}},
776+
},
777+
},
778+
},
779+
},
780+
expected: -1,
781+
},
782+
} {
783+
t.Run(tc.name, func(t *testing.T) {
784+
t.Parallel()
785+
assert.Equal(t, tc.expected, tc.resp.minTime())
786+
})
787+
}
788+
}

0 commit comments

Comments
 (0)