Skip to content

Commit 5e32511

Browse files
committed
Don't send logs if there are no metrics
1 parent 01ce024 commit 5e32511

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

emf/logger.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ func (l *Logger) Log() {
159159
metrics = append(metrics, l.defaultContext.metricDirective)
160160
}
161161
for _, v := range l.contexts {
162-
// TODO check if not empty as above?
163-
metrics = append(metrics, v.metricDirective)
162+
if len(v.metricDirective.Metrics) > 0 {
163+
metrics = append(metrics, v.metricDirective)
164+
}
164165
}
165166

166167
if len(metrics) == 0 {

emf/logger_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ package emf_test
22

33
import (
44
"bytes"
5+
"github.com/kinbiko/jsonassert"
6+
"github.com/prozz/aws-embedded-metrics-golang/emf"
7+
"github.com/tj/assert"
58
"io/ioutil"
69
"os"
710
"testing"
8-
9-
"github.com/kinbiko/jsonassert"
10-
"github.com/prozz/aws-embedded-metrics-golang/emf"
1111
)
1212

1313
func TestEmf(t *testing.T) {
@@ -171,11 +171,6 @@ func TestEmf(t *testing.T) {
171171
},
172172
expected: "testdata/16.json",
173173
},
174-
{
175-
name: "no metrics set",
176-
given: func(logger *emf.Logger) {},
177-
expected: "testdata/17.txt",
178-
},
179174
}
180175

181176
for _, tc := range tcs {
@@ -198,6 +193,23 @@ func TestEmf(t *testing.T) {
198193
jsonassert.New(t).Assertf(buf.String(), string(f))
199194
})
200195
}
196+
197+
t.Run("no metrics set", func(t *testing.T) {
198+
var buf bytes.Buffer
199+
logger := emf.NewFor(&buf)
200+
logger.Log()
201+
202+
assert.Empty(t, buf.String())
203+
})
204+
205+
t.Run("new context, no metrics set", func(t *testing.T) {
206+
var buf bytes.Buffer
207+
logger := emf.NewFor(&buf)
208+
logger.NewContext().Namespace("galaxy")
209+
logger.Log()
210+
211+
assert.Empty(t, buf.String())
212+
})
201213
}
202214

203215
func TestLogger_LogSampled(t *testing.T) {

emf/testdata/17.txt

Whitespace-only changes.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ go 1.14
44

55
require (
66
github.com/kinbiko/jsonassert v1.0.1
7+
github.com/tj/assert v0.0.3
78
)

go.sum

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
git.naspersclassifieds.com/Pawel.Rozynek/aws-embedded-metrics-golang v0.0.0-20200605204858-b8ba9d02bc89 h1:CBLZZgcGL8VUl9+N8t71yZgMF16Qwk4i5l7Lw1LafQY=
2-
git.naspersclassifieds.com/Pawel.Rozynek/aws-embedded-metrics-golang v0.0.0-20200605204858-b8ba9d02bc89/go.mod h1:GLqH9LFDevA5Hss5EuOU+N77htYcG4/d3X2JMMo9yUY=
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34
github.com/kinbiko/jsonassert v1.0.1 h1:8gdLmUaPWuxk2TzQSofKRqatFH6zwTF6AsUH4bugJYY=
45
github.com/kinbiko/jsonassert v1.0.1/go.mod h1:QRwBwiAsrcJpjw+L+Q4WS8psLxuUY+HylVZS/4j74TM=
6+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
10+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
github.com/tj/assert v0.0.3 h1:Df/BlaZ20mq6kuai7f5z2TvPFiwC3xaWJSDQNiIS3Rk=
12+
github.com/tj/assert v0.0.3/go.mod h1:Ne6X72Q+TB1AteidzQncjw9PabbMp4PBMZ1k+vd1Pvk=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16+
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c h1:grhR+C34yXImVGp7EzNk+DTIk+323eIUWOmEevy6bDo=
17+
gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)