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

Function TestTimerByOutcome

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

Source from the content-addressed store, hash-verified

143}
144
145func TestTimerByOutcome(t *testing.T) {
146 var (
147 his = NewHistogramVec(
148 HistogramOpts{Name: "test_histogram"},
149 []string{"outcome"},
150 )
151 outcome = "foo"
152 m = &dto.Metric{}
153 )
154
155 timedFunc := func() {
156 timer := NewTimer(ObserverFunc(func(v float64) {
157 his.WithLabelValues(outcome).Observe(v)
158 }))
159 defer timer.ObserveDuration()
160
161 if outcome == "foo" {
162 outcome = "bar"
163 return
164 }
165 outcome = "foo"
166 }
167
168 timedFunc()
169 his.WithLabelValues("foo").(Histogram).Write(m)
170 if want, got := uint64(0), m.GetHistogram().GetSampleCount(); want != got {
171 t.Errorf("want %d observations for 'foo' histogram, got %d", want, got)
172 }
173 m.Reset()
174 his.WithLabelValues("bar").(Histogram).Write(m)
175 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
176 t.Errorf("want %d observations for 'bar' histogram, got %d", want, got)
177 }
178
179 timedFunc()
180 m.Reset()
181 his.WithLabelValues("foo").(Histogram).Write(m)
182 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
183 t.Errorf("want %d observations for 'foo' histogram, got %d", want, got)
184 }
185 m.Reset()
186 his.WithLabelValues("bar").(Histogram).Write(m)
187 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
188 t.Errorf("want %d observations for 'bar' histogram, got %d", want, got)
189 }
190
191 timedFunc()
192 m.Reset()
193 his.WithLabelValues("foo").(Histogram).Write(m)
194 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
195 t.Errorf("want %d observations for 'foo' histogram, got %d", want, got)
196 }
197 m.Reset()
198 his.WithLabelValues("bar").(Histogram).Write(m)
199 if want, got := uint64(2), m.GetHistogram().GetSampleCount(); want != got {
200 t.Errorf("want %d observations for 'bar' histogram, got %d", want, got)
201 }
202}

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected