Skip to content

Commit

Permalink
Add values to correct bucket
Browse files Browse the repository at this point in the history
Signed-off-by: William Perron <william.perron@shopify.com>
  • Loading branch information
wperron committed Mar 7, 2022
1 parent ad6f1f6 commit 47d3c54
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
13 changes: 7 additions & 6 deletions prometheus/examples_test.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -605,7 +606,7 @@ func ExampleNewConstHistogram() {
// name: "testName"
// value: "testVal"
// >
// value: 42
// value: 24
// timestamp: <
// seconds: -62135596800
// >
Expand Down Expand Up @@ -633,7 +634,7 @@ func ExampleNewConstHistogram() {
// name: "testName"
// value: "testVal"
// >
// value: 42
// value: 89
// timestamp: <
// seconds: -62135596800
// >
Expand All @@ -647,7 +648,7 @@ func ExampleNewConstHistogram() {
// name: "testName"
// value: "testVal"
// >
// value: 42
// value: 157
// timestamp: <
// seconds: -62135596800
// >
Expand Down
15 changes: 13 additions & 2 deletions prometheus/histogram.go
Expand Up @@ -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)
Expand All @@ -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]
}
}
}

Expand Down

0 comments on commit 47d3c54

Please sign in to comment.