MCPcopy
hub / github.com/prometheus/client_golang / TestWithExemplarsMetric

Function TestWithExemplarsMetric

prometheus/metric_test.go:48–96  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

46}
47
48func TestWithExemplarsMetric(t *testing.T) {
49 t.Run("histogram", func(t *testing.T) {
50 // Create a constant histogram from values we got from a 3rd party telemetry system.
51 h := MustNewConstHistogram(
52 NewDesc("http_request_duration_seconds", "A histogram of the HTTP request durations.", nil, nil),
53 4711, 403.34,
54 // Four buckets, but we expect five as the +Inf bucket will be created if we see value outside of those buckets.
55 map[float64]uint64{25: 121, 50: 2403, 100: 3221, 200: 4233},
56 )
57
58 m := &withExemplarsMetric{Metric: h, exemplars: []*dto.Exemplar{
59 {Value: proto.Float64(2000.0)}, // Unordered exemplars.
60 {Value: proto.Float64(500.0)},
61 {Value: proto.Float64(42.0)},
62 {Value: proto.Float64(157.0)},
63 {Value: proto.Float64(100.0)},
64 {Value: proto.Float64(89.0)},
65 {Value: proto.Float64(24.0)},
66 {Value: proto.Float64(25.1)},
67 }}
68 metric := dto.Metric{}
69 if err := m.Write(&metric); err != nil {
70 t.Fatal(err)
71 }
72 if want, got := 5, len(metric.GetHistogram().Bucket); want != got {
73 t.Errorf("want %v, got %v", want, got)
74 }
75
76 expectedExemplarVals := []float64{24.0, 25.1, 89.0, 157.0, 500.0}
77 for i, b := range metric.GetHistogram().Bucket {
78 if b.Exemplar == nil {
79 t.Errorf("Expected exemplar for bucket %v, got nil", i)
80 }
81 if want, got := expectedExemplarVals[i], *metric.GetHistogram().Bucket[i].Exemplar.Value; want != got {
82 t.Errorf("%v: want %v, got %v", i, want, got)
83 }
84 }
85
86 infBucket := metric.GetHistogram().Bucket[len(metric.GetHistogram().Bucket)-1]
87
88 if want, got := math.Inf(1), infBucket.GetUpperBound(); want != got {
89 t.Errorf("want %v, got %v", want, got)
90 }
91
92 if want, got := uint64(4711), infBucket.GetCumulativeCount(); want != got {
93 t.Errorf("want %v, got %v", want, got)
94 }
95 })
96}
97
98func TestWithExemplarsNativeHistogramMetric(t *testing.T) {
99 t.Run("native histogram single exemplar", func(t *testing.T) {

Callers

nothing calls this directly

Calls 4

WriteMethod · 0.95
MustNewConstHistogramFunction · 0.85
NewDescFunction · 0.85
RunMethod · 0.80

Tested by

no test coverage detected