From 9114c17625649b27757ecd647d7ba24867c79cf5 Mon Sep 17 00:00:00 2001 From: Jan Fajerski Date: Thu, 11 Jul 2024 15:23:00 +0200 Subject: [PATCH] api: support classic float histograms Signed-off-by: Jan Fajerski --- api/v1/api.go | 12 +++++-- api/v1/api_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/api/v1/api.go b/api/v1/api.go index 2e63c72a..9e625c2a 100644 --- a/api/v1/api.go +++ b/api/v1/api.go @@ -245,7 +245,11 @@ func makeEncodableMetrics(metrics []*dto.Metric, metricsType dto.MetricType) []e } } else { metric["buckets"] = makeBuckets(m) - metric["count"] = fmt.Sprint(dtoH.GetSampleCount()) + if count := dtoH.GetSampleCountFloat(); count > 0 { + metric["count"] = fmt.Sprint(count) + } else { + metric["count"] = fmt.Sprint(dtoH.GetSampleCount()) + } } default: metric["value"] = fmt.Sprint(getValue(m)) @@ -274,7 +278,11 @@ func makeQuantiles(m *dto.Metric) map[string]string { func makeBuckets(m *dto.Metric) map[string]string { result := map[string]string{} for _, b := range m.GetHistogram().Bucket { - result[fmt.Sprint(b.GetUpperBound())] = fmt.Sprint(b.GetCumulativeCount()) + if count := b.GetCumulativeCountFloat(); count > 0 { + result[fmt.Sprint(b.GetUpperBound())] = fmt.Sprint(count) + } else { + result[fmt.Sprint(b.GetUpperBound())] = fmt.Sprint(b.GetCumulativeCount()) + } } return result } diff --git a/api/v1/api_test.go b/api/v1/api_test.go index 40efacc5..2c4cc3d0 100644 --- a/api/v1/api_test.go +++ b/api/v1/api_test.go @@ -74,7 +74,51 @@ var ( Label: []*dto.LabelPair{ { Name: proto.String("testing"), - Value: proto.String("int histogram"), + Value: proto.String("int classic histogram"), + }, + }, + Histogram: &dto.Histogram{ + SampleCount: proto.Uint64(20), + SampleSum: proto.Float64(99.23), + Bucket: []*dto.Bucket{ + { + UpperBound: proto.Float64(250000), + CumulativeCount: proto.Uint64(3), + }, + { + UpperBound: proto.Float64(500000), + CumulativeCount: proto.Uint64(17), + }, + }, + }, + }, + { + Label: []*dto.LabelPair{ + { + Name: proto.String("testing"), + Value: proto.String("float classic histogram"), + }, + }, + Histogram: &dto.Histogram{ + SampleCountFloat: proto.Float64(20), + SampleSum: proto.Float64(99.23), + Bucket: []*dto.Bucket{ + { + UpperBound: proto.Float64(250000), + CumulativeCountFloat: proto.Float64(3), + }, + { + UpperBound: proto.Float64(500000), + CumulativeCountFloat: proto.Float64(17), + }, + }, + }, + }, + { + Label: []*dto.LabelPair{ + { + Name: proto.String("testing"), + Value: proto.String("int native histogram"), }, }, Histogram: &dto.Histogram{ @@ -109,7 +153,7 @@ var ( Label: []*dto.LabelPair{ { Name: proto.String("testing"), - Value: proto.String("float histogram"), + Value: proto.String("float native histogram"), }, }, Histogram: &dto.Histogram{ @@ -261,6 +305,32 @@ func TestMetricsAPI(t *testing.T) { "time_stamp": "2020-03-10T00:54:08.025744841+05:30", "type": "HISTOGRAM", "metrics": [ + { + "buckets": { + "250000": "3", + "500000": "17" + }, + "count": "20", + "labels": { + "instance": "inst'a\"n\\ce1", + "job": "Björn", + "testing": "int classic histogram" + }, + "sum": "99.23" + }, + { + "buckets": { + "250000": "3", + "500000": "17" + }, + "count": "20", + "labels": { + "instance": "inst'a\"n\\ce1", + "job": "Björn", + "testing": "float classic histogram" + }, + "sum": "99.23" + }, { "buckets": [ [ @@ -280,7 +350,7 @@ func TestMetricsAPI(t *testing.T) { "labels": { "instance": "inst'a\"n\\ce1", "job": "Björn", - "testing": "int histogram" + "testing": "int native histogram" }, "sum": "99.23" }, @@ -327,7 +397,7 @@ func TestMetricsAPI(t *testing.T) { "labels": { "instance": "inst'a\"n\\ce1", "job": "Björn", - "testing": "float histogram" + "testing": "float native histogram" }, "sum": "99.23" }