Skip to content
Merged
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
13 changes: 13 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ linters:
- govet
- loggercheck
- misspell
- modernize
- nilnesserr
# TODO(bwplotka): Enable once https://github.com/golangci/golangci-lint/issues/3228 is fixed.
# - nolintlint
Expand Down Expand Up @@ -43,6 +44,9 @@ linters:
- linters:
- gocritic
text: "appendAssign"
- linters:
- errcheck
path: _test.go
warn-unused: true
settings:
depguard:
Expand Down Expand Up @@ -77,6 +81,15 @@ linters:
- shadow
- fieldalignment
enable-all: true
modernize:
disable:
# Suggest replacing omitempty with omitzero for struct fields.
# Disable this check for now since it introduces too many changes in our existing codebase.
# See https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize#hdr-Analyzer_omitzero for more details.
- omitzero
# Disable newexpr check for now since it introduces too many changes in our existing codebase.
# To be re-enabled as a part of https://github.com/prometheus/prometheus/issues/18066.
- newexpr
perfsprint:
# Optimizes even if it requires an int or uint type cast.
int-conversion: true
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Secret string
var MarshalSecretValue = false

// MarshalYAML implements the yaml.Marshaler interface for Secrets.
func (s Secret) MarshalYAML() (interface{}, error) {
func (s Secret) MarshalYAML() (any, error) {
if MarshalSecretValue {
return string(s), nil
}
Expand All @@ -44,7 +44,7 @@ func (s Secret) MarshalYAML() (interface{}, error) {
}

// UnmarshalYAML implements the yaml.Unmarshaler interface for Secrets.
func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (s *Secret) UnmarshalYAML(unmarshal func(any) error) error {
type plain Secret
return unmarshal((*plain)(s))
}
Expand Down
32 changes: 15 additions & 17 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
"maps"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -76,7 +77,7 @@ var TLSVersions = map[string]TLSVersion{
"TLS10": (TLSVersion)(tls.VersionTLS10),
}

func (tv *TLSVersion) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (tv *TLSVersion) UnmarshalYAML(unmarshal func(any) error) error {
var s string
err := unmarshal(&s)
if err != nil {
Expand All @@ -89,7 +90,7 @@ func (tv *TLSVersion) UnmarshalYAML(unmarshal func(interface{}) error) error {
return fmt.Errorf("unknown TLS version: %s", s)
}

func (tv TLSVersion) MarshalYAML() (interface{}, error) {
func (tv TLSVersion) MarshalYAML() (any, error) {
for s, v := range TLSVersions {
if tv == v {
return s, nil
Expand Down Expand Up @@ -178,7 +179,7 @@ type URL struct {
}

// UnmarshalYAML implements the yaml.Unmarshaler interface for URLs.
func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (u *URL) UnmarshalYAML(unmarshal func(any) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
Expand All @@ -193,7 +194,7 @@ func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error {
}

// MarshalYAML implements the yaml.Marshaler interface for URLs.
func (u URL) MarshalYAML() (interface{}, error) {
func (u URL) MarshalYAML() (any, error) {
if u.URL != nil {
return u.Redacted(), nil
}
Expand Down Expand Up @@ -269,16 +270,16 @@ type OAuth2 struct {
Audience string `yaml:"audience,omitempty" json:"audience,omitempty"`
// Claims is a map of claims to be added to the JWT token. Only used if
// GrantType is set to "urn:ietf:params:oauth:grant-type:jwt-bearer".
Claims map[string]interface{} `yaml:"claims,omitempty" json:"claims,omitempty"`
Scopes []string `yaml:"scopes,omitempty" json:"scopes,omitempty"`
TokenURL string `yaml:"token_url,omitempty" json:"token_url,omitempty"`
EndpointParams map[string]string `yaml:"endpoint_params,omitempty" json:"endpoint_params,omitempty"`
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
Claims map[string]any `yaml:"claims,omitempty" json:"claims,omitempty"`
Scopes []string `yaml:"scopes,omitempty" json:"scopes,omitempty"`
TokenURL string `yaml:"token_url,omitempty" json:"token_url,omitempty"`
EndpointParams map[string]string `yaml:"endpoint_params,omitempty" json:"endpoint_params,omitempty"`
TLSConfig TLSConfig `yaml:"tls_config,omitempty"`
ProxyConfig `yaml:",inline"`
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (o *OAuth2) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (o *OAuth2) UnmarshalYAML(unmarshal func(any) error) error {
type plain OAuth2
if err := unmarshal((*plain)(o)); err != nil {
return err
Expand Down Expand Up @@ -463,7 +464,7 @@ func (c *HTTPClientConfig) Validate() error {
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *HTTPClientConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (c *HTTPClientConfig) UnmarshalYAML(unmarshal func(any) error) error {
type plain HTTPClientConfig
*c = DefaultHTTPClientConfig
if err := unmarshal((*plain)(c)); err != nil {
Expand All @@ -483,7 +484,7 @@ func (c *HTTPClientConfig) UnmarshalJSON(data []byte) error {
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (a *BasicAuth) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (a *BasicAuth) UnmarshalYAML(unmarshal func(any) error) error {
type plain BasicAuth
return unmarshal((*plain)(a))
}
Expand Down Expand Up @@ -1130,10 +1131,7 @@ func cloneRequest(r *http.Request) *http.Request {
r2 := new(http.Request)
*r2 = *r
// Deep copy of the Header.
r2.Header = make(http.Header)
for k, s := range r.Header {
r2.Header[k] = s
}
maps.Copy(r.Header, r2.Header)
return r2
}

Expand Down Expand Up @@ -1256,7 +1254,7 @@ func (c *TLSConfig) SetDirectory(dir string) {
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *TLSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (c *TLSConfig) UnmarshalYAML(unmarshal func(any) error) error {
type plain TLSConfig
if err := unmarshal((*plain)(c)); err != nil {
return err
Expand Down
18 changes: 6 additions & 12 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,6 @@ func TestTLSRoundTripper(t *testing.T) {

var c *http.Client
for i, tc := range testCases {
tc := tc
t.Run(strconv.Itoa(i), func(t *testing.T) {
writeCertificate(bs, tc.ca, ca)
writeCertificate(bs, tc.cert, cert)
Expand Down Expand Up @@ -1239,7 +1238,6 @@ func TestTLSRoundTripper_Inline(t *testing.T) {
}

for i, tc := range testCases {
tc := tc
t.Run(strconv.Itoa(i), func(t *testing.T) {
cfg := HTTPClientConfig{
TLSConfig: TLSConfig{
Expand Down Expand Up @@ -1316,10 +1314,8 @@ func TestTLSRoundTripperRaces(t *testing.T) {
ch := make(chan struct{})
var total, ok int64
// Spawn 10 Go routines polling the server concurrently.
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for range 10 {
wg.Go(func() {
for {
select {
case <-ch:
Expand All @@ -1333,13 +1329,11 @@ func TestTLSRoundTripperRaces(t *testing.T) {
}
}
}
}()
})
}

// Change the CA file every 10ms for 1 second.
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
i := 0
for {
tick := time.NewTicker(10 * time.Millisecond)
Expand All @@ -1355,7 +1349,7 @@ func TestTLSRoundTripperRaces(t *testing.T) {
return
}
}
}()
})

wg.Wait()
require.NotEqualf(t, ok, total, "Expecting some requests to fail but got %d/%d successful requests", ok, total)
Expand Down Expand Up @@ -1788,7 +1782,7 @@ endpoint_params:
Scopes: []string{"A", "B"},
TokenURL: ts.tokenURL(),
EndpointParams: map[string]string{"hi": "hello"},
Claims: map[string]interface{}{
Claims: map[string]any{
"iss": "https://example.com",
"aud": "common-test",
"sub": "common",
Expand Down
5 changes: 2 additions & 3 deletions config/oauth_assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/json"
"fmt"
"io"
"maps"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -133,9 +134,7 @@ func (js jwtSource) Token() (*oauth2.Token, error) {
claims["scope"] = scopes
}

for k, v := range js.conf.PrivateClaims {
claims[k] = v
}
maps.Copy(claims, js.conf.PrivateClaims)

assertion := jwt.NewWithClaims(js.conf.SigningAlgorithm, claims)
if js.conf.PrivateKeyID != "" {
Expand Down
4 changes: 2 additions & 2 deletions expfmt/expfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func NewOpenMetricsFormat(version string) (Format, error) {
// removed.
func (f Format) WithEscapingScheme(s model.EscapingScheme) Format {
var terms []string
for _, p := range strings.Split(string(f), ";") {
for p := range strings.SplitSeq(string(f), ";") {
toks := strings.Split(p, "=")
if len(toks) != 2 {
trimmed := strings.TrimSpace(p)
Expand Down Expand Up @@ -194,7 +194,7 @@ func (f Format) FormatType() FormatType {
// "escaping" term exists, that will be used. Otherwise, the global default will
// be returned.
func (f Format) ToEscapingScheme() model.EscapingScheme {
for _, p := range strings.Split(string(f), ";") {
for p := range strings.SplitSeq(string(f), ";") {
toks := strings.Split(p, "=")
if len(toks) != 2 {
continue
Expand Down
4 changes: 2 additions & 2 deletions expfmt/text_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ const (

var (
bufPool = sync.Pool{
New: func() interface{} {
New: func() any {
return bufio.NewWriter(io.Discard)
},
}
numBufPool = sync.Pool{
New: func() interface{} {
New: func() any {
b := make([]byte, 0, initialNumBufSize)
return &b
},
Expand Down
6 changes: 3 additions & 3 deletions helpers/templates/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

var errNaNOrInf = errors.New("value is NaN or Inf")

func ConvertToFloat(i interface{}) (float64, error) {
func ConvertToFloat(i any) (float64, error) {
switch v := i.(type) {
case float64:
return v, nil
Expand Down Expand Up @@ -58,7 +58,7 @@ func FloatToTime(v float64) (*time.Time, error) {
return &t, nil
}

func HumanizeDuration(i interface{}) (string, error) {
func HumanizeDuration(i any) (string, error) {
v, err := ConvertToFloat(i)
if err != nil {
return "", err
Expand Down Expand Up @@ -105,7 +105,7 @@ func HumanizeDuration(i interface{}) (string, error) {
return fmt.Sprintf("%.4g%ss", v, prefix), nil
}

func HumanizeTimestamp(i interface{}) (string, error) {
func HumanizeTimestamp(i any) (string, error) {
v, err := ConvertToFloat(i)
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions helpers/templates/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
func TestHumanizeDuration(t *testing.T) {
tc := []struct {
name string
input interface{}
input any
expected string
}{
// Integers
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestHumanizeDurationErrorString(t *testing.T) {
func TestHumanizeTimestamp(t *testing.T) {
tc := []struct {
name string
input interface{}
input any
expected string
}{
// Int
Expand Down
2 changes: 1 addition & 1 deletion model/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (ln LabelName) IsValidLegacy() bool {
}

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (ln *LabelName) UnmarshalYAML(unmarshal func(interface{}) error) error {
func (ln *LabelName) UnmarshalYAML(unmarshal func(any) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
Expand Down
13 changes: 4 additions & 9 deletions model/labelset.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package model
import (
"encoding/json"
"fmt"
"maps"
"sort"
)

Expand Down Expand Up @@ -107,23 +108,17 @@ func (ls LabelSet) Before(o LabelSet) bool {
// Clone returns a copy of the label set.
func (ls LabelSet) Clone() LabelSet {
lsn := make(LabelSet, len(ls))
for ln, lv := range ls {
lsn[ln] = lv
}
maps.Copy(lsn, ls)
return lsn
}

// Merge is a helper function to non-destructively merge two label sets.
func (ls LabelSet) Merge(other LabelSet) LabelSet {
result := make(LabelSet, len(ls))

for k, v := range ls {
result[k] = v
}
maps.Copy(result, ls)

for k, v := range other {
result[k] = v
}
maps.Copy(result, other)

return result
}
Expand Down
5 changes: 2 additions & 3 deletions model/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/json"
"errors"
"fmt"
"maps"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -258,9 +259,7 @@ func (m Metric) Before(o Metric) bool {
// Clone returns a copy of the Metric.
func (m Metric) Clone() Metric {
clone := make(Metric, len(m))
for k, v := range m {
clone[k] = v
}
maps.Copy(clone, m)
return clone
}

Expand Down
2 changes: 1 addition & 1 deletion model/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ func TestEscapeMetricFamily(t *testing.T) {
},
}

unexportList := []interface{}{dto.MetricFamily{}, dto.Metric{}, dto.LabelPair{}, dto.Counter{}, dto.Gauge{}}
unexportList := []any{dto.MetricFamily{}, dto.Metric{}, dto.LabelPair{}, dto.Counter{}, dto.Gauge{}}

for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion model/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func benchmarkMetricToFastFingerprintConc(b *testing.B, ls LabelSet, e Fingerpri
end.Add(concLevel)
errc := make(chan error, 1)

for i := 0; i < concLevel; i++ {
for range concLevel {
go func() {
start.Wait()
for j := b.N / concLevel; j >= 0; j-- {
Expand Down
Loading
Loading