Repository: go-kit/kit
Pull request #961 (2020-02-21 15:31:41)

Log CloudWatch response error
Condition to log CloudWatch Response Error is broken, as a result, the error is never logged.
This PR fixes the condition


Patch:
diff --git a/README.md b/README.md
index 7ca325f..ff7b335 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,4 @@
-# Go kit<br/>[![Circle CI](https://circleci.com/gh/go-kit/kit.svg?style=shield)](https://circleci.com/gh/go-kit/kit) [![Travis CI](https://travis-ci.org/go-kit/kit.svg?branch=master)](https://travis-ci.org/go-kit/kit) [![builds.sr.ht status](https://builds.sr.ht/~peterbourgon/kit.svg)](https://builds.sr.ht/~peterbourgon/kit?) [![GoDoc](https://godoc.org/github.com/go-kit/kit?status.svg)](https://godoc.org/github.com/go-kit/kit) [![Coverage Status](https://coveralls.io/repos/go-kit/kit/badge.svg?branch=master&service=github)](https://coveralls.io/github/go-kit/kit?branch=master) [![Go Report Card](https://goreportcard.com/badge/go-kit/kit)](https://goreportcard.com/report/go-kit/kit) [![Sourcegraph](https://sourcegraph.com/github.com/go-kit/kit/-/badge.svg)](https://sourcegraph.com/github.com/go-kit/kit?badge)
+# Go kit<br/>[![Circle CI](https://circleci.com/gh/go-kit/kit.svg?style=shield)](https://circleci.com/gh/go-kit/kit) [![Travis CI](https://travis-ci.org/go-kit/kit.svg?branch=master)](https://travis-ci.org/go-kit/kit) [![builds.sr.ht status](https://builds.sr.ht/~peterbourgon/kit.svg)](https://builds.sr.ht/~peterbourgon/kit?) [![GoDev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/go-kit/kit?tab=doc) [![Coverage Status](https://coveralls.io/repos/go-kit/kit/badge.svg?branch=master&service=github)](https://coveralls.io/github/go-kit/kit?branch=master) [![Go Report Card](https://goreportcard.com/badge/go-kit/kit)](https://goreportcard.com/report/go-kit/kit) [![Sourcegraph](https://sourcegraph.com/github.com/go-kit/kit/-/badge.svg)](https://sourcegraph.com/github.com/go-kit/kit?badge)
 
 **Go kit** is a **programming toolkit** for building microservices
 (or elegant monoliths) in Go. We solve common problems in distributed
diff --git a/metrics/cloudwatch/cloudwatch.go b/metrics/cloudwatch/cloudwatch.go
index 0aad8a3..b14cdcd 100644
--- a/metrics/cloudwatch/cloudwatch.go
+++ b/metrics/cloudwatch/cloudwatch.go
@@ -255,7 +255,7 @@ func (cw *CloudWatch) Send() error {
 	}
 	var firstErr error
 	for i := 0; i < cap(errors); i++ {
-		if err := <-errors; err != nil && firstErr != nil {
+		if err := <-errors; err != nil && firstErr == nil {
 			firstErr = err
 		}
 	}


Test patch:
diff --git a/metrics/cloudwatch/cloudwatch_test.go b/metrics/cloudwatch/cloudwatch_test.go
index 7496525..d534b7e 100644
--- a/metrics/cloudwatch/cloudwatch_test.go
+++ b/metrics/cloudwatch/cloudwatch_test.go
@@ -15,6 +15,10 @@ import (
 	"github.com/go-kit/kit/metrics/teststat"
 )
 
+const metricNameToGenerateError = "metric_name_used_to_throw_an_error"
+
+var errTest = errors.New("test error")
+
 type mockCloudWatch struct {
 	cloudwatchiface.CloudWatchAPI
 	mtx                sync.RWMutex
@@ -33,6 +37,10 @@ func (mcw *mockCloudWatch) PutMetricData(input *cloudwatch.PutMetricDataInput) (
 	mcw.mtx.Lock()
 	defer mcw.mtx.Unlock()
 	for _, datum := range input.MetricData {
+		if *datum.MetricName == metricNameToGenerateError {
+			return nil, errTest
+		}
+
 		if len(datum.Values) > 0 {
 			for _, v := range datum.Values {
 				mcw.valuesReceived[*datum.MetricName] = append(mcw.valuesReceived[*datum.MetricName], *v)
@@ -125,8 +133,7 @@ func TestCounterLowSendConcurrency(t *testing.T) {
 		wants = append(wants, teststat.FillCounter(counters[name]))
 	}
 
-	err := cw.Send()
-	if err != nil {
+	if err := cw.Send(); err != nil {
 		t.Fatal(err)
 	}
 
@@ -280,3 +287,13 @@ func TestHistogram(t *testing.T) {
 		t.Fatal(err)
 	}
 }
+
+func TestErrorLog(t *testing.T) {
+	namespace := "abc"
+	svc := newMockCloudWatch()
+	cw := New(namespace, svc, WithLogger(log.NewNopLogger()))
+	cw.NewGauge(metricNameToGenerateError).Set(123)
+	if err := cw.Send(); err != errTest {
+		t.Fatal("Expected error, but didn't get one")
+	}
+}