Fix `CumulativeCount` value of `+Inf` bucket created from exemplar (#1148)

* Fix `CumulativeCount` value of `+Inf` bucket created from exemplar

Signed-off-by: Balint Zsilavecz <balint.zsilavecz@skyscanner.net>

* Update prometheus/metric_test.go

Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
Signed-off-by: Balint Zsilavecz <balint.zsilavecz@skyscanner.net>

* Clarify description of implicit `+Inf` bucket count

Signed-off-by: Balint Zsilavecz <balint.zsilavecz@skyscanner.net>

* Fix test variables

Signed-off-by: Balint Zsilavecz <balint.zsilavecz@skyscanner.net>

Signed-off-by: Balint Zsilavecz <balint.zsilavecz@skyscanner.net>
Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
This commit is contained in:
Balint Zsilavecz 2022-10-13 12:52:19 +01:00 committed by bwplotka
parent 8cc2b6c472
commit 1f93f64580
3 changed files with 9 additions and 5 deletions

View File

@ -613,7 +613,7 @@ func (h *constHistogram) Write(out *dto.Metric) error {
// to send it to Prometheus in the Collect method. // to send it to Prometheus in the Collect method.
// //
// buckets is a map of upper bounds to cumulative counts, excluding the +Inf // buckets is a map of upper bounds to cumulative counts, excluding the +Inf
// bucket. // bucket. The +Inf bucket is implicit, and its value is equal to the provided count.
// //
// NewConstHistogram returns an error if the length of labelValues is not // NewConstHistogram returns an error if the length of labelValues is not
// consistent with the variable labels in Desc or if Desc is invalid. // consistent with the variable labels in Desc or if Desc is invalid.

View File

@ -187,7 +187,7 @@ func (m *withExemplarsMetric) Write(pb *dto.Metric) error {
} else { } else {
// 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. // 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{ b := &dto.Bucket{
CumulativeCount: proto.Uint64(pb.Histogram.Bucket[len(pb.Histogram.GetBucket())-1].GetCumulativeCount()), CumulativeCount: proto.Uint64(pb.Histogram.GetSampleCount()),
UpperBound: proto.Float64(math.Inf(1)), UpperBound: proto.Float64(math.Inf(1)),
Exemplar: e, Exemplar: e,
} }

View File

@ -79,10 +79,14 @@ func TestWithExemplarsMetric(t *testing.T) {
} }
} }
infBucket := metric.GetHistogram().Bucket[len(metric.GetHistogram().Bucket)-1].GetUpperBound() infBucket := metric.GetHistogram().Bucket[len(metric.GetHistogram().Bucket)-1]
if infBucket != math.Inf(1) { if want, got := math.Inf(1), infBucket.GetUpperBound(); want != got {
t.Errorf("want %v, got %v", math.Inf(1), infBucket) t.Errorf("want %v, got %v", want, got)
}
if want, got := uint64(4711), infBucket.GetCumulativeCount(); want != got {
t.Errorf("want %v, got %v", want, got)
} }
}) })
} }