Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metrics: fix issues reported by staticcheck #20365

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions metrics/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,39 @@ func TestCounterClear(t *testing.T) {
c := NewCounter()
c.Inc(1)
c.Clear()
if count := c.Count(); 0 != count {
if count := c.Count(); count != 0 {
t.Errorf("c.Count(): 0 != %v\n", count)
}
}

func TestCounterDec1(t *testing.T) {
c := NewCounter()
c.Dec(1)
if count := c.Count(); -1 != count {
if count := c.Count(); count != -1 {
t.Errorf("c.Count(): -1 != %v\n", count)
}
}

func TestCounterDec2(t *testing.T) {
c := NewCounter()
c.Dec(2)
if count := c.Count(); -2 != count {
if count := c.Count(); count != -2 {
t.Errorf("c.Count(): -2 != %v\n", count)
}
}

func TestCounterInc1(t *testing.T) {
c := NewCounter()
c.Inc(1)
if count := c.Count(); 1 != count {
if count := c.Count(); count != 1 {
t.Errorf("c.Count(): 1 != %v\n", count)
}
}

func TestCounterInc2(t *testing.T) {
c := NewCounter()
c.Inc(2)
if count := c.Count(); 2 != count {
if count := c.Count(); count != 2 {
t.Errorf("c.Count(): 2 != %v\n", count)
}
}
Expand All @@ -56,22 +56,22 @@ func TestCounterSnapshot(t *testing.T) {
c.Inc(1)
snapshot := c.Snapshot()
c.Inc(1)
if count := snapshot.Count(); 1 != count {
if count := snapshot.Count(); count != 1 {
t.Errorf("c.Count(): 1 != %v\n", count)
}
}

func TestCounterZero(t *testing.T) {
c := NewCounter()
if count := c.Count(); 0 != count {
if count := c.Count(); count != 0 {
t.Errorf("c.Count(): 0 != %v\n", count)
}
}

func TestGetOrRegisterCounter(t *testing.T) {
r := NewRegistry()
NewRegisteredCounter("foo", r).Inc(47)
if c := GetOrRegisterCounter("foo", r); 47 != c.Count() {
if c := GetOrRegisterCounter("foo", r); c.Count() != 47 {
t.Fatal(c)
}
}
2 changes: 1 addition & 1 deletion metrics/gauge_float64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestFunctionalGaugeFloat64(t *testing.T) {
func TestGetOrRegisterFunctionalGaugeFloat64(t *testing.T) {
r := NewRegistry()
NewRegisteredFunctionalGaugeFloat64("foo", r, func() float64 { return 47 })
if g := GetOrRegisterGaugeFloat64("foo", r); 47 != g.Value() {
if g := GetOrRegisterGaugeFloat64("foo", r); g.Value() != 47 {
t.Fatal(g)
}
}
8 changes: 4 additions & 4 deletions metrics/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func BenchmarkGuage(b *testing.B) {
func TestGauge(t *testing.T) {
g := NewGauge()
g.Update(int64(47))
if v := g.Value(); 47 != v {
if v := g.Value(); v != 47 {
t.Errorf("g.Value(): 47 != %v\n", v)
}
}
Expand All @@ -26,15 +26,15 @@ func TestGaugeSnapshot(t *testing.T) {
g.Update(int64(47))
snapshot := g.Snapshot()
g.Update(int64(0))
if v := snapshot.Value(); 47 != v {
if v := snapshot.Value(); v != 47 {
t.Errorf("g.Value(): 47 != %v\n", v)
}
}

func TestGetOrRegisterGauge(t *testing.T) {
r := NewRegistry()
NewRegisteredGauge("foo", r).Update(47)
if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
if g := GetOrRegisterGauge("foo", r); g.Value() != 47 {
t.Fatal(g)
}
}
Expand All @@ -55,7 +55,7 @@ func TestFunctionalGauge(t *testing.T) {
func TestGetOrRegisterFunctionalGauge(t *testing.T) {
r := NewRegistry()
NewRegisteredFunctionalGauge("foo", r, func() int64 { return 47 })
if g := GetOrRegisterGauge("foo", r); 47 != g.Value() {
if g := GetOrRegisterGauge("foo", r); g.Value() != 47 {
t.Fatal(g)
}
}
Expand Down
34 changes: 17 additions & 17 deletions metrics/histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGetOrRegisterHistogram(t *testing.T) {
r := NewRegistry()
s := NewUniformSample(100)
NewRegisteredHistogram("foo", r, s).Update(47)
if h := GetOrRegisterHistogram("foo", r, s); 1 != h.Count() {
if h := GetOrRegisterHistogram("foo", r, s); h.Count() != 1 {
t.Fatal(h)
}
}
Expand All @@ -29,29 +29,29 @@ func TestHistogram10000(t *testing.T) {

func TestHistogramEmpty(t *testing.T) {
h := NewHistogram(NewUniformSample(100))
if count := h.Count(); 0 != count {
if count := h.Count(); count != 0 {
t.Errorf("h.Count(): 0 != %v\n", count)
}
if min := h.Min(); 0 != min {
if min := h.Min(); min != 0 {
t.Errorf("h.Min(): 0 != %v\n", min)
}
if max := h.Max(); 0 != max {
if max := h.Max(); max != 0 {
t.Errorf("h.Max(): 0 != %v\n", max)
}
if mean := h.Mean(); 0.0 != mean {
if mean := h.Mean(); mean != 0.0 {
t.Errorf("h.Mean(): 0.0 != %v\n", mean)
}
if stdDev := h.StdDev(); 0.0 != stdDev {
if stdDev := h.StdDev(); stdDev != 0.0 {
t.Errorf("h.StdDev(): 0.0 != %v\n", stdDev)
}
ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
if 0.0 != ps[0] {
if ps[0] != 0.0 {
t.Errorf("median: 0.0 != %v\n", ps[0])
}
if 0.0 != ps[1] {
if ps[1] != 0.0 {
t.Errorf("75th percentile: 0.0 != %v\n", ps[1])
}
if 0.0 != ps[2] {
if ps[2] != 0.0 {
t.Errorf("99th percentile: 0.0 != %v\n", ps[2])
}
}
Expand All @@ -67,29 +67,29 @@ func TestHistogramSnapshot(t *testing.T) {
}

func testHistogram10000(t *testing.T, h Histogram) {
if count := h.Count(); 10000 != count {
if count := h.Count(); count != 10000 {
t.Errorf("h.Count(): 10000 != %v\n", count)
}
if min := h.Min(); 1 != min {
if min := h.Min(); min != 1 {
t.Errorf("h.Min(): 1 != %v\n", min)
}
if max := h.Max(); 10000 != max {
if max := h.Max(); max != 10000 {
t.Errorf("h.Max(): 10000 != %v\n", max)
}
if mean := h.Mean(); 5000.5 != mean {
if mean := h.Mean(); mean != 5000.5 {
t.Errorf("h.Mean(): 5000.5 != %v\n", mean)
}
if stdDev := h.StdDev(); 2886.751331514372 != stdDev {
if stdDev := h.StdDev(); stdDev != 2886.751331514372 {
t.Errorf("h.StdDev(): 2886.751331514372 != %v\n", stdDev)
}
ps := h.Percentiles([]float64{0.5, 0.75, 0.99})
if 5000.5 != ps[0] {
if ps[0] != 5000.5 {
t.Errorf("median: 5000.5 != %v\n", ps[0])
}
if 7500.75 != ps[1] {
if ps[1] != 7500.75 {
t.Errorf("75th percentile: 7500.75 != %v\n", ps[1])
}
if 9900.99 != ps[2] {
if ps[2] != 9900.99 {
t.Errorf("99th percentile: 9900.99 != %v\n", ps[2])
}
}
6 changes: 3 additions & 3 deletions metrics/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func InfluxDBWithTags(r metrics.Registry, d time.Duration, url, database, userna
func InfluxDBWithTagsOnce(r metrics.Registry, url, database, username, password, namespace string, tags map[string]string) error {
u, err := uurl.Parse(url)
if err != nil {
return fmt.Errorf("Unable to parse InfluxDB. url: %s, err: %v", url, err)
return fmt.Errorf("unable to parse InfluxDB. url: %s, err: %v", url, err)
}

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

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

return nil
Expand Down
2 changes: 1 addition & 1 deletion metrics/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestRegistryMarshallJSON(t *testing.T) {
r := NewRegistry()
r.Register("counter", NewCounter())
enc.Encode(r)
if s := b.String(); "{\"counter\":{\"count\":0}}\n" != s {
if s := b.String(); s != "{\"counter\":{\"count\":0}}\n" {
t.Fatalf(s)
}
}
Expand Down
2 changes: 1 addition & 1 deletion metrics/librato/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *LibratoClient) PostMetrics(batch Batch) (err error) {
if body, err = ioutil.ReadAll(resp.Body); err != nil {
body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err))
}
err = fmt.Errorf("Unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body))
err = fmt.Errorf("unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body))
}
return
}
5 changes: 3 additions & 2 deletions metrics/librato/librato.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ func Librato(r metrics.Registry, d time.Duration, e string, t string, s string,

func (rep *Reporter) Run() {
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")
ticker := time.Tick(rep.Interval)
ticker := time.NewTicker(rep.Interval)
defer ticker.Stop()
metricsApi := &LibratoClient{rep.Email, rep.Token}
for now := range ticker {
for now := range ticker.C {
var metrics Batch
var err error
if metrics, err = rep.BuildRequest(now, rep.Registry); err != nil {
Expand Down
10 changes: 5 additions & 5 deletions metrics/meter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func BenchmarkMeter(b *testing.B) {
func TestGetOrRegisterMeter(t *testing.T) {
r := NewRegistry()
NewRegisteredMeter("foo", r).Mark(47)
if m := GetOrRegisterMeter("foo", r); 47 != m.Count() {
if m := GetOrRegisterMeter("foo", r); m.Count() != 47 {
t.Fatal(m)
}
}
Expand All @@ -40,19 +40,19 @@ func TestMeterDecay(t *testing.T) {
func TestMeterNonzero(t *testing.T) {
m := NewMeter()
m.Mark(3)
if count := m.Count(); 3 != count {
if count := m.Count(); count != 3 {
t.Errorf("m.Count(): 3 != %v\n", count)
}
}

func TestMeterStop(t *testing.T) {
l := len(arbiter.meters)
m := NewMeter()
if len(arbiter.meters) != l+1 {
if l+1 != len(arbiter.meters) {
t.Errorf("arbiter.meters: %d != %d\n", l+1, len(arbiter.meters))
}
m.Stop()
if len(arbiter.meters) != l {
if l != len(arbiter.meters) {
t.Errorf("arbiter.meters: %d != %d\n", l, len(arbiter.meters))
}
}
Expand All @@ -67,7 +67,7 @@ func TestMeterSnapshot(t *testing.T) {

func TestMeterZero(t *testing.T) {
m := NewMeter()
if count := m.Count(); 0 != count {
if count := m.Count(); count != 0 {
t.Errorf("m.Count(): 0 != %v\n", count)
}
}
22 changes: 14 additions & 8 deletions metrics/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ func TestRegistry(t *testing.T) {
i := 0
r.Each(func(name string, iface interface{}) {
i++
if "foo" != name {
if name != "foo" {
t.Fatal(name)
}
if _, ok := iface.(Counter); !ok {
t.Fatal(iface)
}
})
if 1 != i {
if i != 1 {
t.Fatal(i)
}
r.Unregister("foo")
i = 0
r.Each(func(string, interface{}) { i++ })
if 0 != i {
if i != 0 {
t.Fatal(i)
}
}
Expand All @@ -52,19 +52,19 @@ func TestRegistryDuplicate(t *testing.T) {
t.Fatal(iface)
}
})
if 1 != i {
if i != 1 {
t.Fatal(i)
}
}

func TestRegistryGet(t *testing.T) {
r := NewRegistry()
r.Register("foo", NewCounter())
if count := r.Get("foo").(Counter).Count(); 0 != count {
if count := r.Get("foo").(Counter).Count(); count != 0 {
t.Fatal(count)
}
r.Get("foo").(Counter).Inc(1)
if count := r.Get("foo").(Counter).Count(); 1 != count {
if count := r.Get("foo").(Counter).Count(); count != 1 {
t.Fatal(count)
}
}
Expand Down Expand Up @@ -271,14 +271,17 @@ func TestChildPrefixedRegistryOfChildRegister(t *testing.T) {
t.Fatal(err.Error())
}
err = r2.Register("baz", NewCounter())
if err != nil {
t.Fatal(err.Error())
}
c := NewCounter()
Register("bars", c)

i := 0
r2.Each(func(name string, m interface{}) {
i++
if name != "prefix.prefix2.baz" {
//t.Fatal(name)
t.Fatal(name)
}
})
if i != 1 {
Expand All @@ -294,11 +297,14 @@ func TestWalkRegistries(t *testing.T) {
t.Fatal(err.Error())
}
err = r2.Register("baz", NewCounter())
if err != nil {
t.Fatal(err.Error())
}
c := NewCounter()
Register("bars", c)

_, prefix := findPrefix(r2, "")
if "prefix.prefix2." != prefix {
if prefix != "prefix.prefix2." {
t.Fatal(prefix)
}

Expand Down
Loading