Skip to content

Commit

Permalink
Merge pull request #176 from jan--f/float-histgram-render-fix
Browse files Browse the repository at this point in the history
histogram: get sample count after distinguishing classic and native
  • Loading branch information
beorn7 committed Jul 11, 2024
2 parents db513a9 + 088592b commit 48e4461
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 2 deletions.
14 changes: 12 additions & 2 deletions prom2json.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func makeHistogram(m *dto.Metric) Histogram {
hist := Histogram{
Labels: makeLabels(m),
TimestampMs: makeTimestamp(m),
Count: fmt.Sprint(dtoH.GetSampleCount()),
Sum: fmt.Sprint(dtoH.GetSampleSum()),
}
// A native histogram is marked by at least one span.
Expand All @@ -121,11 +120,18 @@ func makeHistogram(m *dto.Metric) Histogram {
if h == nil {
// float histogram
hist.Buckets = histogram.BucketsAsJson[float64](histogram.GetAPIFloatBuckets(fh))
hist.Count = fmt.Sprint(fh.Count)
} else {
hist.Buckets = histogram.BucketsAsJson[uint64](histogram.GetAPIBuckets(h))
hist.Count = fmt.Sprint(h.Count)
}
} else {
hist.Buckets = makeBuckets(m)
if count := dtoH.GetSampleCountFloat(); count > 0 {
hist.Count = fmt.Sprint(count)
} else {
hist.Count = fmt.Sprint(dtoH.GetSampleCount())
}
}
return hist
}
Expand Down Expand Up @@ -156,7 +162,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
119 changes: 119 additions & 0 deletions prom2json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,51 @@ var tcs = []testCase{
},
},
},
testCase{
name: "test float histograms",
mFamily: &dto.MetricFamily{
Name: strPtr("histogram1"),
Type: metricTypePtr(dto.MetricType_HISTOGRAM),
Metric: []*dto.Metric{
&dto.Metric{
// Test summary with NaN
Label: []*dto.LabelPair{
createLabelPair("tag1", "abc"),
createLabelPair("tag2", "def"),
},
Histogram: &dto.Histogram{
SampleCountFloat: floatPtr(1),
SampleSum: floatPtr(2),
Bucket: []*dto.Bucket{
createFloatBucket(250000, 3),
createFloatBucket(500000, 4),
createFloatBucket(1e+06, 5),
},
},
},
},
},
output: &Family{
Name: "histogram1",
Help: "",
Type: "HISTOGRAM",
Metrics: []interface{}{
Histogram{
Labels: map[string]string{
"tag1": "abc",
"tag2": "def",
},
Buckets: map[string]string{
"250000": "3",
"500000": "4",
"1e+06": "5",
},
Count: "1",
Sum: "2",
},
},
},
},
testCase{
name: "test native histograms",
mFamily: &dto.MetricFamily{
Expand Down Expand Up @@ -279,6 +324,73 @@ var tcs = []testCase{
},
},
},
testCase{
name: "test native float histograms",
mFamily: &dto.MetricFamily{
Name: strPtr("histogram2"),
Type: metricTypePtr(dto.MetricType_HISTOGRAM),
Metric: []*dto.Metric{
&dto.Metric{
// Test summary with NaN
Label: []*dto.LabelPair{
createLabelPair("tag1", "abc"),
createLabelPair("tag2", "def"),
},
Histogram: &dto.Histogram{
SampleCountFloat: floatPtr(10),
SampleSum: floatPtr(123.45),
Schema: int32Ptr(1),
PositiveSpan: []*dto.BucketSpan{
createBucketSpan(0, 3),
createBucketSpan(1, 1),
},
PositiveCount: []float64{1, 3, 6, 10},
},
},
},
},
output: &Family{
Name: "histogram2",
Help: "",
Type: "HISTOGRAM",
Metrics: []interface{}{
Histogram{
Labels: map[string]string{
"tag1": "abc",
"tag2": "def",
},
Buckets: [][]interface{}{
{
uint64(0),
"0.7071067811865475",
"1",
"1",
},
{
uint64(0),
"1",
"1.414213562373095",
"3",
},
{
uint64(0),
"1.414213562373095",
"2",
"6",
},
{
uint64(0),
"2.82842712474619",
"4",
"10",
},
},
Count: "10",
Sum: "123.45",
},
},
},
},
}

func TestConvertToMetricFamily(t *testing.T) {
Expand Down Expand Up @@ -336,6 +448,13 @@ func createBucket(bound float64, count uint64) *dto.Bucket {
}
}

func createFloatBucket(bound float64, count float64) *dto.Bucket {
return &dto.Bucket{
UpperBound: &bound,
CumulativeCountFloat: &count,
}
}

func createBucketSpan(offset int32, length uint32) *dto.BucketSpan {
return &dto.BucketSpan{
Offset: &offset,
Expand Down

0 comments on commit 48e4461

Please sign in to comment.