diff --git a/cmd/promtool/tsdb.go b/cmd/promtool/tsdb.go index b7fad5fe0..844a387c4 100644 --- a/cmd/promtool/tsdb.go +++ b/cmd/promtool/tsdb.go @@ -23,7 +23,6 @@ import ( "path/filepath" "runtime" "runtime/pprof" - "sort" "strconv" "strings" "sync" @@ -34,6 +33,7 @@ import ( "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/index" + "golang.org/x/exp/slices" "github.com/alecthomas/units" "github.com/go-kit/log" @@ -447,7 +447,7 @@ func analyzeBlock(path, blockID string, limit int, runExtended bool) error { postingInfos := []postingInfo{} printInfo := func(postingInfos []postingInfo) { - sort.Slice(postingInfos, func(i, j int) bool { return postingInfos[i].metric > postingInfos[j].metric }) + slices.SortFunc(postingInfos, func(a, b postingInfo) bool { return a.metric > b.metric }) for i, pc := range postingInfos { if i >= limit { diff --git a/rules/manager.go b/rules/manager.go index 31c90e9e9..4f848a74b 100644 --- a/rules/manager.go +++ b/rules/manager.go @@ -19,7 +19,6 @@ import ( "fmt" "math" "net/url" - "sort" "sync" "time" @@ -30,6 +29,7 @@ import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" + "golang.org/x/exp/slices" "github.com/prometheus/prometheus/model/labels" "github.com/prometheus/prometheus/model/rulefmt" @@ -490,10 +490,9 @@ func (g *Group) AlertingRules() []*AlertingRule { alerts = append(alerts, alertingRule) } } - sort.Slice(alerts, func(i, j int) bool { - return alerts[i].State() > alerts[j].State() || - (alerts[i].State() == alerts[j].State() && - alerts[i].Name() < alerts[j].Name()) + slices.SortFunc(alerts, func(a, b *AlertingRule) bool { + return a.State() > b.State() || + (a.State() == b.State() && a.Name() < b.Name()) }) return alerts } @@ -1189,11 +1188,11 @@ func (m *Manager) RuleGroups() []*Group { rgs = append(rgs, g) } - sort.Slice(rgs, func(i, j int) bool { - if rgs[i].file != rgs[j].file { - return rgs[i].file < rgs[j].file + slices.SortFunc(rgs, func(a, b *Group) bool { + if a.file != b.file { + return a.file < b.file } - return rgs[i].name < rgs[j].name + return a.name < b.name }) return rgs diff --git a/scrape/scrape.go b/scrape/scrape.go index 8c4cc51e7..ec65c5ad9 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -23,7 +23,6 @@ import ( "math" "net/http" "reflect" - "sort" "strconv" "sync" "time" @@ -35,6 +34,7 @@ import ( config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/common/version" + "golang.org/x/exp/slices" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery/targetgroup" @@ -720,8 +720,8 @@ func mutateSampleLabels(lset labels.Labels, target *Target, honor bool, rc []*re } func resolveConflictingExposedLabels(lb *labels.Builder, conflictingExposedLabels []labels.Label) { - sort.SliceStable(conflictingExposedLabels, func(i, j int) bool { - return len(conflictingExposedLabels[i].Name) < len(conflictingExposedLabels[j].Name) + slices.SortStableFunc(conflictingExposedLabels, func(a, b labels.Label) bool { + return len(a.Name) < len(b.Name) }) for _, l := range conflictingExposedLabels { diff --git a/storage/remote/read_handler.go b/storage/remote/read_handler.go index 116eb9596..aca4d7dd5 100644 --- a/storage/remote/read_handler.go +++ b/storage/remote/read_handler.go @@ -16,12 +16,12 @@ package remote import ( "context" "net/http" - "sort" "sync" "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" + "golang.org/x/exp/slices" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/model/labels" @@ -92,8 +92,8 @@ func (h *readHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { Value: value, }) } - sort.Slice(sortedExternalLabels, func(i, j int) bool { - return sortedExternalLabels[i].Name < sortedExternalLabels[j].Name + slices.SortFunc(sortedExternalLabels, func(a, b prompb.Label) bool { + return a.Name < b.Name }) responseType, err := NegotiateResponseType(req.AcceptedResponseTypes) diff --git a/tsdb/compact.go b/tsdb/compact.go index e2b6f4c5e..0d42f627f 100644 --- a/tsdb/compact.go +++ b/tsdb/compact.go @@ -20,7 +20,6 @@ import ( "io" "os" "path/filepath" - "sort" "time" "github.com/go-kit/log" @@ -28,6 +27,7 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "golang.org/x/exp/slices" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb/chunkenc" @@ -200,8 +200,8 @@ func (c *LeveledCompactor) Plan(dir string) ([]string, error) { } func (c *LeveledCompactor) plan(dms []dirMeta) ([]string, error) { - sort.Slice(dms, func(i, j int) bool { - return dms[i].meta.MinTime < dms[j].meta.MinTime + slices.SortFunc(dms, func(a, b dirMeta) bool { + return a.meta.MinTime < b.meta.MinTime }) res := c.selectOverlappingDirs(dms) @@ -380,8 +380,8 @@ func CompactBlockMetas(uid ulid.ULID, blocks ...*BlockMeta) *BlockMeta { for s := range sources { res.Compaction.Sources = append(res.Compaction.Sources, s) } - sort.Slice(res.Compaction.Sources, func(i, j int) bool { - return res.Compaction.Sources[i].Compare(res.Compaction.Sources[j]) < 0 + slices.SortFunc(res.Compaction.Sources, func(a, b ulid.ULID) bool { + return a.Compare(b) < 0 }) res.MinTime = mint diff --git a/tsdb/db.go b/tsdb/db.go index 32dae57a5..62359a737 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -22,7 +22,6 @@ import ( "math" "os" "path/filepath" - "sort" "strconv" "strings" "sync" @@ -34,6 +33,7 @@ import ( "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "go.uber.org/atomic" + "golang.org/x/exp/slices" "golang.org/x/sync/errgroup" "github.com/prometheus/prometheus/config" @@ -579,8 +579,8 @@ func (db *DBReadOnly) Blocks() ([]BlockReader, error) { return nil, nil } - sort.Slice(loadable, func(i, j int) bool { - return loadable[i].Meta().MinTime < loadable[j].Meta().MinTime + slices.SortFunc(loadable, func(a, b *Block) bool { + return a.Meta().MinTime < b.Meta().MinTime }) blockMetas := make([]BlockMeta, 0, len(loadable)) @@ -1445,8 +1445,8 @@ func (db *DB) reloadBlocks() (err error) { } db.metrics.blocksBytes.Set(float64(blocksSize)) - sort.Slice(toLoad, func(i, j int) bool { - return toLoad[i].Meta().MinTime < toLoad[j].Meta().MinTime + slices.SortFunc(toLoad, func(a, b *Block) bool { + return a.Meta().MinTime < b.Meta().MinTime }) // Swap new blocks first for subsequently created readers to be seen. @@ -1515,8 +1515,8 @@ func deletableBlocks(db *DB, blocks []*Block) map[ulid.ULID]struct{} { // Sort the blocks by time - newest to oldest (largest to smallest timestamp). // This ensures that the retentions will remove the oldest blocks. - sort.Slice(blocks, func(i, j int) bool { - return blocks[i].Meta().MaxTime > blocks[j].Meta().MaxTime + slices.SortFunc(blocks, func(a, b *Block) bool { + return a.Meta().MaxTime > b.Meta().MaxTime }) for _, block := range blocks { diff --git a/tsdb/exemplar.go b/tsdb/exemplar.go index 01718bb57..bf401da3c 100644 --- a/tsdb/exemplar.go +++ b/tsdb/exemplar.go @@ -15,11 +15,11 @@ package tsdb import ( "context" - "sort" "sync" "unicode/utf8" "github.com/prometheus/client_golang/prometheus" + "golang.org/x/exp/slices" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/model/exemplar" @@ -185,8 +185,8 @@ func (ce *CircularExemplarStorage) Select(start, end int64, matchers ...[]*label } } - sort.Slice(ret, func(i, j int) bool { - return labels.Compare(ret[i].SeriesLabels, ret[j].SeriesLabels) < 0 + slices.SortFunc(ret, func(a, b exemplar.QueryResult) bool { + return labels.Compare(a.SeriesLabels, b.SeriesLabels) < 0 }) return ret, nil diff --git a/tsdb/head_read.go b/tsdb/head_read.go index 0e6e005ea..c7146026a 100644 --- a/tsdb/head_read.go +++ b/tsdb/head_read.go @@ -137,8 +137,8 @@ func (h *headIndexReader) SortedPostings(p index.Postings) index.Postings { return index.ErrPostings(errors.Wrap(err, "expand postings")) } - sort.Slice(series, func(i, j int) bool { - return labels.Compare(series[i].lset, series[j].lset) < 0 + slices.SortFunc(series, func(a, b *memSeries) bool { + return labels.Compare(a.lset, b.lset) < 0 }) // Convert back to list. diff --git a/tsdb/index/postings.go b/tsdb/index/postings.go index 2ac6edbdc..3be8a1997 100644 --- a/tsdb/index/postings.go +++ b/tsdb/index/postings.go @@ -107,11 +107,11 @@ func (p *MemPostings) SortedKeys() []labels.Label { } p.mtx.RUnlock() - sort.Slice(keys, func(i, j int) bool { - if keys[i].Name != keys[j].Name { - return keys[i].Name < keys[j].Name + slices.SortFunc(keys, func(a, b labels.Label) bool { + if a.Name != b.Name { + return a.Name < b.Name } - return keys[i].Value < keys[j].Value + return a.Value < b.Value }) return keys } diff --git a/tsdb/index/postingsstats.go b/tsdb/index/postingsstats.go index 6b29bddab..8e5f62dba 100644 --- a/tsdb/index/postingsstats.go +++ b/tsdb/index/postingsstats.go @@ -15,7 +15,8 @@ package index import ( "math" - "sort" + + "golang.org/x/exp/slices" ) // Stat holds values for a single cardinality statistic. @@ -62,8 +63,8 @@ func (m *maxHeap) push(item Stat) { } func (m *maxHeap) get() []Stat { - sort.Slice(m.Items, func(i, j int) bool { - return m.Items[i].Count > m.Items[j].Count + slices.SortFunc(m.Items, func(a, b Stat) bool { + return a.Count > b.Count }) return m.Items } diff --git a/tsdb/wlog/checkpoint.go b/tsdb/wlog/checkpoint.go index 42b03f48f..fe9952a30 100644 --- a/tsdb/wlog/checkpoint.go +++ b/tsdb/wlog/checkpoint.go @@ -20,13 +20,13 @@ import ( "math" "os" "path/filepath" - "sort" "strconv" "strings" "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/pkg/errors" + "golang.org/x/exp/slices" "github.com/prometheus/prometheus/tsdb/chunks" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" @@ -374,8 +374,8 @@ func listCheckpoints(dir string) (refs []checkpointRef, err error) { refs = append(refs, checkpointRef{name: fi.Name(), index: idx}) } - sort.Slice(refs, func(i, j int) bool { - return refs[i].index < refs[j].index + slices.SortFunc(refs, func(a, b checkpointRef) bool { + return a.index < b.index }) return refs, nil diff --git a/tsdb/wlog/wlog.go b/tsdb/wlog/wlog.go index e38cb94cb..b7b1623f9 100644 --- a/tsdb/wlog/wlog.go +++ b/tsdb/wlog/wlog.go @@ -22,7 +22,6 @@ import ( "io" "os" "path/filepath" - "sort" "strconv" "sync" "time" @@ -32,6 +31,7 @@ import ( "github.com/golang/snappy" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "golang.org/x/exp/slices" "github.com/prometheus/prometheus/tsdb/fileutil" ) @@ -859,8 +859,8 @@ func listSegments(dir string) (refs []segmentRef, err error) { } refs = append(refs, segmentRef{name: fn, index: k}) } - sort.Slice(refs, func(i, j int) bool { - return refs[i].index < refs[j].index + slices.SortFunc(refs, func(a, b segmentRef) bool { + return a.index < b.index }) for i := 0; i < len(refs)-1; i++ { if refs[i].index+1 != refs[i+1].index {