Skip to content

Commit af1a19f

Browse files
mmorel-35machine424
authored andcommitted
enable errorf rule from perfsprint linter
Signed-off-by: Matthieu MOREL <[email protected]>
1 parent 37f3f3f commit af1a19f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+164
-149
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ linters-settings:
109109
extra-rules: true
110110
perfsprint:
111111
# Optimizes `fmt.Errorf`.
112-
errorf: false
112+
errorf: true
113113
revive:
114114
# By default, revive will enable only the linting rules that are named in the configuration file.
115115
# So, it's needed to explicitly enable all required rules here.

cmd/promtool/analyze.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ import (
3434
)
3535

3636
var (
37-
errNotNativeHistogram = fmt.Errorf("not a native histogram")
38-
errNotEnoughData = fmt.Errorf("not enough data")
37+
errNotNativeHistogram = errors.New("not a native histogram")
38+
errNotEnoughData = errors.New("not enough data")
3939

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

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

175175
return matrix, nil
@@ -259,7 +259,7 @@ func getBucketCountsAtTime(matrix model.Matrix, numBuckets, timeIdx int) ([]int,
259259
prev := matrix[i].Values[timeIdx]
260260
// Assume the results are nicely aligned.
261261
if curr.Timestamp != prev.Timestamp {
262-
return counts, fmt.Errorf("matrix result is not time aligned")
262+
return counts, errors.New("matrix result is not time aligned")
263263
}
264264
counts[i+1] = int(curr.Value - prev.Value)
265265
}

cmd/promtool/backfill.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func getMinAndMaxTimestamps(p textparse.Parser) (int64, int64, error) {
4949

5050
_, ts, _ := p.Series()
5151
if ts == nil {
52-
return 0, 0, fmt.Errorf("expected timestamp for series got none")
52+
return 0, 0, errors.New("expected timestamp for series got none")
5353
}
5454

5555
if *ts > maxt {

cmd/promtool/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ func checkExperimental(f bool) {
444444
}
445445
}
446446

447-
var errLint = fmt.Errorf("lint error")
447+
var errLint = errors.New("lint error")
448448

449449
type lintConfig struct {
450450
all bool

cmd/promtool/tsdb.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ func analyzeCompaction(ctx context.Context, block tsdb.BlockReader, indexr tsdb.
662662
histogramChunkSize = append(histogramChunkSize, len(chk.Bytes()))
663663
fhchk, ok := chk.(*chunkenc.FloatHistogramChunk)
664664
if !ok {
665-
return fmt.Errorf("chunk is not FloatHistogramChunk")
665+
return errors.New("chunk is not FloatHistogramChunk")
666666
}
667667
it := fhchk.Iterator(nil)
668668
bucketCount := 0
@@ -677,7 +677,7 @@ func analyzeCompaction(ctx context.Context, block tsdb.BlockReader, indexr tsdb.
677677
histogramChunkSize = append(histogramChunkSize, len(chk.Bytes()))
678678
hchk, ok := chk.(*chunkenc.HistogramChunk)
679679
if !ok {
680-
return fmt.Errorf("chunk is not HistogramChunk")
680+
return errors.New("chunk is not HistogramChunk")
681681
}
682682
it := hchk.Iterator(nil)
683683
bucketCount := 0

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ func (c *AlertmanagerConfig) UnmarshalYAML(unmarshal func(interface{}) error) er
10721072
c.HTTPClientConfig.Authorization != nil || c.HTTPClientConfig.OAuth2 != nil
10731073

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

10781078
// Check for users putting URLs in target groups.
@@ -1420,7 +1420,7 @@ func (c *OTLPConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
14201420
for i, attr := range c.PromoteResourceAttributes {
14211421
attr = strings.TrimSpace(attr)
14221422
if attr == "" {
1423-
err = errors.Join(err, fmt.Errorf("empty promoted OTel resource attribute"))
1423+
err = errors.Join(err, errors.New("empty promoted OTel resource attribute"))
14241424
continue
14251425
}
14261426
if _, exists := seen[attr]; exists {

discovery/aws/ec2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ type EC2Discovery struct {
161161
func NewEC2Discovery(conf *EC2SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*EC2Discovery, error) {
162162
m, ok := metrics.(*ec2Metrics)
163163
if !ok {
164-
return nil, fmt.Errorf("invalid discovery metrics type")
164+
return nil, errors.New("invalid discovery metrics type")
165165
}
166166

167167
if logger == nil {

discovery/aws/lightsail.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ type LightsailDiscovery struct {
134134
func NewLightsailDiscovery(conf *LightsailSDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*LightsailDiscovery, error) {
135135
m, ok := metrics.(*lightsailMetrics)
136136
if !ok {
137-
return nil, fmt.Errorf("invalid discovery metrics type")
137+
return nil, errors.New("invalid discovery metrics type")
138138
}
139139

140140
if logger == nil {

discovery/azure/azure.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ type Discovery struct {
186186
func NewDiscovery(cfg *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
187187
m, ok := metrics.(*azureMetrics)
188188
if !ok {
189-
return nil, fmt.Errorf("invalid discovery metrics type")
189+
return nil, errors.New("invalid discovery metrics type")
190190
}
191191

192192
if logger == nil {

discovery/consul/consul.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ type Discovery struct {
189189
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
190190
m, ok := metrics.(*consulMetrics)
191191
if !ok {
192-
return nil, fmt.Errorf("invalid discovery metrics type")
192+
return nil, errors.New("invalid discovery metrics type")
193193
}
194194

195195
if logger == nil {

discovery/digitalocean/digitalocean.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package digitalocean
1515

1616
import (
1717
"context"
18+
"errors"
1819
"fmt"
1920
"log/slog"
2021
"net"
@@ -114,7 +115,7 @@ type Discovery struct {
114115
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
115116
m, ok := metrics.(*digitaloceanMetrics)
116117
if !ok {
117-
return nil, fmt.Errorf("invalid discovery metrics type")
118+
return nil, errors.New("invalid discovery metrics type")
118119
}
119120

120121
d := &Discovery{

discovery/dns/dns.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ type Discovery struct {
121121
func NewDiscovery(conf SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
122122
m, ok := metrics.(*dnsMetrics)
123123
if !ok {
124-
return nil, fmt.Errorf("invalid discovery metrics type")
124+
return nil, errors.New("invalid discovery metrics type")
125125
}
126126

127127
if logger == nil {

discovery/dns/dns_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package dns
1515

1616
import (
1717
"context"
18-
"fmt"
18+
"errors"
1919
"log/slog"
2020
"net"
2121
"testing"
@@ -53,7 +53,7 @@ func TestDNS(t *testing.T) {
5353
Type: "A",
5454
},
5555
lookup: func(name string, qtype uint16, logger *slog.Logger) (*dns.Msg, error) {
56-
return nil, fmt.Errorf("some error")
56+
return nil, errors.New("some error")
5757
},
5858
expected: []*targetgroup.Group{},
5959
},

discovery/eureka/eureka.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package eureka
1616
import (
1717
"context"
1818
"errors"
19-
"fmt"
2019
"log/slog"
2120
"net"
2221
"net/http"
@@ -129,7 +128,7 @@ type Discovery struct {
129128
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
130129
m, ok := metrics.(*eurekaMetrics)
131130
if !ok {
132-
return nil, fmt.Errorf("invalid discovery metrics type")
131+
return nil, errors.New("invalid discovery metrics type")
133132
}
134133

135134
rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "eureka_sd")

discovery/file/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ type Discovery struct {
184184
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
185185
fm, ok := metrics.(*fileMetrics)
186186
if !ok {
187-
return nil, fmt.Errorf("invalid discovery metrics type")
187+
return nil, errors.New("invalid discovery metrics type")
188188
}
189189

190190
if logger == nil {

discovery/gce/gce.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ type Discovery struct {
132132
func NewDiscovery(conf SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
133133
m, ok := metrics.(*gceMetrics)
134134
if !ok {
135-
return nil, fmt.Errorf("invalid discovery metrics type")
135+
return nil, errors.New("invalid discovery metrics type")
136136
}
137137

138138
d := &Discovery{

discovery/hetzner/hetzner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ type Discovery struct {
138138
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*refresh.Discovery, error) {
139139
m, ok := metrics.(*hetznerMetrics)
140140
if !ok {
141-
return nil, fmt.Errorf("invalid discovery metrics type")
141+
return nil, errors.New("invalid discovery metrics type")
142142
}
143143

144144
r, err := newRefresher(conf, logger)

discovery/http/http.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
8686
return err
8787
}
8888
if c.URL == "" {
89-
return fmt.Errorf("URL is missing")
89+
return errors.New("URL is missing")
9090
}
9191
parsedURL, err := url.Parse(c.URL)
9292
if err != nil {
9393
return err
9494
}
9595
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
96-
return fmt.Errorf("URL scheme must be 'http' or 'https'")
96+
return errors.New("URL scheme must be 'http' or 'https'")
9797
}
9898
if parsedURL.Host == "" {
99-
return fmt.Errorf("host is missing in URL")
99+
return errors.New("host is missing in URL")
100100
}
101101
return c.HTTPClientConfig.Validate()
102102
}
@@ -118,7 +118,7 @@ type Discovery struct {
118118
func NewDiscovery(conf *SDConfig, logger *slog.Logger, clientOpts []config.HTTPClientOption, metrics discovery.DiscovererMetrics) (*Discovery, error) {
119119
m, ok := metrics.(*httpMetrics)
120120
if !ok {
121-
return nil, fmt.Errorf("invalid discovery metrics type")
121+
return nil, errors.New("invalid discovery metrics type")
122122
}
123123

124124
if logger == nil {

discovery/ionos/ionos.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package ionos
1515

1616
import (
1717
"errors"
18-
"fmt"
1918
"log/slog"
2019
"time"
2120

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

5251
if conf.ionosEndpoint == "" {

discovery/kubernetes/kubernetes.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,28 +173,28 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
173173
return err
174174
}
175175
if c.Role == "" {
176-
return fmt.Errorf("role missing (one of: pod, service, endpoints, endpointslice, node, ingress)")
176+
return errors.New("role missing (one of: pod, service, endpoints, endpointslice, node, ingress)")
177177
}
178178
err = c.HTTPClientConfig.Validate()
179179
if err != nil {
180180
return err
181181
}
182182
if c.APIServer.URL != nil && c.KubeConfig != "" {
183183
// Api-server and kubeconfig_file are mutually exclusive
184-
return fmt.Errorf("cannot use 'kubeconfig_file' and 'api_server' simultaneously")
184+
return errors.New("cannot use 'kubeconfig_file' and 'api_server' simultaneously")
185185
}
186186
if c.KubeConfig != "" && !reflect.DeepEqual(c.HTTPClientConfig, config.DefaultHTTPClientConfig) {
187187
// Kubeconfig_file and custom http config are mutually exclusive
188-
return fmt.Errorf("cannot use a custom HTTP client configuration together with 'kubeconfig_file'")
188+
return errors.New("cannot use a custom HTTP client configuration together with 'kubeconfig_file'")
189189
}
190190
if c.APIServer.URL == nil && !reflect.DeepEqual(c.HTTPClientConfig, config.DefaultHTTPClientConfig) {
191-
return fmt.Errorf("to use custom HTTP client configuration please provide the 'api_server' URL explicitly")
191+
return errors.New("to use custom HTTP client configuration please provide the 'api_server' URL explicitly")
192192
}
193193
if c.APIServer.URL != nil && c.NamespaceDiscovery.IncludeOwnNamespace {
194-
return fmt.Errorf("cannot use 'api_server' and 'namespaces.own_namespace' simultaneously")
194+
return errors.New("cannot use 'api_server' and 'namespaces.own_namespace' simultaneously")
195195
}
196196
if c.KubeConfig != "" && c.NamespaceDiscovery.IncludeOwnNamespace {
197-
return fmt.Errorf("cannot use 'kubeconfig_file' and 'namespaces.own_namespace' simultaneously")
197+
return errors.New("cannot use 'kubeconfig_file' and 'namespaces.own_namespace' simultaneously")
198198
}
199199

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

294294
if l == nil {
@@ -672,7 +672,7 @@ func (d *Discovery) newPodsByNodeInformer(plw *cache.ListWatch) cache.SharedInde
672672
indexers[nodeIndex] = func(obj interface{}) ([]string, error) {
673673
pod, ok := obj.(*apiv1.Pod)
674674
if !ok {
675-
return nil, fmt.Errorf("object is not a pod")
675+
return nil, errors.New("object is not a pod")
676676
}
677677
return []string{pod.Spec.NodeName}, nil
678678
}
@@ -686,7 +686,7 @@ func (d *Discovery) newEndpointsByNodeInformer(plw *cache.ListWatch) cache.Share
686686
indexers[podIndex] = func(obj interface{}) ([]string, error) {
687687
e, ok := obj.(*apiv1.Endpoints)
688688
if !ok {
689-
return nil, fmt.Errorf("object is not endpoints")
689+
return nil, errors.New("object is not endpoints")
690690
}
691691
var pods []string
692692
for _, target := range e.Subsets {
@@ -705,7 +705,7 @@ func (d *Discovery) newEndpointsByNodeInformer(plw *cache.ListWatch) cache.Share
705705
indexers[nodeIndex] = func(obj interface{}) ([]string, error) {
706706
e, ok := obj.(*apiv1.Endpoints)
707707
if !ok {
708-
return nil, fmt.Errorf("object is not endpoints")
708+
return nil, errors.New("object is not endpoints")
709709
}
710710
var nodes []string
711711
for _, target := range e.Subsets {
@@ -751,7 +751,7 @@ func (d *Discovery) newEndpointSlicesByNodeInformer(plw *cache.ListWatch, object
751751
}
752752
}
753753
default:
754-
return nil, fmt.Errorf("object is not an endpointslice")
754+
return nil, errors.New("object is not an endpointslice")
755755
}
756756

757757
return nodes, nil

discovery/linode/linode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ type Discovery struct {
141141
func NewDiscovery(conf *SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
142142
m, ok := metrics.(*linodeMetrics)
143143
if !ok {
144-
return nil, fmt.Errorf("invalid discovery metrics type")
144+
return nil, errors.New("invalid discovery metrics type")
145145
}
146146

147147
d := &Discovery{

discovery/manager_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ package discovery
1515

1616
import (
1717
"context"
18+
"errors"
1819
"fmt"
1920
"sort"
2021
"strconv"
@@ -1209,9 +1210,9 @@ func TestGaugeFailedConfigs(t *testing.T) {
12091210

12101211
c := map[string]Configs{
12111212
"prometheus": {
1212-
errorConfig{fmt.Errorf("tests error 0")},
1213-
errorConfig{fmt.Errorf("tests error 1")},
1214-
errorConfig{fmt.Errorf("tests error 2")},
1213+
errorConfig{errors.New("tests error 0")},
1214+
errorConfig{errors.New("tests error 1")},
1215+
errorConfig{errors.New("tests error 2")},
12151216
},
12161217
}
12171218
discoveryManager.ApplyConfig(c)

discovery/marathon/marathon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ type Discovery struct {
143143
func NewDiscovery(conf SDConfig, logger *slog.Logger, metrics discovery.DiscovererMetrics) (*Discovery, error) {
144144
m, ok := metrics.(*marathonMetrics)
145145
if !ok {
146-
return nil, fmt.Errorf("invalid discovery metrics type")
146+
return nil, errors.New("invalid discovery metrics type")
147147
}
148148

149149
rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "marathon_sd")

0 commit comments

Comments
 (0)