Skip to content

Commit 3d5f17f

Browse files
authored
fix: json output emits zero values (#212)
Metrics with zero values were being omitted. This change makes it so they are emitted with the zero value.
1 parent c512cf9 commit 3d5f17f

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

internal/command/formatter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func jsonEnvelope(e *loggregator_v2.Envelope) ([]byte, error) {
192192

193193
return json.Marshal(m)
194194
default:
195-
return protojson.Marshal(e)
195+
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(e)
196196
}
197197
}
198198

internal/command/tail_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,10 @@ var _ = Describe("LogCache", func() {
278278
)
279279

280280
Expect(writer.bytes).To(MatchJSON(fmt.Sprintf(`{"batch":[
281-
{"timestamp":"%d","source_id":"app-name","instance_id":"0","event":{"title":"some-title","body":"some-body"}},
282-
{"timestamp":"%d","source_id":"app-name","instance_id":"0","timer":{"name":"http","start":"1517940773000000000","stop":"1517940773000000000"}},
283-
{"timestamp":"%d","source_id":"app-name","instance_id":"0","gauge":{"metrics":{"some-name":{"unit":"my-unit","value":99}}}},
284-
{"timestamp":"%d","source_id":"app-name","instance_id":"0","counter":{"name":"some-name","total":"99"}},
281+
{"timestamp":"%d","source_id":"app-name","instance_id":"0","deprecated_tags":{},"tags":{},"event":{"title":"some-title","body":"some-body"}},
282+
{"timestamp":"%d","source_id":"app-name","instance_id":"0","deprecated_tags":{},"tags":{},"timer":{"name":"http","start":"1517940773000000000","stop":"1517940773000000000"}},
283+
{"timestamp":"%d","source_id":"app-name","instance_id":"0","deprecated_tags":{},"tags":{},"gauge":{"metrics":{"some-name":{"unit":"my-unit","value":99},"other-name":{"unit":"other-unit","value":0}}}},
284+
{"timestamp":"%d","source_id":"app-name","instance_id":"0","deprecated_tags":{},"tags":{},"counter":{"name":"some-name","total":"99","delta":"0"}},
285285
{"timestamp":"%d","source_id":"app-name","instance_id":"0","tags":{"source_type":"APP/PROC/WEB"},"log":{"payload":"log body"}}
286286
]}`, startTime.UnixNano(), startTime.UnixNano(), startTime.UnixNano(), startTime.UnixNano(), startTime.UnixNano())))
287287
})
@@ -304,9 +304,9 @@ var _ = Describe("LogCache", func() {
304304
)
305305

306306
Expect(writer.bytes).To(MatchJSON(fmt.Sprintf(`{"batch":[
307-
{"timestamp":"%d","source_id":"app-name","instance_id":"0","timer":{"name":"http","start":"1517940773000000000","stop":"1517940773000000000"}},
308-
{"timestamp":"%d","source_id":"app-name","instance_id":"0","gauge":{"metrics":{"some-name":{"unit":"my-unit","value":99}}}},
309-
{"timestamp":"%d","source_id":"app-name","instance_id":"0","counter":{"name":"some-name","total":"99"}}
307+
{"timestamp":"%d","source_id":"app-name","instance_id":"0","deprecated_tags":{},"tags":{},"timer":{"name":"http","start":"1517940773000000000","stop":"1517940773000000000"}},
308+
{"timestamp":"%d","source_id":"app-name","instance_id":"0","deprecated_tags":{},"tags":{},"gauge":{"metrics":{"some-name":{"unit":"my-unit","value":99},"other-name":{"unit":"other-unit","value":0}}}},
309+
{"timestamp":"%d","source_id":"app-name","instance_id":"0","deprecated_tags":{},"tags":{},"counter":{"name":"some-name","total":"99","delta":"0"}}
310310
]}`, startTime.UnixNano(), startTime.UnixNano(), startTime.UnixNano())))
311311

312312
Expect(httpClient.requestURLs).ToNot(BeEmpty())
@@ -334,7 +334,7 @@ var _ = Describe("LogCache", func() {
334334
)
335335

336336
Expect(writer.bytes).To(MatchJSON(fmt.Sprintf(`{"batch":[
337-
{"timestamp":"%d","source_id":"app-name","instance_id":"0","event":{"title":"some-title","body":"some-body"}},
337+
{"timestamp":"%d","source_id":"app-name","instance_id":"0","deprecated_tags":{},"tags":{},"event":{"title":"some-title","body":"some-body"}},
338338
{"timestamp":"%d","source_id":"app-name","instance_id":"0","tags":{"source_type":"APP/PROC/WEB"},"log":{"payload":"log body"}}
339339
]}`, startTime.UnixNano(), startTime.UnixNano())))
340340

@@ -1895,6 +1895,10 @@ var mixedResponseTemplate = `{
18951895
"some-name": {
18961896
"value": 99,
18971897
"unit":"my-unit"
1898+
},
1899+
"other-name": {
1900+
"value": 0,
1901+
"unit": "other-unit"
18981902
}
18991903
}
19001904
}

0 commit comments

Comments
 (0)