Skip to content
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

Sync with prometheus: enable errorf rule from perfsprint linter #777

Merged
merged 2 commits into from
Dec 2, 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
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ linters-settings:
extra-rules: true
perfsprint:
# Optimizes `fmt.Errorf`.
errorf: false
errorf: true
revive:
# By default, revive will enable only the linting rules that are named in the configuration file.
# So, it's needed to explicitly enable all required rules here.
Expand Down
8 changes: 4 additions & 4 deletions cmd/promtool/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import (
)

var (
errNotNativeHistogram = fmt.Errorf("not a native histogram")
errNotEnoughData = fmt.Errorf("not enough data")
errNotNativeHistogram = errors.New("not a native histogram")
errNotEnoughData = errors.New("not enough data")

outputHeader = `Bucket stats for each histogram series over time
------------------------------------------------
Expand Down Expand Up @@ -169,7 +169,7 @@ func querySamples(ctx context.Context, api v1.API, query string, end time.Time)

matrix, ok := values.(model.Matrix)
if !ok {
return nil, fmt.Errorf("query of buckets resulted in non-Matrix")
return nil, errors.New("query of buckets resulted in non-Matrix")
}

return matrix, nil
Expand Down Expand Up @@ -259,7 +259,7 @@ func getBucketCountsAtTime(matrix model.Matrix, numBuckets, timeIdx int) ([]int,
prev := matrix[i].Values[timeIdx]
// Assume the results are nicely aligned.
if curr.Timestamp != prev.Timestamp {
return counts, fmt.Errorf("matrix result is not time aligned")
return counts, errors.New("matrix result is not time aligned")
}
counts[i+1] = int(curr.Value - prev.Value)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/promtool/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func getMinAndMaxTimestamps(p textparse.Parser) (int64, int64, error) {

_, ts, _ := p.Series()
if ts == nil {
return 0, 0, fmt.Errorf("expected timestamp for series got none")
return 0, 0, errors.New("expected timestamp for series got none")
}

if *ts > maxt {
Expand Down
2 changes: 1 addition & 1 deletion cmd/promtool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func checkExperimental(f bool) {
}
}

var errLint = fmt.Errorf("lint error")
var errLint = errors.New("lint error")

type lintConfig struct {
all bool
Expand Down
4 changes: 2 additions & 2 deletions cmd/promtool/tsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ func analyzeCompaction(ctx context.Context, block tsdb.BlockReader, indexr tsdb.
histogramChunkSize = append(histogramChunkSize, len(chk.Bytes()))
fhchk, ok := chk.(*chunkenc.FloatHistogramChunk)
if !ok {
return fmt.Errorf("chunk is not FloatHistogramChunk")
return errors.New("chunk is not FloatHistogramChunk")
}
it := fhchk.Iterator(nil)
bucketCount := 0
Expand All @@ -677,7 +677,7 @@ func analyzeCompaction(ctx context.Context, block tsdb.BlockReader, indexr tsdb.
histogramChunkSize = append(histogramChunkSize, len(chk.Bytes()))
hchk, ok := chk.(*chunkenc.HistogramChunk)
if !ok {
return fmt.Errorf("chunk is not HistogramChunk")
return errors.New("chunk is not HistogramChunk")
}
it := hchk.Iterator(nil)
bucketCount := 0
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ func (c *AlertmanagerConfig) UnmarshalYAML(unmarshal func(interface{}) error) er
c.HTTPClientConfig.Authorization != nil || c.HTTPClientConfig.OAuth2 != nil

if httpClientConfigAuthEnabled && c.SigV4Config != nil {
return fmt.Errorf("at most one of basic_auth, authorization, oauth2, & sigv4 must be configured")
return errors.New("at most one of basic_auth, authorization, oauth2, & sigv4 must be configured")
}

// Check for users putting URLs in target groups.
Expand Down Expand Up @@ -1420,7 +1420,7 @@ func (c *OTLPConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
for i, attr := range c.PromoteResourceAttributes {
attr = strings.TrimSpace(attr)
if attr == "" {
err = errors.Join(err, fmt.Errorf("empty promoted OTel resource attribute"))
err = errors.Join(err, errors.New("empty promoted OTel resource attribute"))
continue
}
if _, exists := seen[attr]; exists {
Expand Down
2 changes: 1 addition & 1 deletion discovery/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ type EC2Discovery struct {
func NewEC2Discovery(conf *EC2SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*EC2Discovery, error) {
m, ok := metrics.(*ec2Metrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

if logger == nil {
Expand Down
2 changes: 1 addition & 1 deletion discovery/aws/lightsail.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type LightsailDiscovery struct {
func NewLightsailDiscovery(conf *LightsailSDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*LightsailDiscovery, error) {
m, ok := metrics.(*lightsailMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

if logger == nil {
Expand Down
2 changes: 1 addition & 1 deletion discovery/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ type Discovery struct {
func NewDiscovery(cfg *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
m, ok := metrics.(*azureMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

if logger == nil {
Expand Down
2 changes: 1 addition & 1 deletion discovery/consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ type Discovery struct {
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
m, ok := metrics.(*consulMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

if logger == nil {
Expand Down
3 changes: 2 additions & 1 deletion discovery/digitalocean/digitalocean.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package digitalocean

import (
"context"
"errors"
"fmt"
"log/slog"
"net"
Expand Down Expand Up @@ -114,7 +115,7 @@ type Discovery struct {
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
m, ok := metrics.(*digitaloceanMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

d := &Discovery{
Expand Down
2 changes: 1 addition & 1 deletion discovery/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ type Discovery struct {
func NewDiscovery(conf SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
m, ok := metrics.(*dnsMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

if logger == nil {
Expand Down
4 changes: 2 additions & 2 deletions discovery/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package dns

import (
"context"
"fmt"
"errors"
"log/slog"
"net"
"testing"
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestDNS(t *testing.T) {
Type: "A",
},
lookup: func(name string, qtype uint16, logger *slog.Logger) (*dns.Msg, error) {
return nil, fmt.Errorf("some error")
return nil, errors.New("some error")
},
expected: []*targetgroup.Group{},
},
Expand Down
3 changes: 1 addition & 2 deletions discovery/eureka/eureka.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package eureka
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
Expand Down Expand Up @@ -129,7 +128,7 @@ type Discovery struct {
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
m, ok := metrics.(*eurekaMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "eureka_sd")
Expand Down
2 changes: 1 addition & 1 deletion discovery/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ type Discovery struct {
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
fm, ok := metrics.(*fileMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

if logger == nil {
Expand Down
2 changes: 1 addition & 1 deletion discovery/gce/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type Discovery struct {
func NewDiscovery(conf SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
m, ok := metrics.(*gceMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

d := &Discovery{
Expand Down
2 changes: 1 addition & 1 deletion discovery/hetzner/hetzner.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ type Discovery struct {
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*refresh.Discovery, error) {
m, ok := metrics.(*hetznerMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

r, err := newRefresher(conf, logger)
Expand Down
8 changes: 4 additions & 4 deletions discovery/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
return err
}
if c.URL == "" {
return fmt.Errorf("URL is missing")
return errors.New("URL is missing")
}
parsedURL, err := url.Parse(c.URL)
if err != nil {
return err
}
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
return fmt.Errorf("URL scheme must be 'http' or 'https'")
return errors.New("URL scheme must be 'http' or 'https'")
}
if parsedURL.Host == "" {
return fmt.Errorf("host is missing in URL")
return errors.New("host is missing in URL")
}
return c.HTTPClientConfig.Validate()
}
Expand All @@ -118,7 +118,7 @@ type Discovery struct {
func NewDiscovery(conf *SDConfig, logger *slog.Logger, clientOpts []config.HTTPClientOption, metrics discovery.DiscovererMetrics) (*Discovery, error) {
m, ok := metrics.(*httpMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

if logger == nil {
Expand Down
3 changes: 1 addition & 2 deletions discovery/ionos/ionos.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package ionos

import (
"errors"
"fmt"
"log/slog"
"time"

Expand Down Expand Up @@ -46,7 +45,7 @@ type Discovery struct{}
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*refresh.Discovery, error) {
m, ok := metrics.(*ionosMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

if conf.ionosEndpoint == "" {
Expand Down
22 changes: 11 additions & 11 deletions discovery/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,28 +173,28 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
return err
}
if c.Role == "" {
return fmt.Errorf("role missing (one of: pod, service, endpoints, endpointslice, node, ingress)")
return errors.New("role missing (one of: pod, service, endpoints, endpointslice, node, ingress)")
}
err = c.HTTPClientConfig.Validate()
if err != nil {
return err
}
if c.APIServer.URL != nil && c.KubeConfig != "" {
// Api-server and kubeconfig_file are mutually exclusive
return fmt.Errorf("cannot use 'kubeconfig_file' and 'api_server' simultaneously")
return errors.New("cannot use 'kubeconfig_file' and 'api_server' simultaneously")
}
if c.KubeConfig != "" && !reflect.DeepEqual(c.HTTPClientConfig, config.DefaultHTTPClientConfig) {
// Kubeconfig_file and custom http config are mutually exclusive
return fmt.Errorf("cannot use a custom HTTP client configuration together with 'kubeconfig_file'")
return errors.New("cannot use a custom HTTP client configuration together with 'kubeconfig_file'")
}
if c.APIServer.URL == nil && !reflect.DeepEqual(c.HTTPClientConfig, config.DefaultHTTPClientConfig) {
return fmt.Errorf("to use custom HTTP client configuration please provide the 'api_server' URL explicitly")
return errors.New("to use custom HTTP client configuration please provide the 'api_server' URL explicitly")
}
if c.APIServer.URL != nil && c.NamespaceDiscovery.IncludeOwnNamespace {
return fmt.Errorf("cannot use 'api_server' and 'namespaces.own_namespace' simultaneously")
return errors.New("cannot use 'api_server' and 'namespaces.own_namespace' simultaneously")
}
if c.KubeConfig != "" && c.NamespaceDiscovery.IncludeOwnNamespace {
return fmt.Errorf("cannot use 'kubeconfig_file' and 'namespaces.own_namespace' simultaneously")
return errors.New("cannot use 'kubeconfig_file' and 'namespaces.own_namespace' simultaneously")
}

foundSelectorRoles := make(map[Role]struct{})
Expand Down Expand Up @@ -288,7 +288,7 @@ func (d *Discovery) getNamespaces() []string {
func New(l *slog.Logger, metrics discovery.DiscovererMetrics, conf *SDConfig) (*Discovery, error) {
m, ok := metrics.(*kubernetesMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

if l == nil {
Expand Down Expand Up @@ -672,7 +672,7 @@ func (d *Discovery) newPodsByNodeInformer(plw *cache.ListWatch) cache.SharedInde
indexers[nodeIndex] = func(obj interface{}) ([]string, error) {
pod, ok := obj.(*apiv1.Pod)
if !ok {
return nil, fmt.Errorf("object is not a pod")
return nil, errors.New("object is not a pod")
}
return []string{pod.Spec.NodeName}, nil
}
Expand All @@ -686,7 +686,7 @@ func (d *Discovery) newEndpointsByNodeInformer(plw *cache.ListWatch) cache.Share
indexers[podIndex] = func(obj interface{}) ([]string, error) {
e, ok := obj.(*apiv1.Endpoints)
if !ok {
return nil, fmt.Errorf("object is not endpoints")
return nil, errors.New("object is not endpoints")
}
var pods []string
for _, target := range e.Subsets {
Expand All @@ -705,7 +705,7 @@ func (d *Discovery) newEndpointsByNodeInformer(plw *cache.ListWatch) cache.Share
indexers[nodeIndex] = func(obj interface{}) ([]string, error) {
e, ok := obj.(*apiv1.Endpoints)
if !ok {
return nil, fmt.Errorf("object is not endpoints")
return nil, errors.New("object is not endpoints")
}
var nodes []string
for _, target := range e.Subsets {
Expand Down Expand Up @@ -751,7 +751,7 @@ func (d *Discovery) newEndpointSlicesByNodeInformer(plw *cache.ListWatch, object
}
}
default:
return nil, fmt.Errorf("object is not an endpointslice")
return nil, errors.New("object is not an endpointslice")
}

return nodes, nil
Expand Down
2 changes: 1 addition & 1 deletion discovery/linode/linode.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ type Discovery struct {
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
m, ok := metrics.(*linodeMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

d := &Discovery{
Expand Down
7 changes: 4 additions & 3 deletions discovery/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package discovery

import (
"context"
"errors"
"fmt"
"sort"
"strconv"
Expand Down Expand Up @@ -1209,9 +1210,9 @@ func TestGaugeFailedConfigs(t *testing.T) {

c := map[string]Configs{
"prometheus": {
errorConfig{fmt.Errorf("tests error 0")},
errorConfig{fmt.Errorf("tests error 1")},
errorConfig{fmt.Errorf("tests error 2")},
errorConfig{errors.New("tests error 0")},
errorConfig{errors.New("tests error 1")},
errorConfig{errors.New("tests error 2")},
},
}
discoveryManager.ApplyConfig(c)
Expand Down
2 changes: 1 addition & 1 deletion discovery/marathon/marathon.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type Discovery struct {
func NewDiscovery(conf SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
m, ok := metrics.(*marathonMetrics)
if !ok {
return nil, fmt.Errorf("invalid discovery metrics type")
return nil, errors.New("invalid discovery metrics type")
}

rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "marathon_sd")
Expand Down
Loading
Loading