Skip to content

Commit

Permalink
fix bad Gauge test (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
taraspos committed Apr 16, 2020
1 parent e10c4c8 commit 833a0dd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
8 changes: 4 additions & 4 deletions metrics/cloudwatch/cloudwatch.go
Expand Up @@ -175,16 +175,16 @@ func (cw *CloudWatch) Send() error {
})

cw.gauges.Reset().Walk(func(name string, lvs lv.LabelValues, values []float64) bool {
if len(values) == 0 {
return true
}

datum := &cloudwatch.MetricDatum{
MetricName: aws.String(name),
Dimensions: makeDimensions(lvs...),
Timestamp: aws.Time(now),
}

if len(values) == 0 {
return true
}

// CloudWatch Put Metrics API (https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html)
// expects batch of unique values including the array of corresponding counts
valuesCounter := make(map[float64]int)
Expand Down
2 changes: 1 addition & 1 deletion metrics/cloudwatch/cloudwatch_test.go
Expand Up @@ -164,9 +164,9 @@ func TestGauge(t *testing.T) {
t.Fatal(err)
}
svc.mtx.RLock()
defer svc.mtx.RUnlock()
res := svc.valuesReceived[name]
delete(svc.valuesReceived, name)
defer svc.mtx.RUnlock()
return res
}

Expand Down
20 changes: 17 additions & 3 deletions metrics/teststat/teststat.go
Expand Up @@ -7,6 +7,7 @@ import (
"math"
"math/rand"
"reflect"
"sort"
"strings"

"github.com/go-kit/kit/metrics"
Expand Down Expand Up @@ -53,11 +54,24 @@ func TestGauge(gauge metrics.Gauge, value func() []float64) error {
for i := 0; i < n; i++ {
f := float64(a[i])
gauge.Add(f)
want[len(want)-1] += f
want = append(want, want[len(want)-1]+f)
}

if have := value(); reflect.DeepEqual(want, have) {
return fmt.Errorf("want %f, have %f", want, have)
have := value()

switch len(have) {
case 0:
return fmt.Errorf("got 0 values")
case 1: // provider doesn't support multi value
if have[0] != want[len(want)-1] {
return fmt.Errorf("want %f, have %f", want, have)
}
default: // provider support multi value gauges
sort.Float64s(want)
sort.Float64s(have)
if !reflect.DeepEqual(want, have) {
return fmt.Errorf("want %f, have %f", want, have)
}
}

return nil
Expand Down

0 comments on commit 833a0dd

Please sign in to comment.