Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ linters:
desc: "Use github.com/klauspost/compress instead of zlib"
- pkg: "golang.org/x/exp/slices"
desc: "Use 'slices' instead."
- pkg: "sort"
desc: "Use 'slices' instead."
errcheck:
exclude-functions:
# Don't flag lines such as "io.Copy(io.Discard, resp.Body)".
Expand Down
14 changes: 8 additions & 6 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ type closeIdler interface {
type TLSVersion uint16

var TLSVersions = map[string]TLSVersion{
"TLS13": (TLSVersion)(tls.VersionTLS13),
"TLS12": (TLSVersion)(tls.VersionTLS12),
"TLS11": (TLSVersion)(tls.VersionTLS11),
"TLS10": (TLSVersion)(tls.VersionTLS10),
"TLS13": TLSVersion(tls.VersionTLS13),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not covered by the PR title.

"TLS12": TLSVersion(tls.VersionTLS12),
"TLS11": TLSVersion(tls.VersionTLS11),
"TLS10": TLSVersion(tls.VersionTLS10),
}

func (tv *TLSVersion) UnmarshalYAML(unmarshal func(any) error) error {
Expand Down Expand Up @@ -639,11 +639,13 @@ func NewRoundTripperFromConfigWithContext(ctx context.Context, cfg HTTPClientCon
dialContext = conntrack.NewDialContextFunc(
conntrack.DialWithDialContextFunc((func(context.Context, string, string) (net.Conn, error))(opts.dialContextFunc)),
conntrack.DialWithTracing(),
conntrack.DialWithName(name))
conntrack.DialWithName(name),
)
Comment on lines +642 to +643

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spurious change

} else {
dialContext = conntrack.NewDialContextFunc(
conntrack.DialWithTracing(),
conntrack.DialWithName(name))
conntrack.DialWithName(name),
)
}

newRT := func(tlsConfig *tls.Config) (http.RoundTripper, error) {
Expand Down
2 changes: 1 addition & 1 deletion config/tls_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestInvalidTLSConfig(t *testing.T) {
}

func TestTLSVersionStringer(t *testing.T) {
s := (TLSVersion)(tls.VersionTLS13)
s := TLSVersion(tls.VersionTLS13)
require.Equalf(t, "TLS13", s.String(), "tls.VersionTLS13 string should be TLS13, got %s", s.String())
}

Expand Down
10 changes: 5 additions & 5 deletions expfmt/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"net/http"
"os"
"reflect"
"sort"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -99,8 +99,8 @@ mf2 4
require.NoError(t, err)
all = append(all, smpls...)
}
sort.Sort(all)
sort.Sort(out)
slices.SortFunc(all, (*model.Sample).Compare)
slices.SortFunc(out, (*model.Sample).Compare)
require.Truef(t, reflect.DeepEqual(all, out), "output does not match")
}

Expand Down Expand Up @@ -399,8 +399,8 @@ func TestProtoDecoder(t *testing.T) {
require.NoError(t, err)
all = append(all, smpls...)
}
sort.Sort(all)
sort.Sort(scenario.expected)
slices.SortFunc(all, (*model.Sample).Compare)
slices.SortFunc(scenario.expected, (*model.Sample).Compare)
require.Truef(t, reflect.DeepEqual(all, scenario.expected), "%d. output does not match, want: %#v, got %#v", i, scenario.expected, all)
}
}
Expand Down
28 changes: 18 additions & 10 deletions model/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,25 @@ func (a *Alert) Validate() error {
// Alert is a list of alerts that can be sorted in chronological order.
type Alerts []*Alert

func (as Alerts) Len() int { return len(as) }
func (as Alerts) Swap(i, j int) { as[i], as[j] = as[j], as[i] }

func (as Alerts) Less(i, j int) bool {
if as[i].StartsAt.Before(as[j].StartsAt) {
return true
}
if as[i].EndsAt.Before(as[j].EndsAt) {
return true
// Compare implements the cmp.Comparator interface for Alert pointers.
// It compares by StartsAt, then EndsAt, then Fingerprint.
Comment on lines +113 to +114
func (a *Alert) Compare(o *Alert) int {
switch {
case a.StartsAt.Before(o.StartsAt):
return -1
case o.StartsAt.Before(a.StartsAt):
return 1
case a.EndsAt.Before(o.EndsAt):
return -1
case o.EndsAt.Before(a.EndsAt):
return 1
case a.Fingerprint() < o.Fingerprint():
return -1
case a.Fingerprint() > o.Fingerprint():
return 1
default:
return 0
}
return as[i].Fingerprint() < as[j].Fingerprint()
}

// HasFiring returns true iff one of the alerts is not resolved.
Expand Down
6 changes: 3 additions & 3 deletions model/alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package model

import (
"fmt"
"sort"
"slices"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -251,11 +251,11 @@ func TestSortAlerts(t *testing.T) {
},
}

sort.Sort(alerts)
slices.SortFunc(alerts, (*Alert).Compare)

expected := []string{
"DiskFull[5ffe595][resolved]",
"InternalError[09cfd46][resolved]",
"DiskFull[5ffe595][resolved]",
"OutOfMemory[d43a602][resolved]",
"DiskFull[5ff4595][resolved]",
"OutOfMemory[d444602][resolved]",
Expand Down
17 changes: 1 addition & 16 deletions model/fingerprinting.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,9 @@ func (f Fingerprint) String() string {
}

// Fingerprints represents a collection of Fingerprint subject to a given
// natural sorting scheme. It implements sort.Interface.
// natural sorting scheme.
type Fingerprints []Fingerprint

// Len implements sort.Interface.
func (f Fingerprints) Len() int {
return len(f)
}

// Less implements sort.Interface.
func (f Fingerprints) Less(i, j int) bool {
return f[i] < f[j]
}

// Swap implements sort.Interface.
func (f Fingerprints) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}

// FingerprintSet is a set of Fingerprints.
type FingerprintSet map[Fingerprint]struct{}

Expand Down
5 changes: 3 additions & 2 deletions model/fingerprinting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
package model

import (
"sort"
"cmp"
"slices"
"testing"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ func TestFingerprintsSort(t *testing.T) {
18446744073709551615,
}

sort.Sort(fingerPrints)
slices.SortFunc(fingerPrints, cmp.Compare)

expected := Fingerprints{
0,
Expand Down
63 changes: 16 additions & 47 deletions model/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,9 @@ func (ln *LabelName) UnmarshalJSON(b []byte) error {
return nil
}

// LabelNames is a sortable LabelName slice. In implements sort.Interface.
// LabelNames is a sortable LabelName slice.
type LabelNames []LabelName

func (l LabelNames) Len() int {
return len(l)
}

func (l LabelNames) Less(i, j int) bool {
return l[i] < l[j]
}

func (l LabelNames) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}

func (l LabelNames) String() string {
labelStrings := make([]string, 0, len(l))
for _, label := range l {
Expand All @@ -180,50 +168,31 @@ func (lv LabelValue) IsValid() bool {
return utf8.ValidString(string(lv))
}

// LabelValues is a sortable LabelValue slice. It implements sort.Interface.
// LabelValues is a sortable LabelValue slice.
type LabelValues []LabelValue

func (l LabelValues) Len() int {
return len(l)
}

func (l LabelValues) Less(i, j int) bool {
return string(l[i]) < string(l[j])
}

func (l LabelValues) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}

// LabelPair pairs a name with a value.
type LabelPair struct {
Name LabelName
Value LabelValue
}

// LabelPairs is a sortable slice of LabelPair pointers. It implements
// sort.Interface.
type LabelPairs []*LabelPair

func (l LabelPairs) Len() int {
return len(l)
}

func (l LabelPairs) Less(i, j int) bool {
// Compare implements the cmp.Comparator interface for LabelPair pointers.
// It compares by Name, then by Value.
Comment on lines +180 to +181
func (l *LabelPair) Compare(o *LabelPair) int {
switch {
case l[i].Name > l[j].Name:
return false
case l[i].Name < l[j].Name:
return true
case l[i].Value > l[j].Value:
return false
case l[i].Value < l[j].Value:
return true
case l.Name < o.Name:
return -1
case l.Name > o.Name:
return 1
case l.Value < o.Value:
return -1
case l.Value > o.Value:
return 1
default:
return false
return 0
}
}

func (l LabelPairs) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
// LabelPairs is a sortable slice of LabelPair pointers.
type LabelPairs []*LabelPair
9 changes: 5 additions & 4 deletions model/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
package model

import (
"cmp"
"fmt"
"sort"
"slices"
"testing"
)

Expand All @@ -35,7 +36,7 @@ func testLabelNames(t testing.TB) {
}

for i, scenario := range scenarios {
sort.Sort(scenario.in)
slices.SortFunc(scenario.in, cmp.Compare)

for j, expected := range scenario.out {
if expected != scenario.in[j] {
Expand Down Expand Up @@ -71,7 +72,7 @@ func testLabelValues(t testing.TB) {
}

for i, scenario := range scenarios {
sort.Sort(scenario.in)
slices.SortFunc(scenario.in, cmp.Compare)

for j, expected := range scenario.out {
if expected != scenario.in[j] {
Expand Down Expand Up @@ -211,7 +212,7 @@ func TestSortLabelPairs(t *testing.T) {
},
}

sort.Sort(labelPairs)
slices.SortFunc(labelPairs, (*LabelPair).Compare)

expectedLabelPairs := LabelPairs{
{
Expand Down
5 changes: 3 additions & 2 deletions model/labelset.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
package model

import (
"cmp"
"encoding/json"
"fmt"
"maps"
"sort"
"slices"
)

// A LabelSet is a collection of LabelName and LabelValue pairs. The LabelSet
Expand Down Expand Up @@ -85,7 +86,7 @@ func (ls LabelSet) Before(o LabelSet) bool {
lns = append(lns, ln)
}
// It's probably not worth it to de-dup lns.
sort.Sort(lns)
slices.SortFunc(lns, cmp.Compare)
for _, ln := range lns {
mlv, ok := ls[ln]
if !ok {
Expand Down
4 changes: 2 additions & 2 deletions model/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"
"maps"
"regexp"
"sort"
"slices"
"strconv"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -283,7 +283,7 @@ func (m Metric) String() string {
}
return "{}"
default:
sort.Strings(labelStrings)
slices.Sort(labelStrings)
return fmt.Sprintf("%s{%s}", metricName, strings.Join(labelStrings, ", "))
}
}
Expand Down
Loading
Loading