diff --git a/prometheus/examples_test.go b/prometheus/examples_test.go index 6645de56b..e39b879ff 100644 --- a/prometheus/examples_test.go +++ b/prometheus/examples_test.go @@ -16,13 +16,14 @@ package prometheus_test import ( "bytes" "fmt" - "google.golang.org/protobuf/types/known/timestamppb" "math" "net/http" "runtime" "strings" "time" + "google.golang.org/protobuf/types/known/timestamppb" + //nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility. "github.com/golang/protobuf/proto" "github.com/prometheus/common/expfmt" @@ -556,12 +557,12 @@ func ExampleNewConstHistogram() { lp := dto.LabelPair{Name: &n, Value: &v} var labelPairs []*dto.LabelPair labelPairs = append(labelPairs, &lp) - val := float64(42) + vals := []float64{24.0, 42.0, 89.0, 157.0} t, _ := time.Parse("unix", "Mon Jan _2 15:04:05 MST 2006") ts := timestamppb.New(t) for i := 0; i < 4; i++ { - e := dto.Exemplar{Label: labelPairs, Value: &val, Timestamp: ts} + e := dto.Exemplar{Label: labelPairs, Value: &vals[i], Timestamp: ts} exemplars = append(exemplars, &e) } @@ -605,7 +606,7 @@ func ExampleNewConstHistogram() { // name: "testName" // value: "testVal" // > - // value: 42 + // value: 24 // timestamp: < // seconds: -62135596800 // > @@ -633,7 +634,7 @@ func ExampleNewConstHistogram() { // name: "testName" // value: "testVal" // > - // value: 42 + // value: 89 // timestamp: < // seconds: -62135596800 // > @@ -647,7 +648,7 @@ func ExampleNewConstHistogram() { // name: "testName" // value: "testVal" // > - // value: 42 + // value: 157 // timestamp: < // seconds: -62135596800 // > diff --git a/prometheus/histogram.go b/prometheus/histogram.go index 6e4cda7b7..453fb8378 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -582,7 +582,9 @@ func (h *constHistogram) Desc() *Desc { func (h *constHistogram) Write(out *dto.Metric) error { his := &dto.Histogram{} + // h.buckets, buckets and bounds are all the same length buckets := make([]*dto.Bucket, 0, len(h.buckets)) + bounds := make([]float64, 0) his.SampleCount = proto.Uint64(h.count) his.SampleSum = proto.Float64(h.sum) @@ -591,15 +593,24 @@ func (h *constHistogram) Write(out *dto.Metric) error { CumulativeCount: proto.Uint64(count), UpperBound: proto.Float64(upperBound), }) + bounds = append(bounds, upperBound) } + // make sure that both bounds and buckets have the same ordering if len(buckets) > 0 { sort.Sort(buckSort(buckets)) } + sort.Float64s(bounds) if len(h.exemplars) > 0 { - for i := 0; i < len(buckets); i++ { - buckets[i].Exemplar = h.exemplars[i] + r := len(buckets) + for i := 0; i < r; i++ { + bound := sort.SearchFloat64s(bounds, *h.exemplars[i].Value) + // Only append the exemplar if it's within the bounds defined in the + // buckets. + if bound < r { + buckets[bound].Exemplar = h.exemplars[i] + } } }