Skip to content

Commit 819d3c1

Browse files
committed
Add more tests
1 parent 2ec9081 commit 819d3c1

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

prometheus/metric_test.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,72 @@ func TestWithExemplarsMetric(t *testing.T) {
9494
}
9595

9696
func TestWithExemplarsNativeHistogramMetric(t *testing.T) {
97-
t.Run("histogram", func(t *testing.T) {
97+
t.Run("native histogram single exemplar", 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+
t.Run("native histogram multiple exemplar", func(t *testing.T) {
98121
// Create a constant histogram from values we got from a 3rd party telemetry system.
99122
h := MustNewConstNativeHistogram(
100123
NewDesc("http_request_duration_seconds", "A histogram of the HTTP request durations.", nil, nil),
101124
10, 12.1, map[int]int64{1: 7, 2: 1, 3: 2}, map[int]int64{}, 0, 2, 0.2, time.Date(
102125
2009, 11, 17, 20, 34, 58, 651387237, time.UTC))
103126
m := &withExemplarsMetric{Metric: h, exemplars: []*dto.Exemplar{
104127
{Value: proto.Float64(2000.0), Timestamp: timestamppb.New(time.Date(2009, 11, 17, 20, 34, 58, 3243244, time.UTC))},
128+
{Value: proto.Float64(1000.0), Timestamp: timestamppb.New(time.Date(2009, 11, 17, 20, 34, 59, 3243244, time.UTC))},
105129
}}
106130
metric := dto.Metric{}
107131
if err := m.Write(&metric); err != nil {
108132
t.Fatal(err)
109133
}
134+
if want, got := 2, len(metric.GetHistogram().Exemplars); want != got {
135+
t.Errorf("want %v, got %v", want, got)
136+
}
137+
138+
for _, b := range metric.GetHistogram().Bucket {
139+
if b.Exemplar != nil {
140+
t.Error("Not expecting exemplar for bucket")
141+
}
142+
}
143+
})
144+
t.Run("native histogram exemplar without timestamp", func(t *testing.T) {
145+
// Create a constant histogram from values we got from a 3rd party telemetry system.
146+
h := MustNewConstNativeHistogram(
147+
NewDesc("http_request_duration_seconds", "A histogram of the HTTP request durations.", nil, nil),
148+
10, 12.1, map[int]int64{1: 7, 2: 1, 3: 2}, map[int]int64{}, 0, 2, 0.2, time.Date(
149+
2009, 11, 17, 20, 34, 58, 651387237, time.UTC))
150+
m := MustNewMetricWithExemplars(h, Exemplar{
151+
Value: 1000.0,
152+
})
153+
metric := dto.Metric{}
154+
if err := m.Write(&metric); err != nil {
155+
t.Fatal(err)
156+
}
110157
if want, got := 1, len(metric.GetHistogram().Exemplars); want != got {
111158
t.Errorf("want %v, got %v", want, got)
112159
}
160+
if got := metric.GetHistogram().Exemplars[0].Timestamp; got == nil {
161+
t.Errorf("Got nil timestamp")
162+
}
113163

114164
for _, b := range metric.GetHistogram().Bucket {
115165
if b.Exemplar != nil {

0 commit comments

Comments
 (0)