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

Function TestTimerConditionalTiming

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

Source from the content-addressed store, hash-verified

110}
111
112func TestTimerConditionalTiming(t *testing.T) {
113 var (
114 his = NewHistogram(HistogramOpts{
115 Name: "test_histogram",
116 })
117 timeMe = true
118 m = &dto.Metric{}
119 )
120
121 timedFunc := func() {
122 timer := NewTimer(ObserverFunc(func(v float64) {
123 if timeMe {
124 his.Observe(v)
125 }
126 }))
127 defer timer.ObserveDuration()
128 }
129
130 timedFunc() // This will time.
131 his.Write(m)
132 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
133 t.Errorf("want %d observations for histogram, got %d", want, got)
134 }
135
136 timeMe = false
137 timedFunc() // This will not time again.
138 m.Reset()
139 his.Write(m)
140 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
141 t.Errorf("want %d observations for histogram, got %d", want, got)
142 }
143}
144
145func TestTimerByOutcome(t *testing.T) {
146 var (

Callers

nothing calls this directly

Calls 7

ObserveDurationMethod · 0.95
NewTimerFunction · 0.85
ObserverFuncFuncType · 0.85
NewHistogramFunction · 0.70
ObserveMethod · 0.65
WriteMethod · 0.65
ResetMethod · 0.45

Tested by

no test coverage detected