Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

histogram: get sample count after distinguishing classic and native #176

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even classic histograms can be float histograms, so we need to treat this differently for both classic and native histograms.

So what we have to do is to first check dtoH.GetSampleCountFloat(). If that's > 0, take it. Otherwise, take dtoH.GetSampleCount().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh I didn't know that. I guess we need the same in pushgateway too :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, although I think it will never really happen that somebody pushes a classic float histogram to the PGW. But yes, in principle, we want that in the PGW, too.

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