File tree Expand file tree Collapse file tree 3 files changed +31
-7
lines changed Expand file tree Collapse file tree 3 files changed +31
-7
lines changed Original file line number Diff line number Diff line change @@ -158,9 +158,15 @@ func (l *Logger) Log() {
158158 metrics = append (metrics , l .defaultContext .metricDirective )
159159 }
160160 for _ , v := range l .contexts {
161- // TODO check if not empty as above?
162- metrics = append (metrics , v .metricDirective )
161+ if len (v .metricDirective .Metrics ) > 0 {
162+ metrics = append (metrics , v .metricDirective )
163+ }
163164 }
165+
166+ if len (metrics ) == 0 {
167+ return
168+ }
169+
164170 l .values ["_aws" ] = Metadata {
165171 Timestamp : l .timestamp ,
166172 Metrics : metrics ,
Original file line number Diff line number Diff line change @@ -2,12 +2,11 @@ package emf_test
22
33import (
44 "bytes"
5+ "github.com/kinbiko/jsonassert"
6+ "github.com/prozz/aws-embedded-metrics-golang/emf"
57 "io/ioutil"
68 "os"
79 "testing"
8-
9- "github.com/kinbiko/jsonassert"
10- "github.com/prozz/aws-embedded-metrics-golang/emf"
1110)
1211
1312func TestEmf (t * testing.T ) {
@@ -193,6 +192,27 @@ func TestEmf(t *testing.T) {
193192 jsonassert .New (t ).Assertf (buf .String (), string (f ))
194193 })
195194 }
195+
196+ t .Run ("no metrics set" , func (t * testing.T ) {
197+ var buf bytes.Buffer
198+ logger := emf .NewFor (& buf )
199+ logger .Log ()
200+
201+ if buf .String () != "" {
202+ t .Error ("Buffer not empty" )
203+ }
204+ })
205+
206+ t .Run ("new context, no metrics set" , func (t * testing.T ) {
207+ var buf bytes.Buffer
208+ logger := emf .NewFor (& buf )
209+ logger .NewContext ().Namespace ("galaxy" )
210+ logger .Log ()
211+
212+ if buf .String () != "" {
213+ t .Error ("Buffer not empty" )
214+ }
215+ })
196216}
197217
198218func setenv (t * testing.T , env map [string ]string ) {
Original file line number Diff line number Diff line change 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 =
31github.com/kinbiko/jsonassert v1.0.1 h1:8gdLmUaPWuxk2TzQSofKRqatFH6zwTF6AsUH4bugJYY =
42github.com/kinbiko/jsonassert v1.0.1 /go.mod h1:QRwBwiAsrcJpjw+L+Q4WS8psLxuUY+HylVZS/4j74TM =
You can’t perform that action at this time.
0 commit comments