master
go 387 lines 8.78 KB
Raw
1 package prometheus
2
3 import (
4 "math"
5 "testing"
6
7 "github.com/prometheus/common/model"
8 "github.com/prometheus/prometheus/model/labels"
9 "github.com/stretchr/testify/assert"
10 )
11
12 func TestMetricFamilies_Len(t *testing.T) {
13 tests := map[string]struct {
14 mfs MetricFamilies
15 wantLen int
16 }{
17 "initialized with two elements": {
18 mfs: MetricFamilies{"1": nil, "2": nil},
19 wantLen: 2,
20 },
21 "not initialized": {
22 mfs: nil,
23 wantLen: 0,
24 },
25 }
26
27 for name, test := range tests {
28 t.Run(name, func(t *testing.T) {
29 assert.Equal(t, test.mfs.Len(), test.wantLen)
30 })
31 }
32 }
33
34 func TestMetricFamilies_Get(t *testing.T) {
35 const n = "metric"
36
37 tests := map[string]struct {
38 mfs MetricFamilies
39 wantMF *MetricFamily
40 }{
41 "etric is found": {
42 mfs: MetricFamilies{n: &MetricFamily{name: n}},
43 wantMF: &MetricFamily{name: n},
44 },
45 "metric is not found": {
46 mfs: MetricFamilies{"!" + n: &MetricFamily{name: n}},
47 wantMF: nil,
48 },
49 "not initialized": {
50 mfs: nil,
51 wantMF: nil,
52 },
53 }
54
55 for name, test := range tests {
56 t.Run(name, func(t *testing.T) {
57 assert.Equal(t, test.mfs.Get(n), test.wantMF)
58 })
59 }
60 }
61
62 func TestMetricFamilies_GetGauge(t *testing.T) {
63 const n = "metric"
64
65 tests := map[string]struct {
66 mfs MetricFamilies
67 wantMF *MetricFamily
68 }{
69 "metric is found and is Gauge": {
70 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
71 wantMF: &MetricFamily{name: n, typ: model.MetricTypeGauge},
72 },
73 "metric is found but it is not Gauge": {
74 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeUnknown}},
75 wantMF: nil,
76 },
77 "metric is not found": {
78 mfs: MetricFamilies{"!" + n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
79 wantMF: nil,
80 },
81 "not initialized": {
82 mfs: nil,
83 wantMF: nil,
84 },
85 }
86
87 for name, test := range tests {
88 t.Run(name, func(t *testing.T) {
89 assert.Equal(t, test.mfs.GetGauge(n), test.wantMF)
90 })
91 }
92 }
93
94 func TestMetricFamilies_GetCounter(t *testing.T) {
95 const n = "metric"
96
97 tests := map[string]struct {
98 mfs MetricFamilies
99 wantMF *MetricFamily
100 }{
101 "metric is found and is Counter": {
102 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeCounter}},
103 wantMF: &MetricFamily{name: n, typ: model.MetricTypeCounter},
104 },
105 "metric is found but it is not Counter": {
106 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
107 wantMF: nil,
108 },
109 "metric is not found": {
110 mfs: MetricFamilies{"!" + n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
111 wantMF: nil,
112 },
113 "not initialized": {
114 mfs: nil,
115 wantMF: nil,
116 },
117 }
118
119 for name, test := range tests {
120 t.Run(name, func(t *testing.T) {
121 assert.Equal(t, test.mfs.GetCounter(n), test.wantMF)
122 })
123 }
124 }
125
126 func TestMetricFamilies_GetSummary(t *testing.T) {
127 const n = "metric"
128
129 tests := map[string]struct {
130 mfs MetricFamilies
131 wantMF *MetricFamily
132 }{
133 "metric is found and is Summary": {
134 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeSummary}},
135 wantMF: &MetricFamily{name: n, typ: model.MetricTypeSummary},
136 },
137 "metric is found but it is not Summary": {
138 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
139 wantMF: nil,
140 },
141 "metric is not found": {
142 mfs: MetricFamilies{"!" + n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
143 wantMF: nil,
144 },
145 "not initialized": {
146 mfs: nil,
147 wantMF: nil,
148 },
149 }
150
151 for name, test := range tests {
152 t.Run(name, func(t *testing.T) {
153 assert.Equal(t, test.mfs.GetSummary(n), test.wantMF)
154 })
155 }
156 }
157
158 func TestMetricFamilies_GetHistogram(t *testing.T) {
159 const n = "metric"
160
161 tests := map[string]struct {
162 mfs MetricFamilies
163 wantMF *MetricFamily
164 }{
165 "metric is found and is Histogram": {
166 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeHistogram}},
167 wantMF: &MetricFamily{name: n, typ: model.MetricTypeHistogram},
168 },
169 "metric is found but it is not Histogram": {
170 mfs: MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
171 wantMF: nil,
172 },
173 "metric is not found": {
174 mfs: MetricFamilies{"!" + n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
175 wantMF: nil,
176 },
177 "not initialized": {
178 mfs: nil,
179 wantMF: nil,
180 },
181 }
182
183 for name, test := range tests {
184 t.Run(name, func(t *testing.T) {
185 assert.Equal(t, test.mfs.GetHistogram(n), test.wantMF)
186 })
187 }
188 }
189
190 func TestMetricFamily_Name(t *testing.T) {
191 mf := &MetricFamily{name: "name"}
192 assert.Equal(t, mf.Name(), "name")
193 }
194
195 func TestMetricFamily_Type(t *testing.T) {
196 mf := &MetricFamily{typ: model.MetricTypeGauge}
197 assert.Equal(t, mf.Type(), model.MetricTypeGauge)
198 }
199
200 func TestMetricFamily_Help(t *testing.T) {
201 mf := &MetricFamily{help: "help"}
202 assert.Equal(t, mf.Help(), "help")
203 }
204
205 func TestMetricFamily_Metrics(t *testing.T) {
206 metrics := []Metric{{gauge: &Gauge{value: 1}, counter: &Counter{value: 1}}}
207 mf := &MetricFamily{metrics: metrics}
208 assert.Equal(t, mf.Metrics(), metrics)
209 }
210
211 func TestMetric_Labels(t *testing.T) {
212 lbs := labels.Labels{{Name: "1", Value: "1"}, {Name: "2", Value: "2"}}
213 m := &Metric{labels: lbs}
214 assert.Equal(t, m.Labels(), lbs)
215 }
216
217 func TestMetric_Gauge(t *testing.T) {
218 tests := map[string]struct {
219 m *Metric
220 want *Gauge
221 }{
222 "gauge set": {
223 m: &Metric{gauge: &Gauge{value: 1}},
224 want: &Gauge{value: 1},
225 },
226 "gauge not set": {
227 m: &Metric{},
228 want: nil,
229 },
230 }
231
232 for name, test := range tests {
233 t.Run(name, func(t *testing.T) {
234 assert.Equal(t, test.m.Gauge(), test.want)
235 })
236 }
237 }
238
239 func TestMetric_Counter(t *testing.T) {
240 tests := map[string]struct {
241 m *Metric
242 want *Counter
243 }{
244 "counter set": {
245 m: &Metric{counter: &Counter{value: 1}},
246 want: &Counter{value: 1},
247 },
248 "counter not set": {
249 m: &Metric{},
250 want: nil,
251 },
252 }
253
254 for name, test := range tests {
255 t.Run(name, func(t *testing.T) {
256 assert.Equal(t, test.m.Counter(), test.want)
257 })
258 }
259 }
260
261 func TestMetric_Summary(t *testing.T) {
262 tests := map[string]struct {
263 m *Metric
264 want *Summary
265 }{
266 "summary set": {
267 m: &Metric{summary: &Summary{sum: 0.1, count: 3}},
268 want: &Summary{sum: 0.1, count: 3},
269 },
270 "summary not set": {
271 m: &Metric{},
272 want: nil,
273 },
274 }
275
276 for name, test := range tests {
277 t.Run(name, func(t *testing.T) {
278 assert.Equal(t, test.m.Summary(), test.want)
279 })
280 }
281 }
282
283 func TestMetric_Histogram(t *testing.T) {
284 tests := map[string]struct {
285 m *Metric
286 want *Histogram
287 }{
288 "histogram set": {
289 m: &Metric{histogram: &Histogram{sum: 0.1, count: 3}},
290 want: &Histogram{sum: 0.1, count: 3},
291 },
292 "histogram not set": {
293 m: &Metric{},
294 want: nil,
295 },
296 }
297
298 for name, test := range tests {
299 t.Run(name, func(t *testing.T) {
300 assert.Equal(t, test.m.Histogram(), test.want)
301 })
302 }
303 }
304
305 func TestGauge_Value(t *testing.T) {
306 assert.Equal(t, Gauge{value: 1}.Value(), 1.0)
307 }
308
309 func TestCounter_Value(t *testing.T) {
310 assert.Equal(t, Counter{value: 1}.Value(), 1.0)
311 }
312
313 func TestSummary_Sum(t *testing.T) {
314 assert.Equal(t, Summary{sum: 1}.Sum(), 1.0)
315 }
316
317 func TestSummary_Count(t *testing.T) {
318 assert.Equal(t, Summary{count: 1}.Count(), 1.0)
319 }
320
321 func TestSummary_Quantiles(t *testing.T) {
322 assert.Equal(t,
323 Summary{quantiles: []Quantile{{quantile: 0.1, value: 1}}}.Quantiles(),
324 []Quantile{{quantile: 0.1, value: 1}},
325 )
326 }
327
328 func TestSummary_IsNaN(t *testing.T) {
329 tests := map[string]struct {
330 summary Summary
331 want bool
332 }{
333 "all quantiles NaN": {
334 summary: Summary{quantiles: []Quantile{{quantile: 0.5, value: math.NaN()}, {quantile: 0.9, value: math.NaN()}}},
335 want: true,
336 },
337 "no quantiles": {
338 summary: Summary{},
339 want: true,
340 },
341 "mix of NaN and real quantiles": {
342 summary: Summary{quantiles: []Quantile{{quantile: 0.5, value: math.NaN()}, {quantile: 0.9, value: 0.4}}},
343 want: false,
344 },
345 "all quantiles real": {
346 summary: Summary{quantiles: []Quantile{{quantile: 0.5, value: 0.1}, {quantile: 0.9, value: 0.4}}},
347 want: false,
348 },
349 }
350
351 for name, test := range tests {
352 t.Run(name, func(t *testing.T) {
353 assert.Equal(t, test.want, test.summary.IsNaN())
354 })
355 }
356 }
357
358 func TestQuantile_Value(t *testing.T) {
359 assert.Equal(t, Quantile{value: 1}.Value(), 1.0)
360 }
361
362 func TestQuantile_Quantile(t *testing.T) {
363 assert.Equal(t, Quantile{quantile: 0.1}.Quantile(), 0.1)
364 }
365
366 func TestHistogram_Sum(t *testing.T) {
367 assert.Equal(t, Histogram{sum: 1}.Sum(), 1.0)
368 }
369
370 func TestHistogram_Count(t *testing.T) {
371 assert.Equal(t, Histogram{count: 1}.Count(), 1.0)
372 }
373
374 func TestHistogram_Buckets(t *testing.T) {
375 assert.Equal(t,
376 Histogram{buckets: []Bucket{{upperBound: 0.1, cumulativeCount: 1}}}.Buckets(),
377 []Bucket{{upperBound: 0.1, cumulativeCount: 1}},
378 )
379 }
380
381 func TestBucket_UpperBound(t *testing.T) {
382 assert.Equal(t, Bucket{upperBound: 0.1}.UpperBound(), 0.1)
383 }
384
385 func TestBucket_CumulativeCount(t *testing.T) {
386 assert.Equal(t, Bucket{cumulativeCount: 1}.CumulativeCount(), 1.0)
387 }