summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/pkg/prometheus/metric_family_test.go
blob: f373996da737710a6325e101cc6c48b5f926f1cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package prometheus

import (
	"testing"

	"github.com/prometheus/common/model"
	"github.com/prometheus/prometheus/model/labels"
	"github.com/stretchr/testify/assert"
)

func TestMetricFamilies_Len(t *testing.T) {
	tests := map[string]struct {
		mfs     MetricFamilies
		wantLen int
	}{
		"initialized with two elements": {
			mfs:     MetricFamilies{"1": nil, "2": nil},
			wantLen: 2,
		},
		"not initialized": {
			mfs:     nil,
			wantLen: 0,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.mfs.Len(), test.wantLen)
		})
	}
}

func TestMetricFamilies_Get(t *testing.T) {
	const n = "metric"

	tests := map[string]struct {
		mfs    MetricFamilies
		wantMF *MetricFamily
	}{
		"etric is found": {
			mfs:    MetricFamilies{n: &MetricFamily{name: n}},
			wantMF: &MetricFamily{name: n},
		},
		"metric is not found": {
			mfs:    MetricFamilies{"!" + n: &MetricFamily{name: n}},
			wantMF: nil,
		},
		"not initialized": {
			mfs:    nil,
			wantMF: nil,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.mfs.Get(n), test.wantMF)
		})
	}
}

func TestMetricFamilies_GetGauge(t *testing.T) {
	const n = "metric"

	tests := map[string]struct {
		mfs    MetricFamilies
		wantMF *MetricFamily
	}{
		"metric is found and is Gauge": {
			mfs:    MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
			wantMF: &MetricFamily{name: n, typ: model.MetricTypeGauge},
		},
		"metric is found but it is not Gauge": {
			mfs:    MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeUnknown}},
			wantMF: nil,
		},
		"metric is not found": {
			mfs:    MetricFamilies{"!" + n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
			wantMF: nil,
		},
		"not initialized": {
			mfs:    nil,
			wantMF: nil,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.mfs.GetGauge(n), test.wantMF)
		})
	}
}

func TestMetricFamilies_GetCounter(t *testing.T) {
	const n = "metric"

	tests := map[string]struct {
		mfs    MetricFamilies
		wantMF *MetricFamily
	}{
		"metric is found and is Counter": {
			mfs:    MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeCounter}},
			wantMF: &MetricFamily{name: n, typ: model.MetricTypeCounter},
		},
		"metric is found but it is not Counter": {
			mfs:    MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
			wantMF: nil,
		},
		"metric is not found": {
			mfs:    MetricFamilies{"!" + n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
			wantMF: nil,
		},
		"not initialized": {
			mfs:    nil,
			wantMF: nil,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.mfs.GetCounter(n), test.wantMF)
		})
	}
}

func TestMetricFamilies_GetSummary(t *testing.T) {
	const n = "metric"

	tests := map[string]struct {
		mfs    MetricFamilies
		wantMF *MetricFamily
	}{
		"metric is found and is Summary": {
			mfs:    MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeSummary}},
			wantMF: &MetricFamily{name: n, typ: model.MetricTypeSummary},
		},
		"metric is found but it is not Summary": {
			mfs:    MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
			wantMF: nil,
		},
		"metric is not found": {
			mfs:    MetricFamilies{"!" + n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
			wantMF: nil,
		},
		"not initialized": {
			mfs:    nil,
			wantMF: nil,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.mfs.GetSummary(n), test.wantMF)
		})
	}
}

func TestMetricFamilies_GetHistogram(t *testing.T) {
	const n = "metric"

	tests := map[string]struct {
		mfs    MetricFamilies
		wantMF *MetricFamily
	}{
		"metric is found and is Histogram": {
			mfs:    MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeHistogram}},
			wantMF: &MetricFamily{name: n, typ: model.MetricTypeHistogram},
		},
		"metric is found but it is not Histogram": {
			mfs:    MetricFamilies{n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
			wantMF: nil,
		},
		"metric is not found": {
			mfs:    MetricFamilies{"!" + n: &MetricFamily{name: n, typ: model.MetricTypeGauge}},
			wantMF: nil,
		},
		"not initialized": {
			mfs:    nil,
			wantMF: nil,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.mfs.GetHistogram(n), test.wantMF)
		})
	}
}

func TestMetricFamily_Name(t *testing.T) {
	mf := &MetricFamily{name: "name"}
	assert.Equal(t, mf.Name(), "name")
}

func TestMetricFamily_Type(t *testing.T) {
	mf := &MetricFamily{typ: model.MetricTypeGauge}
	assert.Equal(t, mf.Type(), model.MetricTypeGauge)
}

func TestMetricFamily_Help(t *testing.T) {
	mf := &MetricFamily{help: "help"}
	assert.Equal(t, mf.Help(), "help")
}

func TestMetricFamily_Metrics(t *testing.T) {
	metrics := []Metric{{gauge: &Gauge{value: 1}, counter: &Counter{value: 1}}}
	mf := &MetricFamily{metrics: metrics}
	assert.Equal(t, mf.Metrics(), metrics)
}

func TestMetric_Labels(t *testing.T) {
	lbs := labels.Labels{{Name: "1", Value: "1"}, {Name: "2", Value: "2"}}
	m := &Metric{labels: lbs}
	assert.Equal(t, m.Labels(), lbs)
}

func TestMetric_Gauge(t *testing.T) {
	tests := map[string]struct {
		m    *Metric
		want *Gauge
	}{
		"gauge set": {
			m:    &Metric{gauge: &Gauge{value: 1}},
			want: &Gauge{value: 1},
		},
		"gauge not set": {
			m:    &Metric{},
			want: nil,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.m.Gauge(), test.want)
		})
	}
}

func TestMetric_Counter(t *testing.T) {
	tests := map[string]struct {
		m    *Metric
		want *Counter
	}{
		"counter set": {
			m:    &Metric{counter: &Counter{value: 1}},
			want: &Counter{value: 1},
		},
		"counter not set": {
			m:    &Metric{},
			want: nil,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.m.Counter(), test.want)
		})
	}
}

func TestMetric_Summary(t *testing.T) {
	tests := map[string]struct {
		m    *Metric
		want *Summary
	}{
		"summary set": {
			m:    &Metric{summary: &Summary{sum: 0.1, count: 3}},
			want: &Summary{sum: 0.1, count: 3},
		},
		"summary not set": {
			m:    &Metric{},
			want: nil,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.m.Summary(), test.want)
		})
	}
}

func TestMetric_Histogram(t *testing.T) {
	tests := map[string]struct {
		m    *Metric
		want *Histogram
	}{
		"histogram set": {
			m:    &Metric{histogram: &Histogram{sum: 0.1, count: 3}},
			want: &Histogram{sum: 0.1, count: 3},
		},
		"histogram not set": {
			m:    &Metric{},
			want: nil,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			assert.Equal(t, test.m.Histogram(), test.want)
		})
	}
}

func TestGauge_Value(t *testing.T) {
	assert.Equal(t, Gauge{value: 1}.Value(), 1.0)
}

func TestCounter_Value(t *testing.T) {
	assert.Equal(t, Counter{value: 1}.Value(), 1.0)
}

func TestSummary_Sum(t *testing.T) {
	assert.Equal(t, Summary{sum: 1}.Sum(), 1.0)
}

func TestSummary_Count(t *testing.T) {
	assert.Equal(t, Summary{count: 1}.Count(), 1.0)
}

func TestSummary_Quantiles(t *testing.T) {
	assert.Equal(t,
		Summary{quantiles: []Quantile{{quantile: 0.1, value: 1}}}.Quantiles(),
		[]Quantile{{quantile: 0.1, value: 1}},
	)
}

func TestQuantile_Value(t *testing.T) {
	assert.Equal(t, Quantile{value: 1}.Value(), 1.0)
}

func TestQuantile_Quantile(t *testing.T) {
	assert.Equal(t, Quantile{quantile: 0.1}.Quantile(), 0.1)
}

func TestHistogram_Sum(t *testing.T) {
	assert.Equal(t, Histogram{sum: 1}.Sum(), 1.0)
}

func TestHistogram_Count(t *testing.T) {
	assert.Equal(t, Histogram{count: 1}.Count(), 1.0)
}

func TestHistogram_Buckets(t *testing.T) {
	assert.Equal(t,
		Histogram{buckets: []Bucket{{upperBound: 0.1, cumulativeCount: 1}}}.Buckets(),
		[]Bucket{{upperBound: 0.1, cumulativeCount: 1}},
	)
}

func TestBucket_UpperBound(t *testing.T) {
	assert.Equal(t, Bucket{upperBound: 0.1}.UpperBound(), 0.1)
}

func TestBucket_CumulativeCount(t *testing.T) {
	assert.Equal(t, Bucket{cumulativeCount: 1}.CumulativeCount(), 1.0)
}