Skip to content

Commit

Permalink
Fixed support for unordered input of exemplars. (#1100)
Browse files Browse the repository at this point in the history
Signed-off-by: bwplotka <[email protected]>
  • Loading branch information
bwplotka authored Aug 2, 2022
1 parent 44ce5e1 commit 3faf3ba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
2 changes: 0 additions & 2 deletions prometheus/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ func (m *withExemplarsMetric) Write(pb *dto.Metric) error {
Exemplar: e,
}
pb.Histogram.Bucket = append(pb.Histogram.Bucket, b)
break
// Terminating the loop after creating the +Inf bucket and adding one exemplar, if there are other exemplars in the +Inf bucket range they will be ignored.
}
}
default:
Expand Down
16 changes: 8 additions & 8 deletions prometheus/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,19 @@ func TestWithExemplarsMetric(t *testing.T) {
h := MustNewConstHistogram(
NewDesc("http_request_duration_seconds", "A histogram of the HTTP request durations.", nil, nil),
4711, 403.34,
// Four buckets, but we expect five as the +Inf bucket will be created if we see value outside of those buckets.
map[float64]uint64{25: 121, 50: 2403, 100: 3221, 200: 4233},
)

m := &withExemplarsMetric{Metric: h, exemplars: []*dto.Exemplar{
{Value: proto.Float64(24.0)},
{Value: proto.Float64(25.1)},
{Value: proto.Float64(2000.0)}, // Unordered exemplars.
{Value: proto.Float64(500.0)},
{Value: proto.Float64(42.0)},
{Value: proto.Float64(89.0)},
{Value: proto.Float64(100.0)},
{Value: proto.Float64(157.0)},
{Value: proto.Float64(500.0)},
{Value: proto.Float64(2000.0)},
{Value: proto.Float64(100.0)},
{Value: proto.Float64(89.0)},
{Value: proto.Float64(24.0)},
{Value: proto.Float64(25.1)},
}}
metric := dto.Metric{}
if err := m.Write(&metric); err != nil {
Expand All @@ -68,8 +69,7 @@ func TestWithExemplarsMetric(t *testing.T) {
t.Errorf("want %v, got %v", want, got)
}

// When there are more exemplars than there are buckets, a +Inf bucket will be created and the last exemplar value will be added.
expectedExemplarVals := []float64{24.0, 42.0, 100.0, 157.0, 500.0}
expectedExemplarVals := []float64{24.0, 25.1, 89.0, 157.0, 500.0}
for i, b := range metric.GetHistogram().Bucket {
if b.Exemplar == nil {
t.Errorf("Expected exemplar for bucket %v, got nil", i)
Expand Down

0 comments on commit 3faf3ba

Please sign in to comment.