@@ -349,13 +349,15 @@ func matrixMerge(resps []*PrometheusResponse) []SampleStream {
349
349
// We need to make sure we don't repeat samples. This causes some visualisations to be broken in Grafana.
350
350
// The prometheus API is inclusive of start and end timestamps.
351
351
if len (existing .Samples ) > 0 && len (stream .Samples ) > 0 {
352
- if existing .Samples [len (existing .Samples )- 1 ].TimestampMs == stream .Samples [0 ].TimestampMs {
353
- // Typically this the cases, so optimize by not doing full blown search.
352
+ existingEndTs := existing .Samples [len (existing .Samples )- 1 ].TimestampMs
353
+ if existingEndTs == stream .Samples [0 ].TimestampMs {
354
+ // Typically this the cases where only 1 sample point overlap,
355
+ // so optimize with simple code.
354
356
stream .Samples = stream .Samples [1 :]
355
- } else {
356
- existingEndTs := existing . Samples [ len ( existing . Samples ) - 1 ]. TimestampMs
357
- stream .Samples = searchFirstBiggerTimestamp ( existingEndTs , stream .Samples )
358
- }
357
+ } else if existingEndTs > stream . Samples [ 0 ]. TimestampMs {
358
+ // Overlap might be big, use heavier algorithm to remove overlap.
359
+ stream .Samples = chopOffOverlapPortion ( stream .Samples , existingEndTs )
360
+ } // else there is no overlap, yay!
359
361
}
360
362
existing .Samples = append (existing .Samples , stream .Samples ... )
361
363
output [metric ] = existing
@@ -376,11 +378,11 @@ func matrixMerge(resps []*PrometheusResponse) []SampleStream {
376
378
return result
377
379
}
378
380
379
- func searchFirstBiggerTimestamp ( targetTs int64 , samples []cortexpb.Sample ) []cortexpb.Sample {
381
+ func chopOffOverlapPortion ( samples []cortexpb.Sample , choppingPointTs int64 ) []cortexpb.Sample {
380
382
381
383
// assuming stream.Samples is sorted by by timestamp in ascending order.
382
384
searchResult := sort .Search (len (samples ), func (i int ) bool {
383
- return samples [i ].TimestampMs > targetTs
385
+ return samples [i ].TimestampMs > choppingPointTs
384
386
})
385
387
386
388
if searchResult < len (samples ) {
0 commit comments