mergeSampleStreams deduplicates sample streams using a map.
(output map[string]SampleStream, sampleStreams []SampleStream)
| 343 | |
| 344 | // mergeSampleStreams deduplicates sample streams using a map. |
| 345 | func mergeSampleStreams(output map[string]SampleStream, sampleStreams []SampleStream) { |
| 346 | buf := make([]byte, 0, 1024) |
| 347 | for _, stream := range sampleStreams { |
| 348 | metric := string(cortexpb.FromLabelAdaptersToLabels(stream.Labels).Bytes(buf)) |
| 349 | existing, ok := output[metric] |
| 350 | if !ok { |
| 351 | existing = SampleStream{ |
| 352 | Labels: stream.Labels, |
| 353 | } |
| 354 | } |
| 355 | // We need to make sure we don't repeat samples. This causes some visualisations to be broken in Grafana. |
| 356 | // The prometheus API is inclusive of start and end timestamps. |
| 357 | if len(existing.Samples) > 0 && len(stream.Samples) > 0 { |
| 358 | existingEndTs := existing.Samples[len(existing.Samples)-1].TimestampMs |
| 359 | if existingEndTs == stream.Samples[0].TimestampMs { |
| 360 | // Typically this the cases where only 1 sample point overlap, |
| 361 | // so optimize with simple code. |
| 362 | stream.Samples = stream.Samples[1:] |
| 363 | } else if existingEndTs > stream.Samples[0].TimestampMs { |
| 364 | // Overlap might be big, use heavier algorithm to remove overlap. |
| 365 | stream.Samples = sliceSamples(stream.Samples, existingEndTs) |
| 366 | } // else there is no overlap, yay! |
| 367 | } |
| 368 | // Same for histograms as for samples above. |
| 369 | if len(existing.Histograms) > 0 && len(stream.Histograms) > 0 { |
| 370 | existingEndTs := existing.Histograms[len(existing.Histograms)-1].GetTimestampMs() |
| 371 | if existingEndTs == stream.Histograms[0].GetTimestampMs() { |
| 372 | stream.Histograms = stream.Histograms[1:] |
| 373 | } else if existingEndTs > stream.Histograms[0].GetTimestampMs() { |
| 374 | stream.Histograms = sliceHistograms(stream.Histograms, existingEndTs) |
| 375 | } |
| 376 | } |
| 377 | existing.Samples = append(existing.Samples, stream.Samples...) |
| 378 | existing.Histograms = append(existing.Histograms, stream.Histograms...) |
| 379 | |
| 380 | output[metric] = existing |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // sliceSamples assumes given samples are sorted by timestamp in ascending order and |
| 385 | // return a sub slice whose first element's is the smallest timestamp that is strictly |