From 61e9b35139e7e57ea89aafc7dd81756befd0a37f Mon Sep 17 00:00:00 2001 From: Matthieu MOREL Date: Thu, 14 Nov 2024 19:17:37 +0100 Subject: [PATCH] [chore]: enable int-conversion rule of perfsprint (#5964) [perfsprint](https://github.com/catenacyber/perfsprint) is a linter for performance, aiming at usages of fmt.Sprintf which have faster alternatives. This PR enables int-conversion rule Signed-off-by: Matthieu MOREL --- .golangci.yml | 10 +++++++++- bridge/opentracing/bridge.go | 5 +++-- exporters/zipkin/internal/internaltest/harness.go | 3 ++- internal/internaltest/harness.go | 3 ++- internal/shared/internaltest/harness.go.tmpl | 3 ++- sdk/internal/internaltest/harness.go | 3 ++- 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index dbfb2a165a0..a187ba4a7f5 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -22,6 +22,7 @@ linters: - govet - ineffassign - misspell + - perfsprint - revive - staticcheck - tenv @@ -61,10 +62,11 @@ issues: text: "calls to (.+) only in main[(][)] or init[(][)] functions" linters: - revive - # It's okay to not run gosec in a test. + # It's okay to not run gosec and perfsprint in a test. - path: _test\.go linters: - gosec + - perfsprint # Ignoring gosec G404: Use of weak random number generator (math/rand instead of crypto/rand) # as we commonly use it in tests and examples. - text: "G404:" @@ -154,6 +156,12 @@ linters-settings: locale: US ignore-words: - cancelled + perfsprint: + err-error: false + errorf: false + int-conversion: true + sprintf1: false + strconcat: false revive: # Sets the default failure confidence. # This means that linting errors with less than 0.8 confidence will be ignored. diff --git a/bridge/opentracing/bridge.go b/bridge/opentracing/bridge.go index 8aee5567f9e..848f028c87f 100644 --- a/bridge/opentracing/bridge.go +++ b/bridge/opentracing/bridge.go @@ -6,6 +6,7 @@ package opentracing // import "go.opentelemetry.io/otel/bridge/opentracing" import ( "context" "fmt" + "strconv" "strings" "sync" @@ -532,7 +533,7 @@ func otTagToOTelAttr(k string, v interface{}) attribute.KeyValue { case int64: return key.Int64(val) case uint64: - return key.String(fmt.Sprintf("%d", val)) + return key.String(strconv.FormatUint(val, 10)) case float64: return key.Float64(val) case int8: @@ -552,7 +553,7 @@ func otTagToOTelAttr(k string, v interface{}) attribute.KeyValue { case int: return key.Int(val) case uint: - return key.String(fmt.Sprintf("%d", val)) + return key.String(strconv.FormatUint(uint64(val), 10)) case string: return key.String(val) default: diff --git a/exporters/zipkin/internal/internaltest/harness.go b/exporters/zipkin/internal/internaltest/harness.go index 5f335d69351..268b95e41e0 100644 --- a/exporters/zipkin/internal/internaltest/harness.go +++ b/exporters/zipkin/internal/internaltest/harness.go @@ -9,6 +9,7 @@ package internaltest // import "go.opentelemetry.io/otel/exporters/zipkin/intern import ( "context" "fmt" + "strconv" "sync" "testing" "time" @@ -58,7 +59,7 @@ func (h *Harness) TestTracerProvider(subjectFactory func() trace.TracerProvider) go func(name, version string) { _ = tp.Tracer(name, trace.WithInstrumentationVersion(version)) wg.Done() - }(fmt.Sprintf("tracer %d", i%5), fmt.Sprintf("%d", i)) + }(fmt.Sprintf("tracer %d", i%5), strconv.Itoa(i)) } wg.Wait() done <- struct{}{} diff --git a/internal/internaltest/harness.go b/internal/internaltest/harness.go index c2e4a795804..bff592a929a 100644 --- a/internal/internaltest/harness.go +++ b/internal/internaltest/harness.go @@ -9,6 +9,7 @@ package internaltest // import "go.opentelemetry.io/otel/internal/internaltest" import ( "context" "fmt" + "strconv" "sync" "testing" "time" @@ -58,7 +59,7 @@ func (h *Harness) TestTracerProvider(subjectFactory func() trace.TracerProvider) go func(name, version string) { _ = tp.Tracer(name, trace.WithInstrumentationVersion(version)) wg.Done() - }(fmt.Sprintf("tracer %d", i%5), fmt.Sprintf("%d", i)) + }(fmt.Sprintf("tracer %d", i%5), strconv.Itoa(i)) } wg.Wait() done <- struct{}{} diff --git a/internal/shared/internaltest/harness.go.tmpl b/internal/shared/internaltest/harness.go.tmpl index 36db35476b4..1cb55aaf2bd 100644 --- a/internal/shared/internaltest/harness.go.tmpl +++ b/internal/shared/internaltest/harness.go.tmpl @@ -9,6 +9,7 @@ package internaltest import ( "context" "fmt" + "strconv" "sync" "testing" "time" @@ -58,7 +59,7 @@ func (h *Harness) TestTracerProvider(subjectFactory func() trace.TracerProvider) go func(name, version string) { _ = tp.Tracer(name, trace.WithInstrumentationVersion(version)) wg.Done() - }(fmt.Sprintf("tracer %d", i%5), fmt.Sprintf("%d", i)) + }(fmt.Sprintf("tracer %d", i%5), strconv.Itoa(i)) } wg.Wait() done <- struct{}{} diff --git a/sdk/internal/internaltest/harness.go b/sdk/internal/internaltest/harness.go index ecc754d9d43..eb041a5506d 100644 --- a/sdk/internal/internaltest/harness.go +++ b/sdk/internal/internaltest/harness.go @@ -9,6 +9,7 @@ package internaltest // import "go.opentelemetry.io/otel/sdk/internal/internalte import ( "context" "fmt" + "strconv" "sync" "testing" "time" @@ -58,7 +59,7 @@ func (h *Harness) TestTracerProvider(subjectFactory func() trace.TracerProvider) go func(name, version string) { _ = tp.Tracer(name, trace.WithInstrumentationVersion(version)) wg.Done() - }(fmt.Sprintf("tracer %d", i%5), fmt.Sprintf("%d", i)) + }(fmt.Sprintf("tracer %d", i%5), strconv.Itoa(i)) } wg.Wait() done <- struct{}{}