From 2117b1f819a4a613830aab1af92e5a0ad9c9ea71 Mon Sep 17 00:00:00 2001 From: Luke Winikates Date: Thu, 16 Nov 2023 10:39:42 -0800 Subject: [PATCH 1/2] wip: enable a bunch of new linting rules at once --- .golangci.yaml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.golangci.yaml b/.golangci.yaml index 82360a2..231f534 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -9,9 +9,18 @@ linters: - revive - ineffassign - staticcheck + - misspell + - gocyclo + - typecheck + - megacheck + - goimports + - goconst + - unconvert + - gocritic issues: exclude-rules: - path: example.*_test\.go linters: - ineffassign - revive + exclude-use-default: false From 617079c777457bab78c60665a12bc38a0a7020de Mon Sep 17 00:00:00 2001 From: Luke Winikates Date: Thu, 16 Nov 2023 11:27:29 -0800 Subject: [PATCH 2/2] wip --- .golangci.yaml | 18 ++++++------- application/heartbeater.go | 2 +- internal/sdkmetrics/registry_test.go | 8 +++--- internal/span/formatter.go | 5 ++-- senders/auth.go | 11 +++----- senders/option.go | 3 +-- senders/real_sender.go | 40 +++++++++++++--------------- 7 files changed, 40 insertions(+), 47 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index 231f534..f0ea37e 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -2,21 +2,21 @@ linters: disable-all: true enable: - errcheck + - gci +# - goconst + - gocritic + - gocyclo + - gofmt + - goimports - gosimple - govet - - gofmt - - gci - - revive - ineffassign - - staticcheck + - megacheck - misspell - - gocyclo + - revive + - staticcheck - typecheck - - megacheck - - goimports - - goconst - unconvert - - gocritic issues: exclude-rules: - path: example.*_test\.go diff --git a/application/heartbeater.go b/application/heartbeater.go index e30c023..6be6317 100644 --- a/application/heartbeater.go +++ b/application/heartbeater.go @@ -67,7 +67,7 @@ func (hb *heartbeater) beat() { hb.send(tags) } - //send customTags heartbeat + // send customTags heartbeat hb.mux.Lock() for len(hb.customTags) > 0 { tags := hb.customTags[0] diff --git a/internal/sdkmetrics/registry_test.go b/internal/sdkmetrics/registry_test.go index 8befb5b..d2356ca 100644 --- a/internal/sdkmetrics/registry_test.go +++ b/internal/sdkmetrics/registry_test.go @@ -20,17 +20,17 @@ func (f *fakeSender) SendDeltaCounter(string, float64, string, map[string]string func (f *fakeSender) SendMetric(name string, _ float64, _ int64, _ string, tags map[string]string) error { f.count = f.count + 1 if f.prefix != "" && !strings.HasPrefix(name, f.prefix) { - f.errors = f.errors + 1 + f.errors++ } if f.name != "" && f.prefix != "" && (name != f.prefix+"."+f.name) { - f.errors = f.errors + 1 + f.errors++ } for _, k := range f.tags { if tags == nil { - f.errors = f.errors + 1 + f.errors++ } else { if _, ok := tags[k]; !ok { - f.errors = f.errors + 1 + f.errors++ } } } diff --git a/internal/span/formatter.go b/internal/span/formatter.go index 2e45816..2e63cea 100644 --- a/internal/span/formatter.go +++ b/internal/span/formatter.go @@ -9,7 +9,7 @@ import ( "github.com/wavefronthq/wavefront-sdk-go/internal" ) -// Line gets a span line in the Wavefront span data format: +// Line returns a span line in the Wavefront span data format: // source= [pointTags] // Example: // "getAllUsers source=localhost traceId=7b3bf470-9456-11e8-9eb6-529269fb1459 spanId=0313bafe-9457-11e8-9eb6-529269fb1459 @@ -80,6 +80,7 @@ func Line(name string, startMillis, durationMillis int64, source, traceID, spanI return sb.String(), nil } +// LogJSON returns Span Logs as a string in JSON format, func LogJSON(traceID, spanID string, spanLogs []Log, span string) (string, error) { l := Logs{ TraceID: traceID, @@ -91,7 +92,7 @@ func LogJSON(traceID, spanID string, spanLogs []Log, span string) (string, error if err != nil { return "", err } - return string(out[:]) + "\n", nil + return string(out) + "\n", nil } func isUUIDFormat(str string) bool { diff --git a/senders/auth.go b/senders/auth.go index 53fc774..93e2b9d 100644 --- a/senders/auth.go +++ b/senders/auth.go @@ -7,19 +7,16 @@ import ( ) func tokenServiceForCfg(cfg *configuration) auth.Service { - switch cfg.Authentication.(type) { + switch a := cfg.Authentication.(type) { case auth.APIToken: log.Println("The Wavefront SDK will use Direct Ingestion authenticated using an API Token.") - tokenAuth := cfg.Authentication.(auth.APIToken) - return auth.NewWavefrontTokenService(tokenAuth.Token) + return auth.NewWavefrontTokenService(a.Token) case auth.CSPClientCredentials: log.Println("The Wavefront SDK will use Direct Ingestion authenticated using CSP client credentials.") - cspAuth := cfg.Authentication.(auth.CSPClientCredentials) - return auth.NewCSPServerToServerService(cspAuth.BaseURL, cspAuth.ClientID, cspAuth.ClientSecret, cspAuth.OrgID) + return auth.NewCSPServerToServerService(a.BaseURL, a.ClientID, a.ClientSecret, a.OrgID) case auth.CSPAPIToken: log.Println("The Wavefront SDK will use Direct Ingestion authenticated using CSP API Token.") - cspAuth := cfg.Authentication.(auth.CSPAPIToken) - return auth.NewCSPTokenService(cspAuth.BaseURL, cspAuth.Token) + return auth.NewCSPTokenService(a.BaseURL, a.Token) } log.Println("The Wavefront SDK will communicate with a Wavefront Proxy.") diff --git a/senders/option.go b/senders/option.go index c93722c..eb0ac2e 100644 --- a/senders/option.go +++ b/senders/option.go @@ -44,8 +44,7 @@ func CSPBaseURL(baseURL string) CSPOption { // CSPOrgID sets an explicit orgID for Client Credentials authentication func CSPOrgID(orgID string) CSPOption { return func(authentication any) { - switch a := authentication.(type) { - case auth.CSPClientCredentials: + if a, ok := authentication.(auth.CSPClientCredentials); ok { a.OrgID = &orgID } } diff --git a/senders/real_sender.go b/senders/real_sender.go index 45ee48c..b73491f 100644 --- a/senders/real_sender.go +++ b/senders/real_sender.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "strconv" + "strings" "github.com/wavefronthq/wavefront-sdk-go/event" "github.com/wavefronthq/wavefront-sdk-go/histogram" @@ -197,29 +198,24 @@ func (sender *realSender) Close() { } func (sender *realSender) Flush() error { - errStr := "" - err := sender.pointHandler.Flush() - if err != nil { - errStr = errStr + err.Error() + "\n" - } - err = sender.histoHandler.Flush() - if err != nil { - errStr = errStr + err.Error() + "\n" - } - err = sender.spanHandler.Flush() - if err != nil { - errStr = errStr + err.Error() - } - err = sender.spanLogHandler.Flush() - if err != nil { - errStr = errStr + err.Error() - } - err = sender.eventHandler.Flush() - if err != nil { - errStr = errStr + err.Error() + return combineErrorMessages( + sender.pointHandler.Flush(), + sender.histoHandler.Flush(), + sender.spanHandler.Flush(), + sender.spanLogHandler.Flush(), + sender.eventHandler.Flush(), + ) +} + +func combineErrorMessages(errs ...error) error { + var nonNilErrMessages []string + for _, err := range errs { + if err != nil { + nonNilErrMessages = append(nonNilErrMessages, err.Error()) + } } - if errStr != "" { - return fmt.Errorf(errStr) + if len(nonNilErrMessages) > 0 { + return fmt.Errorf(strings.Join(nonNilErrMessages, "\n")) } return nil }