Skip to content

Commit

Permalink
[Ajat|Baskara] log error only when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
ajatprabha committed Jul 1, 2019
1 parent eebb397 commit d85c5a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pkg/metrics/statsd_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ func Update(updateOption UpdateOption) {
case Guage:
err = instance.client.Gauge(formatter(updateOption.Name), int64(updateOption.NumValue), instance.sampleRate)
}
logger.Error(err)
if err != nil {
logger.Error(err)
}
}
9 changes: 7 additions & 2 deletions pkg/metrics/statsd_collector_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package metrics

import (
"errors"
"github.com/cactus/go-statsd-client/statsd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -42,11 +43,12 @@ func TestUpdate(t *testing.T) {
mock.AnythingOfType("float32")).Return(nil)
Update(UpdateOption{Type: Duration, Duration: time.Since(now)})

// error case
mc.On("Gauge",
mock.AnythingOfType("string"),
mock.AnythingOfType("int64"),
mock.AnythingOfType("float32")).Return(nil)
Update(UpdateOption{Type: Guage, NumValue: 500})
mock.AnythingOfType("float32")).Return(errors.New("error"))
Update(UpdateOption{Type: Guage, NumValue: -500})

mc.AssertExpectations(t)
}
Expand All @@ -65,6 +67,9 @@ func (msc *mockStatsdClient) Dec(string, int64, float32) error {

func (msc *mockStatsdClient) Gauge(str string, i int64, sr float32) error {
args := msc.Called(str, i, sr)
if args.Get(0) != nil {
return args.Get(0).(error)
}
return args.Error(0)
}

Expand Down

0 comments on commit d85c5a7

Please sign in to comment.