From f0e1b592ab10ea89a56dcabdbbf91f5d7e91cc40 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Fri, 24 Nov 2023 14:38:35 +0000 Subject: [PATCH] Scraping: use slices.sort for exemplars The sort implementation using Go generics is used everywhere else in Prometheus. Signed-off-by: Bryan Boreham --- model/exemplar/exemplar.go | 15 +++++++++++++++ scrape/scrape.go | 14 ++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/model/exemplar/exemplar.go b/model/exemplar/exemplar.go index 2e39cf6892..08f55374ef 100644 --- a/model/exemplar/exemplar.go +++ b/model/exemplar/exemplar.go @@ -48,3 +48,18 @@ func (e Exemplar) Equals(e2 Exemplar) bool { return e.Value == e2.Value } + +// Sort first by timestamp, then value, then labels. +func Compare(a, b Exemplar) int { + if a.Ts < b.Ts { + return -1 + } else if a.Ts > b.Ts { + return 1 + } + if a.Value < b.Value { + return -1 + } else if a.Value > b.Value { + return 1 + } + return labels.Compare(a.Labels, b.Labels) +} diff --git a/scrape/scrape.go b/scrape/scrape.go index 983bee8378..590eac889c 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -24,7 +24,6 @@ import ( "math" "net/http" "reflect" - "sort" "strconv" "strings" "sync" @@ -1610,17 +1609,8 @@ loop: exemplars = append(exemplars, e) e = exemplar.Exemplar{} // Reset for next time round loop. } - sort.Slice(exemplars, func(i, j int) bool { - // Sort first by timestamp, then value, then labels so the checking - // for duplicates / out of order is more efficient during validation. - if exemplars[i].Ts != exemplars[j].Ts { - return exemplars[i].Ts < exemplars[j].Ts - } - if exemplars[i].Value != exemplars[j].Value { - return exemplars[i].Value < exemplars[j].Value - } - return exemplars[i].Labels.Hash() < exemplars[j].Labels.Hash() - }) + // Sort so that checking for duplicates / out of order is more efficient during validation. + slices.SortFunc(exemplars, exemplar.Compare) outOfOrderExemplars := 0 for _, e := range exemplars { _, exemplarErr := app.AppendExemplar(ref, lset, e)