Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exemplar support for const histogram #986

Merged
merged 7 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add values to correct bucket
Signed-off-by: William Perron <[email protected]>
  • Loading branch information
wperron committed Mar 7, 2022
commit 47d3c542b3c985cacb59ba6d46da64197d015905
13 changes: 7 additions & 6 deletions prometheus/examples_test.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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