From c35f138a9a59661ffd281bd3351a241c5afd2170 Mon Sep 17 00:00:00 2001 From: beorn7 Date: Mon, 19 Jul 2021 19:58:04 +0200 Subject: [PATCH] Be more specific when identifying a sparse histogram It's a prefectly valid use case to have a sparse histogram with a zero threshold of zero (i.e. only observations of exactly zero go into the zero bucket). Even if the current PoC implementation of client_golang doesn't allow that, such a case should be ingested properly. However, there is now the edge case af a sparse histogram with a zero threshold of zero and no observations yet. Such a histogram would look the same if it was meant to be a conventional histogram. For now, we ingest this case as a conventional histogram, but the final format should have means to unambiguously express if a histogram is meant to be ingested as a sparse histogram or as a conventional histogram. Signed-off-by: beorn7 --- pkg/textparse/protobufparse.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/textparse/protobufparse.go b/pkg/textparse/protobufparse.go index a80ceaddc..d722d39a6 100644 --- a/pkg/textparse/protobufparse.go +++ b/pkg/textparse/protobufparse.go @@ -309,7 +309,7 @@ func (p *ProtobufParser) Next() (Entry, error) { p.state = EntryType case EntryType: if p.mf.GetType() == dto.MetricType_HISTOGRAM && - p.mf.GetMetric()[0].GetHistogram().GetSbZeroThreshold() != 0 { + isSparseHistogram(p.mf.GetMetric()[0].GetHistogram()) { p.state = EntryHistogram } else { p.state = EntrySeries @@ -459,3 +459,19 @@ func formatOpenMetricsFloat(f float64) string { } return s + ".0" } + +// isSparseHistogram returns false iff the provided histograms has no +// SparseBuckets and a zero threshold of 0 and a zero count of 0. In principle, +// this could still be meant to be a sparse histgram (with a zero threshold of 0 +// and no observations yet), but for now, we'll treat this case as a conventional +// histogram. +// +// TODO(beorn7): In the final format, there should be an unambiguous way of +// deciding if a histogram should be ingested as a conventional one or a sparse +// one. +func isSparseHistogram(h *dto.Histogram) bool { + return len(h.GetSbNegative().GetDelta()) > 0 || + len(h.GetSbPositive().GetDelta()) > 0 || + h.GetSbZeroCount() > 0 || + h.GetSbZeroThreshold() > 0 +}