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

Function TestTimerObserveWithExemplar

prometheus/timer_test.go:58–104  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

56}
57
58func TestTimerObserveWithExemplar(t *testing.T) {
59 var (
60 exemplar = Labels{"foo": "bar"}
61 his = NewHistogram(HistogramOpts{Name: "test_histogram"})
62 sum = NewSummary(SummaryOpts{Name: "test_summary"})
63 gauge = NewGauge(GaugeOpts{Name: "test_gauge"})
64 )
65
66 func() {
67 hisTimer := NewTimer(his)
68 sumTimer := NewTimer(sum)
69 gaugeTimer := NewTimer(ObserverFunc(gauge.Set))
70 defer hisTimer.ObserveDurationWithExemplar(exemplar)
71 // Gauges and summaries does not implement ExemplarObserver, so we expect them to ignore exemplar.
72 defer sumTimer.ObserveDurationWithExemplar(exemplar)
73 defer gaugeTimer.ObserveDurationWithExemplar(exemplar)
74 }()
75
76 m := &dto.Metric{}
77 his.Write(m)
78 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
79 t.Errorf("want %d observations for histogram, got %d", want, got)
80 }
81 var got []*dto.LabelPair
82 for _, b := range m.GetHistogram().GetBucket() {
83 if b.Exemplar != nil {
84 got = b.Exemplar.GetLabel()
85 break
86 }
87 }
88
89 want := []*dto.LabelPair{{Name: proto.String("foo"), Value: proto.String("bar")}}
90 if !reflect.DeepEqual(got, want) {
91 t.Errorf("expected %v exemplar labels, got %v", want, got)
92 }
93
94 m.Reset()
95 sum.Write(m)
96 if want, got := uint64(1), m.GetSummary().GetSampleCount(); want != got {
97 t.Errorf("want %d observations for summary, got %d", want, got)
98 }
99 m.Reset()
100 gauge.Write(m)
101 if got := m.GetGauge().GetValue(); got <= 0 {
102 t.Errorf("want value > 0 for gauge, got %f", got)
103 }
104}
105
106func TestTimerEmpty(t *testing.T) {
107 emptyTimer := NewTimer(nil)

Callers

nothing calls this directly

Calls 10

NewTimerFunction · 0.85
ObserverFuncFuncType · 0.85
NewHistogramFunction · 0.70
NewSummaryFunction · 0.70
NewGaugeFunction · 0.70
WriteMethod · 0.65
StringMethod · 0.45
ResetMethod · 0.45
GetValueMethod · 0.45

Tested by

no test coverage detected