diff --git a/metrics/cloudwatch/cloudwatch.go b/metrics/cloudwatch/cloudwatch.go index b14cdcd18..43121f52d 100644 --- a/metrics/cloudwatch/cloudwatch.go +++ b/metrics/cloudwatch/cloudwatch.go @@ -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) diff --git a/metrics/cloudwatch/cloudwatch_test.go b/metrics/cloudwatch/cloudwatch_test.go index d534b7ec5..e825635e7 100644 --- a/metrics/cloudwatch/cloudwatch_test.go +++ b/metrics/cloudwatch/cloudwatch_test.go @@ -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 } diff --git a/metrics/teststat/teststat.go b/metrics/teststat/teststat.go index be1d48980..7918b9feb 100644 --- a/metrics/teststat/teststat.go +++ b/metrics/teststat/teststat.go @@ -7,6 +7,7 @@ import ( "math" "math/rand" "reflect" + "sort" "strings" "github.com/go-kit/kit/metrics" @@ -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