From d16b314b72c98c1a7ca28919e54938bd328e24b3 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Tue, 3 May 2022 18:18:55 +0200 Subject: [PATCH] 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 --- promql/value.go | 3 +++ web/api/v1/api.go | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/promql/value.go b/promql/value.go index cd36c9a7e..861dba6ae 100644 --- a/promql/value.go +++ b/promql/value.go @@ -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 { diff --git a/web/api/v1/api.go b/web/api/v1/api.go index 2669350e0..ca63927a4 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -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 {