Histogram: Do not render empty buckets in JSON output

While empty buckets can make sense in the internal representation (by
joining spans that would otherwise need more overhead for separate
representation), there are no spans in the JSON rendering. Therefore,
the JSON should not contain any empty buckets, since any buckets not
included in the output counts as empty anyway.

This changes both the inefficient MarshalJSON implementation as well
as the jsoniter implementation.

Signed-off-by: beorn7 <beorn@grafana.com>
pull/10666/head
beorn7 3 years ago
parent 61d6d1df18
commit d16b314b72

@ -146,6 +146,9 @@ func (p Point) MarshalJSON() ([]byte, error) {
it := p.H.AllBucketIterator()
for it.Next() {
bucket := it.At()
if bucket.Count == 0 {
continue // No need to expose empty buckets in JSON.
}
boundaries := 2 // Exclusive on both sides AKA open interval.
if bucket.LowerInclusive {
if bucket.UpperInclusive {

@ -1802,13 +1802,16 @@ func marshalHistogram(h *histogram.FloatHistogram, stream *jsoniter.Stream) {
bucketFound := false
it := h.AllBucketIterator()
for it.Next() {
bucket := it.At()
if bucket.Count == 0 {
continue // No need to expose empty buckets in JSON.
}
stream.WriteMore()
if !bucketFound {
stream.WriteObjectField(`buckets`)
stream.WriteArrayStart()
}
bucketFound = true
bucket := it.At()
boundaries := 2 // Exclusive on both sides AKA open interval.
if bucket.LowerInclusive {
if bucket.UpperInclusive {

Loading…
Cancel
Save