Skip to content

Commit ddc0b1d

Browse files
authored
support querying native histograms in query frontend (#5996)
Signed-off-by: Ben Ye <[email protected]>
1 parent 0cc657e commit ddc0b1d

File tree

6 files changed

+1819
-117
lines changed

6 files changed

+1819
-117
lines changed

pkg/querier/tripperware/merge.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ func MergeSampleStreams(output map[string]SampleStream, sampleStreams []SampleSt
3030
stream.Samples = sliceSamples(stream.Samples, existingEndTs)
3131
} // else there is no overlap, yay!
3232
}
33+
// Same for histograms as for samples above.
34+
if len(existing.Histograms) > 0 && len(stream.Histograms) > 0 {
35+
existingEndTs := existing.Histograms[len(existing.Histograms)-1].GetTimestampMs()
36+
if existingEndTs == stream.Histograms[0].GetTimestampMs() {
37+
stream.Histograms = stream.Histograms[1:]
38+
} else if existingEndTs > stream.Histograms[0].GetTimestampMs() {
39+
stream.Histograms = sliceHistograms(stream.Histograms, existingEndTs)
40+
}
41+
}
3342
existing.Samples = append(existing.Samples, stream.Samples...)
43+
existing.Histograms = append(existing.Histograms, stream.Histograms...)
44+
3445
output[metric] = existing
3546
}
3647
}
@@ -55,3 +66,23 @@ func sliceSamples(samples []cortexpb.Sample, minTs int64) []cortexpb.Sample {
5566

5667
return samples[searchResult:]
5768
}
69+
70+
// sliceHistogram assumes given histogram are sorted by timestamp in ascending order and
71+
// return a sub slice whose first element's is the smallest timestamp that is strictly
72+
// bigger than the given minTs. Empty slice is returned if minTs is bigger than all the
73+
// timestamps in histogram.
74+
func sliceHistograms(histograms []SampleHistogramPair, minTs int64) []SampleHistogramPair {
75+
if len(histograms) <= 0 || minTs < histograms[0].GetTimestampMs() {
76+
return histograms
77+
}
78+
79+
if len(histograms) > 0 && minTs > histograms[len(histograms)-1].GetTimestampMs() {
80+
return histograms[len(histograms):]
81+
}
82+
83+
searchResult := sort.Search(len(histograms), func(i int) bool {
84+
return histograms[i].GetTimestampMs() > minTs
85+
})
86+
87+
return histograms[searchResult:]
88+
}

0 commit comments

Comments
 (0)