mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-11 13:57:36 -08:00
Merge pull request #13184 from bboreham/exemplar-sort
Scraping: use slices.sort for exemplars
This commit is contained in:
commit
965e603fa7
|
@ -48,3 +48,18 @@ func (e Exemplar) Equals(e2 Exemplar) bool {
|
||||||
|
|
||||||
return e.Value == e2.Value
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -1608,17 +1607,8 @@ loop:
|
||||||
exemplars = append(exemplars, e)
|
exemplars = append(exemplars, e)
|
||||||
e = exemplar.Exemplar{} // Reset for next time round loop.
|
e = exemplar.Exemplar{} // Reset for next time round loop.
|
||||||
}
|
}
|
||||||
sort.Slice(exemplars, func(i, j int) bool {
|
// Sort so that checking for duplicates / out of order is more efficient during validation.
|
||||||
// Sort first by timestamp, then value, then labels so the checking
|
slices.SortFunc(exemplars, exemplar.Compare)
|
||||||
// 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()
|
|
||||||
})
|
|
||||||
outOfOrderExemplars := 0
|
outOfOrderExemplars := 0
|
||||||
for _, e := range exemplars {
|
for _, e := range exemplars {
|
||||||
_, exemplarErr := app.AppendExemplar(ref, lset, e)
|
_, exemplarErr := app.AppendExemplar(ref, lset, e)
|
||||||
|
|
Loading…
Reference in a new issue