8000 chore: enable perfsprint linter by mmorel-35 · Pull Request #717 · prometheus/common · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore: enable perfsprint linter #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2024
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
12 changes: 12 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,25 @@ linters:
- govet
- ineffassign
- misspell
- perfsprint
- revive
- staticcheck
- testifylint
- unused
linters-settings:
goimports:
local-prefixes: github.com/prometheus/common
perfsprint:
# Optimizes even if it requires an int or uint type cast.
int-conversion: true
# Optimizes into `err.Error()` even if it is only equivalent for non-nil errors.
err-error: true
# Optimizes `fmt.Errorf`.
errorf: true
# Optimizes `fmt.Sprintf` with only one argument.
sprintf1: true
# Optimizes into strings concatenation.
strconcat: false
revive:
rules:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#unused-parameter
Expand Down
48 changes: 24 additions & 24 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,33 +357,33 @@ func nonZeroCount[T comparable](values ...T) int {
func (c *HTTPClientConfig) Validate() error {
// Backwards compatibility with the bearer_token field.
if len(c.BearerToken) > 0 && len(c.BearerTokenFile) > 0 {
return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured")
return errors.New("at most one of bearer_token & bearer_token_file must be configured")
}
if (c.BasicAuth != nil || c.OAuth2 != nil) && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) {
return fmt.Errorf("at most one of basic_auth, oauth2, bearer_token & bearer_token_file must be configured")
return errors.New("at most one of basic_auth, oauth2, bearer_token & bearer_token_file must be configured")
}
if c.BasicAuth != nil && nonZeroCount(string(c.BasicAuth.Username) != "", c.BasicAuth.UsernameFile != "", c.BasicAuth.UsernameRef != "") > 1 {
return fmt.Errorf("at most one of basic_auth username, username_file & username_ref must be configured")
return errors.New("at most one of basic_auth username, username_file & username_ref must be configured")
}
if c.BasicAuth != nil && nonZeroCount(string(c.BasicAuth.Password) != "", c.BasicAuth.PasswordFile != "", c.BasicAuth.PasswordRef != "") > 1 {
return fmt.Errorf("at most one of basic_auth password, password_file & password_ref must be configured")
return errors.New("at most one of basic_auth password, password_file & password_ref must be configured")
}
if c.Authorization != nil {
if len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0 {
return fmt.Errorf("authorization is not compatible with bearer_token & bearer_token_file")
return errors.New("authorization is not compatible with bearer_token & bearer_token_file")
}
if nonZeroCount(string(c.Authorization.Credentials) != "", c.Authorization.CredentialsFile != "", c.Authorization.CredentialsRef != "") > 1 {
return fmt.Errorf("at most one of authorization credentials & credentials_file must be configured")
return errors.New("at most one of authorization credentials & credentials_file must be configured")
}
c.Authorization.Type = strings.TrimSpace(c.Authorization.Type)
if len(c.Authorization.Type) == 0 {
c.Authorization.Type = "Bearer"
}
if strings.ToLower(c.Authorization.Type) == "basic" {
return fmt.Errorf(`authorization type cannot be set to "basic", use "basic_auth" instead`)
return errors.New(`authorization type cannot be set to "basic", use "basic_auth" instead`)
}
if c.BasicAuth != nil || c.OAuth2 != nil {
return fmt.Errorf("at most one of basic_auth, oauth2 & authorization must be configured")
return errors.New("at most one of basic_auth, oauth2 & authorization must be configured")
}
} else {
if len(c.BearerToken) > 0 {
Expand All @@ -399,16 +399,16 @@ func (c *HTTPClientConfig) Validate() error {
}
if c.OAuth2 != nil {
if c.BasicAuth != nil {
return fmt.Errorf("at most one of basic_auth, oauth2 & authorization must be configured")
return errors.New("at most one of basic_auth, oauth2 & authorization must be configured")
}
if len(c.OAuth2.ClientID) == 0 {
return fmt.Errorf("oauth2 client_id must be configured")
return errors.New("oauth2 client_id must be configured")
}
if len(c.OAuth2.TokenURL) == 0 {
return fmt.Errorf("oauth2 token_url must be configured")
return errors.New("oauth2 token_url must be configured")
}
if nonZeroCount(len(c.OAuth2.ClientSecret) > 0, len(c.OAuth2.ClientSecretFile) > 0, len(c.OAuth2.ClientSecretRef) > 0) > 1 {
return fmt.Errorf("at most one of oauth2 client_secret, client_secret_file & client_secret_ref must be configured")
return errors.New("at most one of oauth2 client_secret, client_secret_file & client_secret_ref must be configured")
}
}
if err := c.ProxyConfig.Validate(); err != nil {
Expand Down Expand Up @@ -735,7 +735,7 @@ func (s *FileSecret) Fetch(ctx context.Context) (string, error) {
}

func (s *FileSecret) Description() string {
return fmt.Sprintf("file %s", s.file)
return "file " + s.file
}

func (s *FileSecret) Immutable() bool {
Expand All @@ -753,7 +753,7 @@ func (s *refSecret) Fetch(ctx context.Context) (string, error) {
}

func (s *refSecret) Description() string {
return fmt.Sprintf("ref %s", s.ref)
return "ref " + s.ref
}

func (s *refSecret) Immutable() bool {
Expand Down Expand Up @@ -1045,7 +1045,7 @@ func NewTLSConfigWithContext(ctx context.Context, cfg *TLSConfig, optFuncs ...TL

if cfg.MaxVersion != 0 && cfg.MinVersion != 0 {
if cfg.MaxVersion < cfg.MinVersion {
return nil, fmt.Errorf("tls_config.max_version must be greater than or equal to tls_config.min_version if both are specified")
return nil, errors.New("tls_config.max_version must be greater than or equal to tls_config.min_version if both are specified")
}
}

Expand Down Expand Up @@ -1144,19 +1144,19 @@ func (c *TLSConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
// used.
func (c *TLSConfig) Validate() error {
if nonZeroCount(len(c.CA) > 0, len(c.CAFile) > 0, len(c.CARef) > 0) > 1 {
return fmt.Errorf("at most one of ca, ca_file & ca_ref must be configured")
return errors.New("at most one of ca, ca_file & ca_ref must be configured")
}
if nonZeroCount(len(c.Cert) > 0, len(c.CertFile) > 0, len(c.CertRef) > 0) > 1 {
return fmt.Errorf("at most one of cert, cert_file & cert_ref must be configured")
return errors.New("at most one of cert, cert_file & cert_ref must be configured")
}
if nonZeroCount(len(c.Key) > 0, len(c.KeyFile) > 0, len(c.KeyRef) > 0) > 1 {
return fmt.Errorf("at most one of key and key_file must be configured")
return errors.New("at most one of key and key_file must be configured")
}

if c.usingClientCert() && !c.usingClientKey() {
return fmt.Errorf("exactly one of key or key_file must be configured when a client certificate is configured")
return errors.New("exactly one of key or key_file must be configured when a client certificate is configured")
} else if c.usingClientKey() && !c.usingClientCert() {
return fmt.Errorf("exactly one of cert or cert_file must be configured when a client key is configured")
return errors.New("exactly one of cert or cert_file must be configured when a client key is configured")
}

return nil
Expand Down Expand Up @@ -1460,16 +1460,16 @@ type ProxyConfig struct {
// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (c *ProxyConfig) Validate() error {
if len(c.ProxyConnectHeader) > 0 && (!c.ProxyFromEnvironment && (c.ProxyURL.URL == nil || c.ProxyURL.String() == "")) {
return fmt.Errorf("if proxy_connect_header is configured, proxy_url or proxy_from_environment must also be configured")
return errors.New("if proxy_connect_header is configured, proxy_url or proxy_from_environment must also be configured")
}
if c.ProxyFromEnvironment && c.ProxyURL.URL != nil && c.ProxyURL.String() != "" {
return fmt.Errorf("if proxy_from_environment is configured, proxy_url must not be configured")
return errors.New("if proxy_from_environment is configured, proxy_url must not be configured")
}
if c.ProxyFromEnvironment && c.NoProxy != "" {
return fmt.Errorf("if proxy_from_environment is configured, no_proxy must not be configured")
return errors.New("if proxy_from_environment is configured, no_proxy must not be configured")
}
if c.ProxyURL.URL == nil && c.NoProxy != "" {
return fmt.Errorf("if no_proxy is configured, proxy_url must also be configured")
return errors.New("if no_proxy is configured, proxy_url must also be configured")
}
return nil
}
Expand Down
16 changes: 8 additions & 8 deletions config/http_config_test.go
F438
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ func TestNewClientFromInvalidConfig(t *testing.T) {
InsecureSkipVerify: true,
},
},
errorMsg: fmt.Sprintf("unable to read CA cert: unable to read file %s", MissingCA),
errorMsg: "unable to read CA cert: unable to read file " + MissingCA,
},
{
clientConfig: HTTPClientConfig{
Expand All @@ -618,7 +618,7 @@ func TestNewClientFromInvalidConfig(t *testing.T) {
InsecureSkipVerify: true,
},
},
errorMsg: fmt.Sprintf("unable to use specified CA cert file %s", InvalidCA),
errorMsg: "unable to use specified CA cert file " + InvalidCA,
},
}

Expand Down Expand Up @@ -864,7 +864,7 @@ func TestTLSConfigInvalidCA(t *testing.T) {
ServerName: "",
InsecureSkipVerify: false,
},
errorMessage: fmt.Sprintf("unable to read CA cert: unable to read file %s", MissingCA),
errorMessage: "unable to read CA cert: unable to read file " + MissingCA,
},
{
configTLSConfig: TLSConfig{
Expand All @@ -874,7 +874,7 @@ func TestTLSConfigInvalidCA(t *testing.T) {
ServerName: "",
InsecureSkipVerify: false,
},
errorMessage: fmt.Sprintf("unable to read specified client cert: unable to read file %s", MissingCert),
errorMessage: "unable to read specified client cert: unable to read file " + MissingCert,
},
{
configTLSConfig: TLSConfig{
Expand All @@ -884,7 +884,7 @@ func TestTLSConfigInvalidCA(t *testing.T) {
ServerName: "",
InsecureSkipVerify: false,
},
errorMessage: fmt.Sprintf("unable to read specified client key: unable to read file %s", MissingKey),
errorMessage: "unable to read specified client key: unable to read file " + MissingKey,
},
{
configTLSConfig: TLSConfig{
Expand Down Expand Up @@ -1715,7 +1715,7 @@ func TestOAuth2UserAgent(t *testing.T) {
ClientSecret: "2",
Scopes: []string{"A", "B"},
EndpointParams: map[string]string{"hi": "hello"},
TokenURL: fmt.Sprintf("%s/token", ts.URL),
TokenURL: ts.URL + "/token",
}

rt, err := NewRoundTripperFromConfig(config, "test_oauth2", WithUserAgent("myuseragent"))
Expand Down Expand Up @@ -2281,7 +2281,7 @@ func TestProxyConfig_Proxy(t *testing.T) {
},
{
name: "valid proxy_url and localhost",
proxyConfig: fmt.Sprintf(`proxy_url: %s`, proxyServer.URL),
proxyConfig: "proxy_url: " + proxyServer.URL,
expectedProxyURL: proxyServer.URL,
targetURL: "http://localhost/",
},
Expand All @@ -2294,7 +2294,7 @@ no_proxy: prometheus.io`, proxyServer.URL),
},
{
name: "valid proxy_url",
proxyConfig: fmt.Sprintf(`proxy_url: %s`, proxyServer.URL),
proxyConfig: "proxy_url: " + proxyServer.URL,
expectedProxyURL: proxyServer.URL,
targetURL: "http://prometheus.io/",
},
Expand Down
5 changes: 3 additions & 2 deletions config/tls_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -159,7 +160,7 @@ func TestTLSVersionMarshalYAML(t *testing.T) {
{
input: TLSVersion(999),
expected: "",
err: fmt.Errorf("unknown TLS version: 999"),
err: errors.New("unknown TLS version: 999"),
},
}

Expand Down Expand Up @@ -199,7 +200,7 @@ func TestTLSVersionMarshalJSON(t *testing.T) {
{
input: TLSVersion(999),
expected: "",
err: fmt.Errorf("unknown TLS version: 999"),
err: errors.New("unknown TLS version: 999"),
},
}

Expand Down
4 changes: 2 additions & 2 deletions expfmt/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Negotiate(h http.Header) Format {
if escapeParam := ac.Params[model.EscapingKey]; escapeParam != "" {
switch Format(escapeParam) {
case model.AllowUTF8, model.EscapeUnderscores, model.EscapeDots, model.EscapeValues:
escapingScheme = Format(fmt.Sprintf("; escaping=%s", escapeParam))
escapingScheme = Format("; escaping=" + escapeParam)
default:
// If the escaping parameter is unknown, ignore it.
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
if escapeParam := ac.Params[model.EscapingKey]; escapeParam != "" {
switch Format(escapeParam) {
case model.AllowUTF8, model.EscapeUnderscores, model.EscapeDots, model.EscapeValues:
escapingScheme = Format(fmt.Sprintf("; escaping=%s", escapeParam))
escapingScheme = Format("; escaping=" + escapeParam)
default:
// If the escaping parameter is unknown, ignore it.
}
Expand Down
4 changes: 2 additions & 2 deletions expfmt/expfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package expfmt

import (
"fmt"
"errors"
"strings"

"github.com/prometheus/common/model"
Expand Down Expand Up @@ -109,7 +109,7 @@ func NewOpenMetricsFormat(version string) (Format, error) {
if version == OpenMetricsVersion_1_0_0 {
return FmtOpenMetrics_1_0_0, nil
}
return FmtUnknown, fmt.Errorf("unknown open metrics version string")
return FmtUnknown, errors.New("unknown open metrics version string")
}

// WithEscapingScheme returns a copy of Format with the specified escaping
Expand Down
4 changes: 2 additions & 2 deletions expfmt/openmetrics_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
if metricType == dto.MetricType_COUNTER && strings.HasSuffix(compliantName, "_total") {
compliantName = name[:len(name)-6]
}
if toOM.withUnit && in.Unit != nil && !strings.HasSuffix(compliantName, fmt.Sprintf("_%s", *in.Unit)) {
compliantName = compliantName + fmt.Sprintf("_%s", *in.Unit)
if toOM.withUnit && in.Unit != nil && !strings.HasSuffix(compliantName, "_"+*in.Unit) {
compliantName = compliantName + "_" + *in.Unit
}

// Comments, first HELP, then TYPE.
Expand Down
2 changes: 1 addition & 1 deletion expfmt/text_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ func histogramMetricName(name string) string {

func parseFloat(s string) (float64, error) {
if strings.ContainsAny(s, "pP_") {
return 0, fmt.Errorf("unsupported character in float")
return 0, errors.New("unsupported character in float")
}
return strconv.ParseFloat(s, 64)
}
7 changes: 4 additions & 3 deletions model/alert.go
528C
Original file line numberDiff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package model

import (
"errors"
"fmt"
"time"
)
Expand Down Expand Up @@ -89,16 +90,16 @@ func (a *Alert) StatusAt(ts time.Time) AlertStatus {
// Validate checks whether the alert data is inconsistent.
func (a *Alert) Validate() error {
if a.StartsAt.IsZero() {
return fmt.Errorf("start time missing")
return errors.New("start time missing")
}
if !a.EndsAt.IsZero() && a.EndsAt.Before(a.StartsAt) {
return fmt.Errorf("start time must be before end time")
return errors.New("start time must be before end time")
}
if err := a.Labels.Validate(); err != nil {
return fmt.Errorf("invalid label set: %w", err)
}
if len(a.Labels) == 0 {
return fmt.Errorf("at least one label pair required")
return errors.New("at least one label pair required")
}
if err := a.Annotations.Validate(); err != nil {
return fmt.Errorf("invalid annotations: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion model/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package model

import (
"errors"
"fmt"
"regexp"
"sort"
Expand Down Expand Up @@ -443,7 +444,7 @@ func (e EscapingScheme) String() string {

func ToEscapingScheme(s string) (EscapingScheme, error) {
if s == "" {
return NoEscaping, fmt.Errorf("got empty string instead of escaping scheme")
return NoEscaping, errors.New("got empty string instead of escaping scheme")
}
switch s {
case AllowUTF8:
Expand Down
Loading
0