@@ -30,7 +30,18 @@ func MergeSampleStreams(output map[string]SampleStream, sampleStreams []SampleSt
30
30
stream .Samples = sliceSamples (stream .Samples , existingEndTs )
31
31
} // else there is no overlap, yay!
32
32
}
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
+ }
33
42
existing .Samples = append (existing .Samples , stream .Samples ... )
43
+ existing .Histograms = append (existing .Histograms , stream .Histograms ... )
44
+
34
45
output [metric ] = existing
35
46
}
36
47
}
@@ -55,3 +66,23 @@ func sliceSamples(samples []cortexpb.Sample, minTs int64) []cortexpb.Sample {
55
66
56
67
return samples [searchResult :]
57
68
}
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