Skip to content

Commit

Permalink
Merge pull request #668 from jan--f/classic-float-histograms
Browse files Browse the repository at this point in the history
api: support classic float histograms
  • Loading branch information
beorn7 authored Jul 11, 2024
2 parents 0a7ebfb + 9114c17 commit 0429a61
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 6 deletions.
12 changes: 10 additions & 2 deletions api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
Expand Down
78 changes: 74 additions & 4 deletions api/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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": [
[
Expand All @@ -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"
},
Expand Down Expand Up @@ -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"
}
Expand Down

0 comments on commit 0429a61

Please sign in to comment.