Skip to content

Commit

Permalink
Enable staticcheck, fix issues (#180)
Browse files Browse the repository at this point in the history
- tools: Add and enable staticcheck
- Use copy() to copy slice
- Delete unused fields
- scope_benchmark_test: Fix unused append
- example/statds_main: Don't use deprecated API
- m3/resource_pool.go: Ignore releaseProto is unused error
- m3/reporter.go: Ignore SA6002
  • Loading branch information
abhinav authored Jun 21, 2022
1 parent 2affafb commit 86b6b09
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 26 deletions.
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ MODULES = . ./tools

LINT_IGNORE = m3/thrift\|thirdparty
LICENSE_IGNORE = m3/thrift\|thirdparty
STATICCHECK_IGNORE = m3/thrift\|thirdparty\|m3/resource_pool.go:.*releaseProto is unused\|m3/reporter.go:.* argument should be pointer-like to avoid allocations

GOLINT = $(GOBIN)/golint
STATICCHECK = $(GOBIN)/staticcheck

.PHONY: all
all: lint test

.PHONY: lint
lint: gofmt golint gomodtidy license
lint: gofmt golint gomodtidy staticcheck license

.PHONY: golint
golint: $(GOLINT)
Expand All @@ -30,6 +32,18 @@ golint: $(GOLINT)
$(GOLINT): tools/go.mod
cd tools && go install golang.org/x/lint/golint

.PHONY: staticcheck
staticcheck: $(STATICCHECK)
@echo "Checking staticcheck..."
@$(eval LOG := $(shell mktemp -t log.XXXXX))
@$(STATICCHECK) ./... | grep -v '$(STATICCHECK_IGNORE)' > $(LOG) || true
@[ ! -s "$(LOG)" ] || \
(echo "staticcheck failed:" | \
cat - $(LOG) && false)

$(STATICCHECK):
cd tools && go install honnef.co/go/tools/cmd/staticcheck

.PHONY: gofmt
gofmt:
@echo "Checking formatting..."
Expand Down
1 change: 0 additions & 1 deletion instrument/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func NewCall(scope tally.Scope, name string) Call {
}

type call struct {
scope tally.Scope
success tally.Counter
err tally.Counter
timing tally.Timer
Expand Down
9 changes: 3 additions & 6 deletions m3/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,9 @@ func (f *fakeM3Server) Packets() [][]byte {
f.packets.Lock()
defer f.packets.Unlock()

copy := make([][]byte, len(f.packets.values))
for i, packet := range f.packets.values {
copy[i] = packet
}

return copy
packets := make([][]byte, len(f.packets.values))
copy(packets, f.packets.values)
return packets
}

func newFakeM3Service(wg *sync.WaitGroup, countBatches bool) *fakeM3Service {
Expand Down
5 changes: 2 additions & 3 deletions multi/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,8 @@ func (r multiBaseReporters) Flush() {
}

type capabilities struct {
reporting bool
tagging bool
histograms bool
reporting bool
tagging bool
}

func (c *capabilities) Reporting() bool {
Expand Down
1 change: 0 additions & 1 deletion prometheus/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ type reporter struct {
counters map[metricID]*prom.CounterVec
gauges map[metricID]*prom.GaugeVec
timers map[metricID]*promTimerVec
histograms map[metricID]*prom.HistogramVec
}

type promTimerVec struct {
Expand Down
1 change: 1 addition & 0 deletions scope_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ func benchmarkScopeReportingN(b *testing.B, numElems int) {
ids = append(ids, id)
s.Counter(id)
}
_ = ids
b.ResetTimer()

for n := 0; n < b.N; n++ {
Expand Down
2 changes: 0 additions & 2 deletions scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ type testStatsReporter struct {
tg sync.WaitGroup
hg sync.WaitGroup

scope Scope

counters map[string]*testIntValue
gauges map[string]*testFloatValue
timers map[string]*testIntValue
Expand Down
10 changes: 3 additions & 7 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ func (t *timer) RecordStopwatch(stopwatchStart time.Time) {
func (t *timer) snapshot() []time.Duration {
t.unreported.RLock()
snap := make([]time.Duration, len(t.unreported.values))
for i := range t.unreported.values {
snap[i] = t.unreported.values[i]
}
copy(snap, t.unreported.values)
t.unreported.RUnlock()
return snap
}
Expand Down Expand Up @@ -432,10 +430,8 @@ func (h *histogram) snapshotDurations() map[time.Duration]int64 {
}

type histogramBucket struct {
valueUpperBound float64
durationUpperBound time.Duration
cachedValueBucket CachedHistogramBucket
cachedDurationBucket CachedHistogramBucket
valueUpperBound float64
durationUpperBound time.Duration
}

func durationLowerBound(buckets []histogramBucket, i int) time.Duration {
Expand Down
9 changes: 7 additions & 2 deletions statsd/example/statsd_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ import (
// To view statsd emitted metrics locally you can use
// netcat with "nc 8125 -l -u"
func main() {
statter, err := statsd.NewBufferedClient("127.0.0.1:8125",
"stats", 100*time.Millisecond, 1440)
statter, err := statsd.NewClientWithConfig(&statsd.ClientConfig{
Address: "127.0.0.1:8125",
Prefix: "stats",
UseBuffered: true,
FlushInterval: 100 * time.Millisecond,
FlushBytes: 1440,
})
if err != nil {
log.Fatalf("could not create statsd client: %v", err)
}
Expand Down
13 changes: 11 additions & 2 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ module github.com/uber-go/tally/tools

go 1.18

require golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
require (
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
honnef.co/go/tools v0.3.2
)

require golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 // indirect
require (
github.com/BurntSushi/toml v1.1.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20220613132600-b0d781184e0d // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
golang.org/x/tools v0.1.11 // indirect
)
13 changes: 12 additions & 1 deletion tools/go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp/typeparams v0.0.0-20220613132600-b0d781184e0d h1:+W8Qf4iJtMGKkyAygcKohjxTk4JPsL9DpzApJ22m5Ic=
golang.org/x/exp/typeparams v0.0.0-20220613132600-b0d781184e0d/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c h1:aFV+BgZ4svzjfabn8ERpuB4JI4N6/rdy1iusx77G3oU=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 h1:EBZoQjiKKPaLbPrbpssUfuHtwM6KV/vb4U85g/cigFY=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY=
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
honnef.co/go/tools v0.3.2 h1:ytYb4rOqyp1TSa2EPvNVwtPQJctSELKaMyLfqNP4+34=
honnef.co/go/tools v0.3.2/go.mod h1:jzwdWgg7Jdq75wlfblQxO4neNaFFSvgc1tD5Wv8U0Yw=
1 change: 1 addition & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ package tools

import (
_ "golang.org/x/lint/golint"
_ "honnef.co/go/tools/cmd/staticcheck"
)

0 comments on commit 86b6b09

Please sign in to comment.