From 74937a2952b451f6cb3dfbb1a547a3d2be056f02 Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Mon, 18 Nov 2024 10:04:50 +0100 Subject: [PATCH] [chore]: enable all rules of perfsprint (#5978) #### Description [perfsprint](https://github.com/catenacyber/perfsprint) is a linter for performance, aiming at usages of fmt.Sprintf which have faster alternatives. Signed-off-by: Matthieu MOREL --- .golangci.yml | 8 +++---- bridge/opencensus/internal/span.go | 2 +- codes/codes.go | 3 ++- .../otlpmetric/otlpmetricgrpc/exporter.go | 3 ++- .../otlpmetricgrpc/internal/oconf/options.go | 2 +- .../otlp/otlpmetric/otlpmetrichttp/client.go | 2 +- .../otlpmetric/otlpmetrichttp/exporter.go | 3 ++- .../otlpmetrichttp/internal/oconf/options.go | 2 +- .../internal/otlpconfig/options.go | 2 +- .../otlp/otlptrace/otlptracehttp/client.go | 2 +- .../internal/otlpconfig/options.go | 2 +- .../otlp/otlpmetric/oconf/options.go.tmpl | 2 +- .../otlp/otlptrace/otlpconfig/options.go.tmpl | 2 +- sdk/metric/exporter.go | 4 ++-- sdk/metric/meter.go | 2 +- .../metricdata/metricdatatest/comparisons.go | 22 +++++++++---------- sdk/metric/reader.go | 10 ++++----- sdk/trace/sampler_env.go | 5 ++--- 18 files changed, 40 insertions(+), 38 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index a187ba4a7f5..b2df42e5d8c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -157,11 +157,11 @@ linters-settings: ignore-words: - cancelled perfsprint: - err-error: false - errorf: false + err-error: true + errorf: true int-conversion: true - sprintf1: false - strconcat: false + sprintf1: true + strconcat: true revive: # Sets the default failure confidence. # This means that linting errors with less than 0.8 confidence will be ignored. diff --git a/bridge/opencensus/internal/span.go b/bridge/opencensus/internal/span.go index e3311ee98f3..3ddef4d037e 100644 --- a/bridge/opencensus/internal/span.go +++ b/bridge/opencensus/internal/span.go @@ -128,5 +128,5 @@ func (s *Span) AddLink(l octrace.Link) { // String prints a string representation of this span. func (s *Span) String() string { - return fmt.Sprintf("span %s", s.otelSpan.SpanContext().SpanID().String()) + return "span " + s.otelSpan.SpanContext().SpanID().String() } diff --git a/codes/codes.go b/codes/codes.go index 2acbac35466..49a35b12255 100644 --- a/codes/codes.go +++ b/codes/codes.go @@ -5,6 +5,7 @@ package codes // import "go.opentelemetry.io/otel/codes" import ( "encoding/json" + "errors" "fmt" "strconv" ) @@ -63,7 +64,7 @@ func (c *Code) UnmarshalJSON(b []byte) error { return nil } if c == nil { - return fmt.Errorf("nil receiver passed to UnmarshalJSON") + return errors.New("nil receiver passed to UnmarshalJSON") } var x interface{} diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/exporter.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/exporter.go index 98afc0b1e9d..3977c1f8a6c 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/exporter.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/exporter.go @@ -5,6 +5,7 @@ package otlpmetricgrpc // import "go.opentelemetry.io/otel/exporters/otlp/otlpme import ( "context" + "errors" "fmt" "sync" @@ -114,7 +115,7 @@ func (e *Exporter) Shutdown(ctx context.Context) error { return err } -var errShutdown = fmt.Errorf("gRPC exporter is shutdown") +var errShutdown = errors.New("gRPC exporter is shutdown") type shutdownClient struct{} diff --git a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go index c016b4dbe97..2ac8db5a887 100644 --- a/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go +++ b/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go @@ -111,7 +111,7 @@ func cleanPath(urlPath string, defaultPath string) string { return defaultPath } if !path.IsAbs(tmp) { - tmp = fmt.Sprintf("/%s", tmp) + tmp = "/" + tmp } return tmp } diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go index f36388f45af..7d1315dad86 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/client.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/client.go @@ -305,7 +305,7 @@ func newResponseError(header http.Header, wrapped error) error { func (e retryableError) Error() string { if e.err != nil { - return fmt.Sprintf("retry-able request failure: %s", e.err.Error()) + return "retry-able request failure: " + e.err.Error() } return "retry-able request failure" diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/exporter.go b/exporters/otlp/otlpmetric/otlpmetrichttp/exporter.go index 701deb6d390..50ac8f86ea3 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/exporter.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/exporter.go @@ -5,6 +5,7 @@ package otlpmetrichttp // import "go.opentelemetry.io/otel/exporters/otlp/otlpme import ( "context" + "errors" "fmt" "sync" @@ -114,7 +115,7 @@ func (e *Exporter) Shutdown(ctx context.Context) error { return err } -var errShutdown = fmt.Errorf("HTTP exporter is shutdown") +var errShutdown = errors.New("HTTP exporter is shutdown") type shutdownClient struct{} diff --git a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go index 33363193de5..db595e49ec2 100644 --- a/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go +++ b/exporters/otlp/otlpmetric/otlpmetrichttp/internal/oconf/options.go @@ -111,7 +111,7 @@ func cleanPath(urlPath string, defaultPath string) string { return defaultPath } if !path.IsAbs(tmp) { - tmp = fmt.Sprintf("/%s", tmp) + tmp = "/" + tmp } return tmp } diff --git a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go index 3ee452ef72b..0a317d92637 100644 --- a/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracegrpc/internal/otlpconfig/options.go @@ -98,7 +98,7 @@ func cleanPath(urlPath string, defaultPath string) string { return defaultPath } if !path.IsAbs(tmp) { - tmp = fmt.Sprintf("/%s", tmp) + tmp = "/" + tmp } return tmp } diff --git a/exporters/otlp/otlptrace/otlptracehttp/client.go b/exporters/otlp/otlptrace/otlptracehttp/client.go index 38fabf1b660..00adf00847b 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/client.go +++ b/exporters/otlp/otlptrace/otlptracehttp/client.go @@ -327,7 +327,7 @@ func newResponseError(header http.Header, wrapped error) error { func (e retryableError) Error() string { if e.err != nil { - return fmt.Sprintf("retry-able request failure: %s", e.err.Error()) + return "retry-able request failure: " + e.err.Error() } return "retry-able request failure" diff --git a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go index 131906b1ff3..6a9c4d3a652 100644 --- a/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go +++ b/exporters/otlp/otlptrace/otlptracehttp/internal/otlpconfig/options.go @@ -98,7 +98,7 @@ func cleanPath(urlPath string, defaultPath string) string { return defaultPath } if !path.IsAbs(tmp) { - tmp = fmt.Sprintf("/%s", tmp) + tmp = "/" + tmp } return tmp } diff --git a/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl b/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl index f6a21c6137e..c862f64e2de 100644 --- a/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl +++ b/internal/shared/otlp/otlpmetric/oconf/options.go.tmpl @@ -111,7 +111,7 @@ func cleanPath(urlPath string, defaultPath string) string { return defaultPath } if !path.IsAbs(tmp) { - tmp = fmt.Sprintf("/%s", tmp) + tmp = "/" + tmp } return tmp } diff --git a/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl b/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl index e99663f3cfd..4118e156e5a 100644 --- a/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl +++ b/internal/shared/otlp/otlptrace/otlpconfig/options.go.tmpl @@ -98,7 +98,7 @@ func cleanPath(urlPath string, defaultPath string) string { return defaultPath } if !path.IsAbs(tmp) { - tmp = fmt.Sprintf("/%s", tmp) + tmp = "/" + tmp } return tmp } diff --git a/sdk/metric/exporter.go b/sdk/metric/exporter.go index 1a3cccb6775..1969cb42cf4 100644 --- a/sdk/metric/exporter.go +++ b/sdk/metric/exporter.go @@ -5,14 +5,14 @@ package metric // import "go.opentelemetry.io/otel/sdk/metric" import ( "context" - "fmt" + "errors" "go.opentelemetry.io/otel/sdk/metric/metricdata" ) // ErrExporterShutdown is returned if Export or Shutdown are called after an // Exporter has been Shutdown. -var ErrExporterShutdown = fmt.Errorf("exporter is shutdown") +var ErrExporterShutdown = errors.New("exporter is shutdown") // Exporter handles the delivery of metric data to external receivers. This is // the final component in the metric push pipeline. diff --git a/sdk/metric/meter.go b/sdk/metric/meter.go index 823cdf2c62f..a6ccd117b80 100644 --- a/sdk/metric/meter.go +++ b/sdk/metric/meter.go @@ -475,7 +475,7 @@ func (m *meter) RegisterCallback(f metric.Callback, insts ...metric.Observable) validInstruments = append(validInstruments, inst) default: // Instrument external to the SDK. - return nil, fmt.Errorf("invalid observable: from different implementation") + return nil, errors.New("invalid observable: from different implementation") } } diff --git a/sdk/metric/metricdata/metricdatatest/comparisons.go b/sdk/metric/metricdata/metricdatatest/comparisons.go index 75daa8171de..68f9649f6b1 100644 --- a/sdk/metric/metricdata/metricdatatest/comparisons.go +++ b/sdk/metric/metricdata/metricdatatest/comparisons.go @@ -32,7 +32,7 @@ func equalResourceMetrics(a, b metricdata.ResourceMetrics, cfg config) (reasons }, )) if r != "" { - reasons = append(reasons, fmt.Sprintf("ResourceMetrics ScopeMetrics not equal:\n%s", r)) + reasons = append(reasons, "ResourceMetrics ScopeMetrics not equal:\n"+r) } return reasons } @@ -56,7 +56,7 @@ func equalScopeMetrics(a, b metricdata.ScopeMetrics, cfg config) (reasons []stri }, )) if r != "" { - reasons = append(reasons, fmt.Sprintf("ScopeMetrics Metrics not equal:\n%s", r)) + reasons = append(reasons, "ScopeMetrics Metrics not equal:\n"+r) } return reasons } @@ -172,7 +172,7 @@ func equalGauges[N int64 | float64](a, b metricdata.Gauge[N], cfg config) (reaso }, )) if r != "" { - reasons = append(reasons, fmt.Sprintf("Gauge DataPoints not equal:\n%s", r)) + reasons = append(reasons, "Gauge DataPoints not equal:\n"+r) } return reasons } @@ -199,7 +199,7 @@ func equalSums[N int64 | float64](a, b metricdata.Sum[N], cfg config) (reasons [ }, )) if r != "" { - reasons = append(reasons, fmt.Sprintf("Sum DataPoints not equal:\n%s", r)) + reasons = append(reasons, "Sum DataPoints not equal:\n"+r) } return reasons } @@ -223,7 +223,7 @@ func equalHistograms[N int64 | float64](a, b metricdata.Histogram[N], cfg config }, )) if r != "" { - reasons = append(reasons, fmt.Sprintf("Histogram DataPoints not equal:\n%s", r)) + reasons = append(reasons, "Histogram DataPoints not equal:\n"+r) } return reasons } @@ -264,7 +264,7 @@ func equalDataPoints[N int64 | float64](a, b metricdata.DataPoint[N], cfg config }, )) if r != "" { - reasons = append(reasons, fmt.Sprintf("Exemplars not equal:\n%s", r)) + reasons = append(reasons, "Exemplars not equal:\n"+r) } } return reasons @@ -318,7 +318,7 @@ func equalHistogramDataPoints[N int64 | float64](a, b metricdata.HistogramDataPo }, )) if r != "" { - reasons = append(reasons, fmt.Sprintf("Exemplars not equal:\n%s", r)) + reasons = append(reasons, "Exemplars not equal:\n"+r) } } return reasons @@ -343,7 +343,7 @@ func equalExponentialHistograms[N int64 | float64](a, b metricdata.ExponentialHi }, )) if r != "" { - reasons = append(reasons, fmt.Sprintf("Histogram DataPoints not equal:\n%s", r)) + reasons = append(reasons, "Histogram DataPoints not equal:\n"+r) } return reasons } @@ -406,7 +406,7 @@ func equalExponentialHistogramDataPoints[N int64 | float64](a, b metricdata.Expo }, )) if r != "" { - reasons = append(reasons, fmt.Sprintf("Exemplars not equal:\n%s", r)) + reasons = append(reasons, "Exemplars not equal:\n"+r) } } return reasons @@ -432,7 +432,7 @@ func equalSummary(a, b metricdata.Summary, cfg config) (reasons []string) { }, )) if r != "" { - reasons = append(reasons, fmt.Sprintf("Summary DataPoints not equal:\n%s", r)) + reasons = append(reasons, "Summary DataPoints not equal:\n"+r) } return reasons } @@ -634,7 +634,7 @@ func compareDiff[T any](extraExpected, extraActual []T) string { } func missingAttrStr(name string) string { - return fmt.Sprintf("missing attribute %s", name) + return "missing attribute " + name } func hasAttributesExemplars[T int64 | float64](exemplar metricdata.Exemplar[T], attrs ...attribute.KeyValue) (reasons []string) { diff --git a/sdk/metric/reader.go b/sdk/metric/reader.go index d94bdee75b7..9c54561aac7 100644 --- a/sdk/metric/reader.go +++ b/sdk/metric/reader.go @@ -5,26 +5,26 @@ package metric // import "go.opentelemetry.io/otel/sdk/metric" import ( "context" - "fmt" + "errors" "go.opentelemetry.io/otel/sdk/metric/metricdata" ) // errDuplicateRegister is logged by a Reader when an attempt to registered it // more than once occurs. -var errDuplicateRegister = fmt.Errorf("duplicate reader registration") +var errDuplicateRegister = errors.New("duplicate reader registration") // ErrReaderNotRegistered is returned if Collect or Shutdown are called before // the reader is registered with a MeterProvider. -var ErrReaderNotRegistered = fmt.Errorf("reader is not registered") +var ErrReaderNotRegistered = errors.New("reader is not registered") // ErrReaderShutdown is returned if Collect or Shutdown are called after a // reader has been Shutdown once. -var ErrReaderShutdown = fmt.Errorf("reader is shutdown") +var ErrReaderShutdown = errors.New("reader is shutdown") // errNonPositiveDuration is logged when an environmental variable // has non-positive value. -var errNonPositiveDuration = fmt.Errorf("non-positive duration") +var errNonPositiveDuration = errors.New("non-positive duration") // Reader is the interface used between the SDK and an // exporter. Control flow is bi-directional through the diff --git a/sdk/trace/sampler_env.go b/sdk/trace/sampler_env.go index d2d1f72466b..9b672a1d70d 100644 --- a/sdk/trace/sampler_env.go +++ b/sdk/trace/sampler_env.go @@ -5,7 +5,6 @@ package trace // import "go.opentelemetry.io/otel/sdk/trace" import ( "errors" - "fmt" "os" "strconv" "strings" @@ -26,7 +25,7 @@ const ( type errUnsupportedSampler string func (e errUnsupportedSampler) Error() string { - return fmt.Sprintf("unsupported sampler: %s", string(e)) + return "unsupported sampler: " + string(e) } var ( @@ -39,7 +38,7 @@ type samplerArgParseError struct { } func (e samplerArgParseError) Error() string { - return fmt.Sprintf("parsing sampler argument: %s", e.parseErr.Error()) + return "parsing sampler argument: " + e.parseErr.Error() } func (e samplerArgParseError) Unwrap() error {