Debian Codegraph @

prometheus/timer_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2016 The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package prometheus
15
16 import (
17 "reflect"
18 "testing"
19
20 "google.golang.org/protobuf/proto"
21
22 dto "github.com/prometheus/client_model/go"
23 )
24
25 func TestTimerObserve(t *testing.T) {
26 var (
27 his = NewHistogram(HistogramOpts{Name: "test_histogram"})
28 sum = NewSummary(SummaryOpts{Name: "test_summary"})
29 gauge = NewGauge(GaugeOpts{Name: "test_gauge"})
30 )
31
32 func() {
33 hisTimer := NewTimer(his)
34 sumTimer := NewTimer(sum)
35 gaugeTimer := NewTimer(ObserverFunc(gauge.Set))
36 defer hisTimer.ObserveDuration()
37 defer sumTimer.ObserveDuration()
38 defer gaugeTimer.ObserveDuration()
39 }()
40
41 m := &dto.Metric{}
42 his.Write(m)
43 if want, got := uint64(1), m.GetHistogram().GetSampleCount(); want != got {
44 t.Errorf("want %d observations for histogram, got %d", want, got)
45 }
46 m.Reset()
47 sum.Write(m)
48 if want, got := uint64(1), m.GetSummary().GetSampleCount(); want != got {
49 t.Errorf("want %d observations for summary, got %d", want, got)
50 }
51 m.Reset()
52 gauge.Write(m)
53 if got := m.GetGauge().GetValue(); got <= 0 {
54 t.Errorf("want value > 0 for gauge, got %f", got)
55 }
56 }
57
58 func 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
106 func TestTimerEmpty(t *testing.T) {
107 emptyTimer := NewTimer(nil)
108 emptyTimer.ObserveDuration()
109 // Do nothing, just demonstrate it works without panic.
110 }
111
112 func 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
145 func 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 }