Skip to content

Commit 58f2ce8

Browse files
gballetfjl
authored andcommitted
metrics: fix issues reported by staticcheck (#20365)
1 parent dd21f07 commit 58f2ce8

15 files changed

Lines changed: 109 additions & 105 deletions

‎metrics/counter_test.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,39 @@ func TestCounterClear(t *testing.T) {
1414
c := NewCounter()
1515
c.Inc(1)
1616
c.Clear()
17-
if count := c.Count(); 0 != count {
17+
if count := c.Count(); count != 0 {
1818
t.Errorf("c.Count(): 0 != %v\n", count)
1919
}
2020
}
2121

2222
func TestCounterDec1(t *testing.T) {
2323
c := NewCounter()
2424
c.Dec(1)
25-
if count := c.Count(); -1 != count {
25+
if count := c.Count(); count != -1 {
2626
t.Errorf("c.Count(): -1 != %v\n", count)
2727
}
2828
}
2929

3030
func TestCounterDec2(t *testing.T) {
3131
c := NewCounter()
3232
c.Dec(2)
33-
if count := c.Count(); -2 != count {
33+
if count := c.Count(); count != -2 {
3434
t.Errorf("c.Count(): -2 != %v\n", count)
3535
}
3636
}
3737

3838
func TestCounterInc1(t *testing.T) {
3939
c := NewCounter()
4040
c.Inc(1)
41-
if count := c.Count(); 1 != count {
41+
if count := c.Count(); count != 1 {
4242
t.Errorf("c.Count(): 1 != %v\n", count)
4343
}
4444
}
4545

4646
func TestCounterInc2(t *testing.T) {
4747
c := NewCounter()
4848
c.Inc(2)
49-
if count := c.Count(); 2 != count {
49+
if count := c.Count(); count != 2 {
5050
t.Errorf("c.Count(): 2 != %v\n", count)
5151
}
5252
}
@@ -56,22 +56,22 @@ func TestCounterSnapshot(t *testing.T) {
5656
c.Inc(1)
5757
snapshot := c.Snapshot()
5858
c.Inc(1)
59-
if count := snapshot.Count(); 1 != count {
59+
if count := snapshot.Count(); count != 1 {
6060
t.Errorf("c.Count(): 1 != %v\n", count)
6161
}
6262
}
6363

6464
func TestCounterZero(t *testing.T) {
6565
c := NewCounter()
66-
if count := c.Count(); 0 != count {
66+
if count := c.Count(); count != 0 {
6767
t.Errorf("c.Count(): 0 != %v\n", count)
6868
}
6969
}
7070

7171
func TestGetOrRegisterCounter(t *testing.T) {
7272
r := NewRegistry()
7373
NewRegisteredCounter("foo", r).Inc(47)
74-
if c := GetOrRegisterCounter("foo", r); 47 != c.Count() {
74+
if c := GetOrRegisterCounter("foo", r); c.Count() != 47 {
7575
t.Fatal(c)
7676
}
7777
}

‎metrics/gauge_float64_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestFunctionalGaugeFloat64(t *testing.T) {
5353
func TestGetOrRegisterFunctionalGaugeFloat64(t *testing.T) {
5454
r := NewRegistry()
5555
NewRegisteredFunctionalGaugeFloat64("foo", r, func() float64 { return 47 })
56-
if g := GetOrRegisterGaugeFloat64("foo", r); 47 != g.Value() {
56+
if g := GetOrRegisterGaugeFloat64("foo", r); g.Value() != 47 {
5757
t.Fatal(g)
5858
}
5959
}

‎metrics/gauge_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func BenchmarkGuage(b *testing.B) {
1616
func TestGauge(t *testing.T) {
1717
g := NewGauge()
1818
g.Update(int64(47))
19-
if v := g.Value(); 47 != v {
19+
if v := g.Value(); v != 47 {
2020
t.Errorf("g.Value(): 47 != %v\n", v)
2121
}
2222
}
@@ -26,15 +26,15 @@ func TestGaugeSnapshot(t *testing.T) {
2626
g.Update(int64(47))
2727
snapshot := g.Snapshot()
2828
g.Update(int64(0))
29-
if v := snapshot.Value(); 47 != v {
29+
if v := snapshot.Value(); v != 47 {
3030
t.Errorf("g.Value(): 47 != %v\n", v)
3131
}
3232
}
3333

3434
func TestGetOrRegisterGauge(t *testing.T) {
3535
r := NewRegistry()
3636
NewRegisteredGauge("foo", r).Update(47)
37-
if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
37+
if g := GetOrRegisterGauge("foo", r); g.Value() != 47 {
3838
t.Fatal(g)
3939
}
4040
}
@@ -55,7 +55,7 @@ func TestFunctionalGauge(t *testing.T) {
5555
func TestGetOrRegisterFunctionalGauge(t *testing.T) {
5656
r := NewRegistry()
5757
NewRegisteredFunctionalGauge("foo", r, func() int64 { return 47 })
58-
if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
58+
if g := GetOrRegisterGauge("foo", r); g.Value() != 47 {
5959
t.Fatal(g)
6060
}
6161
}

‎metrics/histogram_test.go‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestGetOrRegisterHistogram(t *testing.T) {
1414
r := NewRegistry()
1515
s := NewUniformSample(100)
1616
NewRegisteredHistogram("foo", r, s).Update(47)
17-
if h := GetOrRegisterHistogram("foo", r, s); 1 != h.Count() {
17+
if h := GetOrRegisterHistogram("foo", r, s); h.Count() != 1 {
1818
t.Fatal(h)
1919
}
2020
}
@@ -29,29 +29,29 @@ func TestHistogram10000(t *testing.T) {
2929

3030
func TestHistogramEmpty(t *testing.T) {
3131
h := NewHistogram(NewUniformSample(100))
32-
if count := h.Count(); 0 != count {
32+
if count := h.Count(); count != 0 {
3333
t.Errorf("h.Count(): 0 != %v\n", count)
3434
}
35-
if min := h.Min(); 0 != min {
35+
if min := h.Min(); min != 0 {
3636
t.Errorf("h.Min(): 0 != %v\n", min)
3737
}
38-
if max := h.Max(); 0 != max {
38+
if max := h.Max(); max != 0 {
3939
t.Errorf("h.Max(): 0 != %v\n", max)
4040
}
41-
if mean := h.Mean(); 0.0 != mean {
41+
if mean := h.Mean(); mean != 0.0 {
4242
t.Errorf("h.Mean(): 0.0 != %v\n", mean)
4343
}
44-
if stdDev := h.StdDev(); 0.0 != stdDev {
44+
if stdDev := h.StdDev(); stdDev != 0.0 {
4545
t.Errorf("h.StdDev(): 0.0 != %v\n", stdDev)
4646
}
4747
ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
48-
if 0.0 != ps[0] {
48+
if ps[0] != 0.0 {
4949
t.Errorf("median: 0.0 != %v\n", ps[0])
5050
}
51-
if 0.0 != ps[1] {
51+
if ps[1] != 0.0 {
5252
t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
5353
}
54-
if 0.0 != ps[2] {
54+
if ps[2] != 0.0 {
5555
t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
5656
}
5757
}
@@ -67,29 +67,29 @@ func TestHistogramSnapshot(t *testing.T) {
6767
}
6868

6969
func testHistogram10000(t *testing.T, h Histogram) {
70-
if count := h.Count(); 10000 != count {
70+
if count := h.Count(); count != 10000 {
7171
t.Errorf("h.Count(): 10000 != %v\n", count)
7272
}
73-
if min := h.Min(); 1 != min {
73+
if min := h.Min(); min != 1 {
7474
t.Errorf("h.Min(): 1 != %v\n", min)
7575
}
76-
if max := h.Max(); 10000 != max {
76+
if max := h.Max(); max != 10000 {
7777
t.Errorf("h.Max(): 10000 != %v\n", max)
7878
}
79-
if mean := h.Mean(); 5000.5 != mean {
79+
if mean := h.Mean(); mean != 5000.5 {
8080
t.Errorf("h.Mean(): 5000.5 != %v\n", mean)
8181
}
82-
if stdDev := h.StdDev(); 2886.751331514372 != stdDev {
82+
if stdDev := h.StdDev(); stdDev != 2886.751331514372 {
8383
t.Errorf("h.StdDev(): 2886.751331514372 != %v\n", stdDev)
8484
}
8585
ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
86-
if 5000.5 != ps[0] {
86+
if ps[0] != 5000.5 {
8787
t.Errorf("median: 5000.5 != %v\n", ps[0])
8888
}
89-
if 7500.75 != ps[1] {
89+
if ps[1] != 7500.75 {
9090
t.Errorf("75th percentile: 7500.75 != %v\n", ps[1])
9191
}
92-
if 9900.99 != ps[2] {
92+
if ps[2] != 9900.99 {
9393
t.Errorf("99th percentile: 9900.99 != %v\n", ps[2])
9494
}
9595
}

‎metrics/influxdb/influxdb.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func InfluxDBWithTags(r metrics.Registry, d time.Duration, url, database, userna
6262
func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password, namespace string, tags map[string]string) error {
6363
u, err := uurl.Parse(url)
6464
if err != nil {
65-
return fmt.Errorf("Unable to parse InfluxDB. url: %s, err: %v", url, err)
65+
return fmt.Errorf("unable to parse InfluxDB. url: %s, err: %v", url, err)
6666
}
6767

6868
rep := &reporter{
@@ -76,11 +76,11 @@ func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password,
7676
cache: make(map[string]int64),
7777
}
7878
if err := rep.makeClient(); err != nil {
79-
return fmt.Errorf("Unable to make InfluxDB client. err: %v", err)
79+
return fmt.Errorf("unable to make InfluxDB client. err: %v", err)
8080
}
8181

8282
if err := rep.send(); err != nil {
83-
return fmt.Errorf("Unable to send to InfluxDB. err: %v", err)
83+
return fmt.Errorf("unable to send to InfluxDB. err: %v", err)
8484
}
8585

8686
return nil

‎metrics/json_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestRegistryMarshallJSON(t *testing.T) {
1212
r := NewRegistry()
1313
r.Register("counter", NewCounter())
1414
enc.Encode(r)
15-
if s := b.String(); "{\"counter\":{\"count\":0}}\n" != s {
15+
if s := b.String(); s != "{\"counter\":{\"count\":0}}\n" {
1616
t.Fatalf(s)
1717
}
1818
}

‎metrics/librato/client.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (c *LibratoClient) PostMetrics(batch Batch) (err error) {
9696
if body, err = ioutil.ReadAll(resp.Body); err != nil {
9797
body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err))
9898
}
99-
err = fmt.Errorf("Unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body))
99+
err = fmt.Errorf("unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body))
100100
}
101101
return
102102
}

‎metrics/librato/librato.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ func Librato(r metrics.Registry, d time.Duration, e string, t string, s string,
4242

4343
func (rep *Reporter) Run() {
4444
log.Printf("WARNING: This client has been DEPRECATED! It has been moved to https://github.com/mihasya/go-metrics-librato and will be removed from rcrowley/go-metrics on August 5th 2015")
45-
ticker := time.Tick(rep.Interval)
45+
ticker := time.NewTicker(rep.Interval)
46+
defer ticker.Stop()
4647
metricsApi := &LibratoClient{rep.Email, rep.Token}
47-
for now := range ticker {
48+
for now := range ticker.C {
4849
var metrics Batch
4950
var err error
5051
if metrics, err = rep.BuildRequest(now, rep.Registry); err != nil {

‎metrics/meter_test.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func BenchmarkMeter(b *testing.B) {
1616
func TestGetOrRegisterMeter(t *testing.T) {
1717
r := NewRegistry()
1818
NewRegisteredMeter("foo", r).Mark(47)
19-
if m := GetOrRegisterMeter("foo", r); 47 != m.Count() {
19+
if m := GetOrRegisterMeter("foo", r); m.Count() != 47 {
2020
t.Fatal(m)
2121
}
2222
}
@@ -40,19 +40,19 @@ func TestMeterDecay(t *testing.T) {
4040
func TestMeterNonzero(t *testing.T) {
4141
m := NewMeter()
4242
m.Mark(3)
43-
if count := m.Count(); 3 != count {
43+
if count := m.Count(); count != 3 {
4444
t.Errorf("m.Count(): 3 != %v\n", count)
4545
}
4646
}
4747

4848
func TestMeterStop(t *testing.T) {
4949
l := len(arbiter.meters)
5050
m := NewMeter()
51-
if len(arbiter.meters) != l+1 {
51+
if l+1 != len(arbiter.meters) {
5252
t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
5353
}
5454
m.Stop()
55-
if len(arbiter.meters) != l {
55+
if l != len(arbiter.meters) {
5656
t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
5757
}
5858
}
@@ -67,7 +67,7 @@ func TestMeterSnapshot(t *testing.T) {
6767

6868
func TestMeterZero(t *testing.T) {
6969
m := NewMeter()
70-
if count := m.Count(); 0 != count {
70+
if count := m.Count(); count != 0 {
7171
t.Errorf("m.Count(): 0 != %v\n", count)
7272
}
7373
}

‎metrics/registry_test.go‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ func TestRegistry(t *testing.T) {
1919
i := 0
2020
r.Each(func(name string, iface interface{}) {
2121
i++
22-
if "foo" != name {
22+
if name != "foo" {
2323
t.Fatal(name)
2424
}
2525
if _, ok := iface.(Counter); !ok {
2626
t.Fatal(iface)
2727
}
2828
})
29-
if 1 != i {
29+
if i != 1 {
3030
t.Fatal(i)
3131
}
3232
r.Unregister("foo")
3333
i = 0
3434
r.Each(func(string, interface{}) { i++ })
35-
if 0 != i {
35+
if i != 0 {
3636
t.Fatal(i)
3737
}
3838
}
@@ -52,19 +52,19 @@ func TestRegistryDuplicate(t *testing.T) {
5252
t.Fatal(iface)
5353
}
5454
})
55-
if 1 != i {
55+
if i != 1 {
5656
t.Fatal(i)
5757
}
5858
}
5959

6060
func TestRegistryGet(t *testing.T) {
6161
r := NewRegistry()
6262
r.Register("foo", NewCounter())
63-
if count := r.Get("foo").(Counter).Count(); 0 != count {
63+
if count := r.Get("foo").(Counter).Count(); count != 0 {
6464
t.Fatal(count)
6565
}
6666
r.Get("foo").(Counter).Inc(1)
67-
if count := r.Get("foo").(Counter).Count(); 1 != count {
67+
if count := r.Get("foo").(Counter).Count(); count != 1 {
6868
t.Fatal(count)
6969
}
7070
}
@@ -271,14 +271,17 @@ func TestChildPrefixedRegistryOfChildRegister(t *testing.T) {
271271
t.Fatal(err.Error())
272272
}
273273
err = r2.Register("baz", NewCounter())
274+
if err != nil {
275+
t.Fatal(err.Error())
276+
}
274277
c := NewCounter()
275278
Register("bars", c)
276279

277280
i := 0
278281
r2.Each(func(name string, m interface{}) {
279282
i++
280283
if name != "prefix.prefix2.baz" {
281-
//t.Fatal(name)
284+
t.Fatal(name)
282285
}
283286
})
284287
if i != 1 {
@@ -294,11 +297,14 @@ func TestWalkRegistries(t *testing.T) {
294297
t.Fatal(err.Error())
295298
}
296299
err = r2.Register("baz", NewCounter())
300+
if err != nil {
301+
t.Fatal(err.Error())
302+
}
297303
c := NewCounter()
298304
Register("bars", c)
299305

300306
_, prefix := findPrefix(r2, "")
301-
if "prefix.prefix2." != prefix {
307+
if prefix != "prefix.prefix2." {
302308
t.Fatal(prefix)
303309
}
304310

0 commit comments

Comments
 (0)