Skip to content

Commit 7569bb1

Browse files
committed
fix tests
1 parent 58d00f5 commit 7569bb1

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

prometheus/metric.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,13 @@ func (m *withExemplarsMetric) Write(pb *dto.Metric) error {
186186
case pb.Counter != nil:
187187
pb.Counter.Exemplar = m.exemplars[len(m.exemplars)-1]
188188
case pb.Histogram != nil:
189-
if *pb.Histogram.Schema > math.MinInt32 {
190-
pb.Histogram.Exemplars = append(pb.Histogram.Exemplars, m.exemplars...)
191-
}
192189
for _, e := range m.exemplars {
190+
if pb.Histogram.Schema != nil {
191+
if *pb.Histogram.Schema > math.MinInt32 && e.GetTimestamp() != nil {
192+
pb.Histogram.Exemplars = append(pb.Histogram.Exemplars, e)
193+
}
194+
continue
195+
}
193196
// pb.Histogram.Bucket are sorted by UpperBound.
194197
i := sort.Search(len(pb.Histogram.Bucket), func(i int) bool {
195198
return pb.Histogram.Bucket[i].GetUpperBound() >= e.GetValue()

prometheus/metric_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ package prometheus
1616
import (
1717
"math"
1818
"testing"
19+
"time"
1920

2021
dto "github.com/prometheus/client_model/go"
2122

2223
"google.golang.org/protobuf/proto"
24+
"google.golang.org/protobuf/types/known/timestamppb"
2325
)
2426

2527
func TestBuildFQName(t *testing.T) {
@@ -90,3 +92,29 @@ func TestWithExemplarsMetric(t *testing.T) {
9092
}
9193
})
9294
}
95+
96+
func TestWithExemplarsNativeHistogramMetric(t *testing.T) {
97+
t.Run("histogram", func(t *testing.T) {
98+
// Create a constant histogram from values we got from a 3rd party telemetry system.
99+
h := MustNewConstNativeHistogram(
100+
NewDesc("http_request_duration_seconds", "A histogram of the HTTP request durations.", nil, nil),
101+
10, 12.1, map[int]int64{1: 7, 2: 1, 3: 2}, map[int]int64{}, 0, 2, 0.2, time.Date(
102+
2009, 11, 17, 20, 34, 58, 651387237, time.UTC))
103+
m := &withExemplarsMetric{Metric: h, exemplars: []*dto.Exemplar{
104+
{Value: proto.Float64(2000.0), Timestamp: timestamppb.New(time.Date(2009, 11, 17, 20, 34, 58, 3243244, time.UTC))},
105+
}}
106+
metric := dto.Metric{}
107+
if err := m.Write(&metric); err != nil {
108+
t.Fatal(err)
109+
}
110+
if want, got := 1, len(metric.GetHistogram().Exemplars); want != got {
111+
t.Errorf("want %v, got %v", want, got)
112+
}
113+
114+
for _, b := range metric.GetHistogram().Bucket {
115+
if b.Exemplar != nil {
116+
t.Error("Not expecting exemplar for bucket")
117+
}
118+
}
119+
})
120+
}

0 commit comments

Comments
 (0)