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>
This commit is contained in:
beorn7 2022-05-03 18:18:55 +02:00
parent 61d6d1df18
commit d16b314b72
2 changed files with 7 additions and 1 deletions

View File

@ -146,6 +146,9 @@ func (p Point) MarshalJSON() ([]byte, error) {
it := p.H.AllBucketIterator() it := p.H.AllBucketIterator()
for it.Next() { for it.Next() {
bucket := it.At() 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. boundaries := 2 // Exclusive on both sides AKA open interval.
if bucket.LowerInclusive { if bucket.LowerInclusive {
if bucket.UpperInclusive { if bucket.UpperInclusive {

View File

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