Skip to content

Commit 9114c17

Browse files
committed
api: support classic float histograms
Signed-off-by: Jan Fajerski <[email protected]>
1 parent 0a7ebfb commit 9114c17

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

api/v1/api.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,11 @@ func makeEncodableMetrics(metrics []*dto.Metric, metricsType dto.MetricType) []e
245245
}
246246
} else {
247247
metric["buckets"] = makeBuckets(m)
248-
metric["count"] = fmt.Sprint(dtoH.GetSampleCount())
248+
if count := dtoH.GetSampleCountFloat(); count > 0 {
249+
metric["count"] = fmt.Sprint(count)
250+
} else {
251+
metric["count"] = fmt.Sprint(dtoH.GetSampleCount())
252+
}
249253
}
250254
default:
251255
metric["value"] = fmt.Sprint(getValue(m))
@@ -274,7 +278,11 @@ func makeQuantiles(m *dto.Metric) map[string]string {
274278
func makeBuckets(m *dto.Metric) map[string]string {
275279
result := map[string]string{}
276280
for _, b := range m.GetHistogram().Bucket {
277-
result[fmt.Sprint(b.GetUpperBound())] = fmt.Sprint(b.GetCumulativeCount())
281+
if count := b.GetCumulativeCountFloat(); count > 0 {
282+
result[fmt.Sprint(b.GetUpperBound())] = fmt.Sprint(count)
283+
} else {
284+
result[fmt.Sprint(b.GetUpperBound())] = fmt.Sprint(b.GetCumulativeCount())
285+
}
278286
}
279287
return result
280288
}

api/v1/api_test.go

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,51 @@ var (
7474
Label: []*dto.LabelPair{
7575
{
7676
Name: proto.String("testing"),
77-
Value: proto.String("int histogram"),
77+
Value: proto.String("int classic histogram"),
78+
},
79+
},
80+
Histogram: &dto.Histogram{
81+
SampleCount: proto.Uint64(20),
82+
SampleSum: proto.Float64(99.23),
83+
Bucket: []*dto.Bucket{
84+
{
85+
UpperBound: proto.Float64(250000),
86+
CumulativeCount: proto.Uint64(3),
87+
},
88+
{
89+
UpperBound: proto.Float64(500000),
90+
CumulativeCount: proto.Uint64(17),
91+
},
92+
},
93+
},
94+
},
95+
{
96+
Label: []*dto.LabelPair{
97+
{
98+
Name: proto.String("testing"),
99+
Value: proto.String("float classic histogram"),
100+
},
101+
},
102+
Histogram: &dto.Histogram{
103+
SampleCountFloat: proto.Float64(20),
104+
SampleSum: proto.Float64(99.23),
105+
Bucket: []*dto.Bucket{
106+
{
107+
UpperBound: proto.Float64(250000),
108+
CumulativeCountFloat: proto.Float64(3),
109+
},
110+
{
111+
UpperBound: proto.Float64(500000),
112+
CumulativeCountFloat: proto.Float64(17),
113+
},
114+
},
115+
},
116+
},
117+
{
118+
Label: []*dto.LabelPair{
119+
{
120+
Name: proto.String("testing"),
121+
Value: proto.String("int native histogram"),
78122
},
79123
},
80124
Histogram: &dto.Histogram{
@@ -109,7 +153,7 @@ var (
109153
Label: []*dto.LabelPair{
110154
{
111155
Name: proto.String("testing"),
112-
Value: proto.String("float histogram"),
156+
Value: proto.String("float native histogram"),
113157
},
114158
},
115159
Histogram: &dto.Histogram{
@@ -261,6 +305,32 @@ func TestMetricsAPI(t *testing.T) {
261305
"time_stamp": "2020-03-10T00:54:08.025744841+05:30",
262306
"type": "HISTOGRAM",
263307
"metrics": [
308+
{
309+
"buckets": {
310+
"250000": "3",
311+
"500000": "17"
312+
},
313+
"count": "20",
314+
"labels": {
315+
"instance": "inst'a\"n\\ce1",
316+
"job": "Björn",
317+
"testing": "int classic histogram"
318+
},
319+
"sum": "99.23"
320+
},
321+
{
322+
"buckets": {
323+
"250000": "3",
324+
"500000": "17"
325+
},
326+
"count": "20",
327+
"labels": {
328+
"instance": "inst'a\"n\\ce1",
329+
"job": "Björn",
330+
"testing": "float classic histogram"
331+
},
332+
"sum": "99.23"
333+
},
264334
{
265335
"buckets": [
266336
[
@@ -280,7 +350,7 @@ func TestMetricsAPI(t *testing.T) {
280350
"labels": {
281351
"instance": "inst'a\"n\\ce1",
282352
"job": "Björn",
283-
"testing": "int histogram"
353+
"testing": "int native histogram"
284354
},
285355
"sum": "99.23"
286356
},
@@ -327,7 +397,7 @@ func TestMetricsAPI(t *testing.T) {
327397
"labels": {
328398
"instance": "inst'a\"n\\ce1",
329399
"job": "Björn",
330-
"testing": "float histogram"
400+
"testing": "float native histogram"
331401
},
332402
"sum": "99.23"
333403
}

0 commit comments

Comments
 (0)