Skip to content

Commit

Permalink
Fix CumulativeCount value of +Inf bucket created from exemplar (#…
Browse files Browse the repository at this point in the history
…1148)

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

Signed-off-by: Balint Zsilavecz <[email protected]>

* Update prometheus/metric_test.go

Co-authored-by: Bartlomiej Plotka <[email protected]>
Signed-off-by: Balint Zsilavecz <[email protected]>

* Clarify description of implicit `+Inf` bucket count

Signed-off-by: Balint Zsilavecz <[email protected]>

* Fix test variables

Signed-off-by: Balint Zsilavecz <[email protected]>

Signed-off-by: Balint Zsilavecz <[email protected]>
Co-authored-by: Bartlomiej Plotka <[email protected]>
  • Loading branch information
balintzs and bwplotka committed Nov 1, 2022
1 parent 8cc2b6c commit 1f93f64
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion prometheus/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ func (h *constHistogram) Write(out *dto.Metric) error {
// to send it to Prometheus in the Collect method.
//
// 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
// consistent with the variable labels in Desc or if Desc is invalid.
Expand Down
2 changes: 1 addition & 1 deletion prometheus/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (m *withExemplarsMetric) Write(pb *dto.Metric) error {
} 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.
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)),
Exemplar: e,
}
Expand Down
10 changes: 7 additions & 3 deletions prometheus/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
t.Errorf("want %v, got %v", math.Inf(1), infBucket)
if want, got := math.Inf(1), infBucket.GetUpperBound(); want != got {
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)
}
})
}

0 comments on commit 1f93f64

Please sign in to comment.