diff --git a/.golangci.yaml b/.golangci.yaml index cd20fee..42d0c27 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -2,7 +2,7 @@ linters: disable-all: true enable: # - errcheck -# - gosimple + - gosimple - govet - gofmt - gci diff --git a/histogram/types.go b/histogram/types.go index 8ca3f40..9f92a52 100644 --- a/histogram/types.go +++ b/histogram/types.go @@ -15,11 +15,7 @@ type Centroids []Centroid func (centroids Centroids) Compact() Centroids { tmp := make(map[float64]int) for _, c := range centroids { - if _, ok := tmp[c.Value]; ok { - tmp[c.Value] += c.Count - } else { - tmp[c.Value] = c.Count - } + tmp[c.Value] += c.Count } res := make(Centroids, len(tmp)) idx := 0 diff --git a/internal/auth/csp/fake_csp_handler.go b/internal/auth/csp/fake_csp_handler.go index edab6d9..1f4f163 100644 --- a/internal/auth/csp/fake_csp_handler.go +++ b/internal/auth/csp/fake_csp_handler.go @@ -78,7 +78,6 @@ func FakeCSPHandler(apiTokens []string) http.Handler { w.WriteHeader(http.StatusOK) marshal, _ := json.Marshal(sup) w.Write(marshal) - return }) return mux } diff --git a/internal/histogram/formatter_test.go b/internal/histogram/formatter_test.go index 14c27b0..3194f1a 100644 --- a/internal/histogram/formatter_test.go +++ b/internal/histogram/formatter_test.go @@ -1,13 +1,14 @@ package histogram import ( + "strings" "testing" "github.com/stretchr/testify/assert" "github.com/wavefronthq/wavefront-sdk-go/histogram" ) -var line string +var benchmarkLine string func BenchmarkHistogramLine(b *testing.B) { name := "request.latency" @@ -21,7 +22,7 @@ func BenchmarkHistogramLine(b *testing.B) { for n := 0; n < b.N; n++ { r, _ = HistogramLine(name, centroids, hgs, ts, src, tags, "") } - line = r + benchmarkLine = r } func TestHistogramLineCentroidsFormat(t *testing.T) { @@ -59,34 +60,36 @@ func TestHistogramLine(t *testing.T) { line, err := HistogramLine("request.latency", centroids, map[histogram.Granularity]bool{histogram.MINUTE: true}, 1533529977, "test_source", map[string]string{"env": "test"}, "") expected := "!M 1533529977 #20 30 \"request.latency\" source=\"test_source\" \"env\"=\"test\"\n" - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, expected, line) line, err = HistogramLine("request.latency", centroids, map[histogram.Granularity]bool{histogram.MINUTE: true, histogram.HOUR: false}, 1533529977, "", map[string]string{"env": "test"}, "default") expected = "!M 1533529977 #20 30 \"request.latency\" source=\"default\" \"env\"=\"test\"\n" - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, expected, line) line, err = HistogramLine("request.latency", centroids, map[histogram.Granularity]bool{histogram.HOUR: true, histogram.MINUTE: false}, 1533529977, "", map[string]string{"env": "test"}, "default") expected = "!H 1533529977 #20 30 \"request.latency\" source=\"default\" \"env\"=\"test\"\n" - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, expected, line) line, err = HistogramLine("request.latency", centroids, map[histogram.Granularity]bool{histogram.DAY: true}, 1533529977, "", map[string]string{"env": "test"}, "default") expected = "!D 1533529977 #20 30 \"request.latency\" source=\"default\" \"env\"=\"test\"\n" - assert.Nil(t, err) + assert.NoError(t, err) assert.Equal(t, expected, line) line, err = HistogramLine("request.latency", centroids, map[histogram.Granularity]bool{histogram.MINUTE: true, histogram.HOUR: true, histogram.DAY: false}, 1533529977, "test_source", map[string]string{"env": "test"}, "") - expected = "!M 1533529977 #20 30 \"request.latency\" source=\"test_source\" \"env\"=\"test\"\n" + - "!H 1533529977 #20 30 \"request.latency\" source=\"test_source\" \"env\"=\"test\"\n" - assert.Nil(t, err) - assert.Equal(t, expected, line) + assert.NoError(t, err) + assert.ElementsMatch(t, strings.Split(line, "\n")[0:2], []string{ + "!M 1533529977 #20 30 \"request.latency\" source=\"test_source\" \"env\"=\"test\"", + "!H 1533529977 #20 30 \"request.latency\" source=\"test_source\" \"env\"=\"test\"", + }) + } func makeCentroids() []histogram.Centroid { diff --git a/internal/lines.go b/internal/lines.go index 36ff9ff..51ac4b2 100644 --- a/internal/lines.go +++ b/internal/lines.go @@ -132,12 +132,19 @@ func (lh *RealLineHandler) HandleLine(line string) error { } } +func minInt(x, y int) int { + if x < y { + return x + } + return y +} + func (lh *RealLineHandler) Flush() error { lh.mtx.Lock() defer lh.mtx.Unlock() bufLen := len(lh.buffer) if bufLen > 0 { - size := min(bufLen, lh.BatchSize) + size := minInt(bufLen, lh.BatchSize) lines := make([]string, size) for i := 0; i < size; i++ { lines[i] = <-lh.buffer @@ -153,7 +160,7 @@ func (lh *RealLineHandler) FlushAll() error { bufLen := len(lh.buffer) if bufLen > 0 { var imod int - size := min(bufLen, lh.BatchSize) + size := minInt(bufLen, lh.BatchSize) lines := make([]string, size) for i := 0; i < bufLen; i++ { imod = i % size diff --git a/internal/reporter_test.go b/internal/reporter_test.go index 9702df9..de4e067 100644 --- a/internal/reporter_test.go +++ b/internal/reporter_test.go @@ -13,8 +13,7 @@ import ( ) func TestReporter_BuildRequest(t *testing.T) { - var r *reporter - r = NewReporter("http://localhost:8010/wavefront", auth.NewNoopTokenService(), &http.Client{}).(*reporter) + r := NewReporter("http://localhost:8010/wavefront", auth.NewNoopTokenService(), &http.Client{}).(*reporter) request, err := r.buildRequest("wavefront", nil) require.NoError(t, err) assert.Equal(t, "http://localhost:8010/wavefront/report?f=wavefront", request.URL.String()) diff --git a/internal/sanitize.go b/internal/sanitize.go index a1f57ed..020b2b1 100644 --- a/internal/sanitize.go +++ b/internal/sanitize.go @@ -5,6 +5,9 @@ import ( "strings" ) +var /* const */ quotation = regexp.MustCompile(`"`) +var /* const */ lineBreak = regexp.MustCompile(`\n`) + // Sanitize sanitizes string of metric name, source and key of tags according to the rule of Wavefront proxy. func Sanitize(str string) string { sb := GetBuffer() @@ -62,6 +65,3 @@ func SanitizeValue(str string) string { } return "\"" + lineBreak.ReplaceAllString(res, "\\n") + "\"" } - -var /* const */ quotation = regexp.MustCompile("\"") -var /* const */ lineBreak = regexp.MustCompile("\\n") diff --git a/internal/sdkmetrics/real_registry.go b/internal/sdkmetrics/real_registry.go index 09068fb..58000a8 100644 --- a/internal/sdkmetrics/real_registry.go +++ b/internal/sdkmetrics/real_registry.go @@ -100,17 +100,17 @@ func (registry *realRegistry) report() { defer registry.mtx.Unlock() for k, metric := range registry.metrics { - switch metric.(type) { + switch m := metric.(type) { case *DeltaCounter: - deltaCount := metric.(*DeltaCounter).count() + deltaCount := m.count() registry.sender.SendDeltaCounter(registry.prefix+"."+k, float64(deltaCount), "", registry.tags) metric.(*DeltaCounter).dec(deltaCount) case *MetricCounter: - registry.sender.SendMetric(registry.prefix+"."+k, float64(metric.(*MetricCounter).count()), 0, "", registry.tags) + registry.sender.SendMetric(registry.prefix+"."+k, float64(m.count()), 0, "", registry.tags) case *FunctionalGauge: - registry.sender.SendMetric(registry.prefix+"."+k, float64(metric.(*FunctionalGauge).instantValue()), 0, "", registry.tags) + registry.sender.SendMetric(registry.prefix+"."+k, float64(m.instantValue()), 0, "", registry.tags) case *FunctionalGaugeFloat64: - registry.sender.SendMetric(registry.prefix+"."+k, metric.(*FunctionalGaugeFloat64).instantValue(), 0, "", registry.tags) + registry.sender.SendMetric(registry.prefix+"."+k, m.instantValue(), 0, "", registry.tags) } } } diff --git a/internal/utils.go b/internal/utils.go index cb782d7..ea14014 100644 --- a/internal/utils.go +++ b/internal/utils.go @@ -7,7 +7,7 @@ import ( "strconv" ) -var semVerRegex = regexp.MustCompile("([0-9]\\d*)\\.(\\d+)\\.(\\d+)(?:-([a-zA-Z0-9]+))?") +var semVerRegex = regexp.MustCompile(`([0-9]\d*)\.(\d+)\.(\d+)(?:-([a-zA-Z0-9]+))?`) func GetHostname(defaultVal string) string { hostname, err := os.Hostname() @@ -17,13 +17,6 @@ func GetHostname(defaultVal string) string { return hostname } -func min(x, y int) int { - if x < y { - return x - } - return y -} - func GetSemVer(version string) (float64, error) { if len(version) > 0 { res := semVerRegex.FindStringSubmatch(version) diff --git a/senders/option.go b/senders/option.go index 5c706be..b8bfe27 100644 --- a/senders/option.go +++ b/senders/option.go @@ -25,13 +25,11 @@ type CSPOption func(any) // CSPBaseURL sets an alternative base URL for the CSP server func CSPBaseURL(baseURL string) CSPOption { return func(authentication any) { - switch authentication.(type) { + switch a := authentication.(type) { case *auth.CSPClientCredentials: - credentials := authentication.(*auth.CSPClientCredentials) - credentials.BaseURL = baseURL + a.BaseURL = baseURL case *auth.CSPAPIToken: - token := authentication.(*auth.CSPAPIToken) - token.BaseURL = baseURL + a.BaseURL = baseURL } } } @@ -39,10 +37,9 @@ func CSPBaseURL(baseURL string) CSPOption { // CSPOrgID sets an explicit orgID for Client Credentials authentication func CSPOrgID(orgID string) CSPOption { return func(authentication any) { - switch authentication.(type) { + switch a := authentication.(type) { case auth.CSPClientCredentials: - credentials := authentication.(auth.CSPClientCredentials) - credentials.OrgID = &orgID + a.OrgID = &orgID } } }