Skip to content

Commit

Permalink
chore: enable gosimple linter and resolve linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeWinikates committed Sep 21, 2023
1 parent e4d543b commit f879fca
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ linters:
disable-all: true
enable:
# - errcheck
# - gosimple
- gosimple
- govet
- gofmt
- gci
Expand Down
6 changes: 1 addition & 5 deletions histogram/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion internal/auth/csp/fake_csp_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func FakeCSPHandler(apiTokens []string) http.Handler {
w.WriteHeader(http.StatusOK)
marshal, _ := json.Marshal(sup)
w.Write(marshal)
return
})
return mux
}
23 changes: 13 additions & 10 deletions internal/histogram/formatter_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 9 additions & 2 deletions internal/lines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions internal/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions internal/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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")
10 changes: 5 additions & 5 deletions internal/sdkmetrics/real_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
13 changes: 5 additions & 8 deletions senders/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,21 @@ 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
}
}
}

// 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
}
}
}
Expand Down

0 comments on commit f879fca

Please sign in to comment.