explicitly add +inf bucket in withExemplarsMetric (#1094)

* explicitly adding +inf bucket to withExemplarsMetric

Signed-off-by: Arun Mahendra <arun.mahendra@shopify.com>

* Update prometheus/metric_test.go

Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>

* Update prometheus/metric.go

Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>

* updated comment and removed unnecessary test

Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
This commit is contained in:
Arun Mahendra 2022-08-02 04:48:18 -04:00 committed by GitHub
parent c6d4e40244
commit 807b1ee73c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 4 deletions

View File

@ -15,6 +15,7 @@ package prometheus
import (
"errors"
"math"
"sort"
"strings"
"time"
@ -184,8 +185,15 @@ func (m *withExemplarsMetric) Write(pb *dto.Metric) error {
if i < len(pb.Histogram.Bucket) {
pb.Histogram.Bucket[i].Exemplar = e
} else {
// This is not possible as last bucket is Inf.
panic("no bucket was found for given exemplar value")
// The +Inf bucket should be explicitly added if there is an exemplar for it, similar to non-const histogram logic in https://github.com/prometheus/client_golang/blob/main/prometheus/histogram.go#L357-L365.
b := &dto.Bucket{
CumulativeCount: proto.Uint64(pb.Histogram.Bucket[len(pb.Histogram.GetBucket())-1].GetCumulativeCount()),
UpperBound: proto.Float64(math.Inf(1)),
Exemplar: e,
}
pb.Histogram.Bucket = append(pb.Histogram.Bucket, b)
break
// Terminating the loop after creating the +Inf bucket and adding one exemplar, if there are other exemplars in the +Inf bucket range they will be ignored.
}
}
default:

View File

@ -14,6 +14,7 @@
package prometheus
import (
"math"
"testing"
//nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility.
@ -56,16 +57,19 @@ func TestWithExemplarsMetric(t *testing.T) {
{Value: proto.Float64(89.0)},
{Value: proto.Float64(100.0)},
{Value: proto.Float64(157.0)},
{Value: proto.Float64(500.0)},
{Value: proto.Float64(2000.0)},
}}
metric := dto.Metric{}
if err := m.Write(&metric); err != nil {
t.Fatal(err)
}
if want, got := 4, len(metric.GetHistogram().Bucket); want != got {
if want, got := 5, len(metric.GetHistogram().Bucket); want != got {
t.Errorf("want %v, got %v", want, got)
}
expectedExemplarVals := []float64{24.0, 42.0, 100.0, 157.0}
// When there are more exemplars than there are buckets, a +Inf bucket will be created and the last exemplar value will be added.
expectedExemplarVals := []float64{24.0, 42.0, 100.0, 157.0, 500.0}
for i, b := range metric.GetHistogram().Bucket {
if b.Exemplar == nil {
t.Errorf("Expected exemplar for bucket %v, got nil", i)
@ -74,5 +78,11 @@ func TestWithExemplarsMetric(t *testing.T) {
t.Errorf("%v: want %v, got %v", i, want, got)
}
}
infBucket := metric.GetHistogram().Bucket[len(metric.GetHistogram().Bucket)-1].GetUpperBound()
if infBucket != math.Inf(1) {
t.Errorf("want %v, got %v", math.Inf(1), infBucket)
}
})
}