Debian Codegraph @

prometheus/vec_test.go

raw
1 // Copyright 2014 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 "fmt"
18 "reflect"
19 "strconv"
20 "testing"
21
22 dto "github.com/prometheus/client_model/go"
23 )
24
25 func TestDelete(t *testing.T) {
26 vec := NewGaugeVec(
27 GaugeOpts{
28 Name: "test",
29 Help: "helpless",
30 },
31 []string{"l1", "l2"},
32 )
33 testDelete(t, vec)
34 }
35
36 func TestDeleteWithCollisions(t *testing.T) {
37 vec := NewGaugeVec(
38 GaugeOpts{
39 Name: "test",
40 Help: "helpless",
41 },
42 []string{"l1", "l2"},
43 )
44 vec.hashAdd = func(h uint64, s string) uint64 { return 1 }
45 vec.hashAddByte = func(h uint64, b byte) uint64 { return 1 }
46 testDelete(t, vec)
47 }
48
49 func TestDeleteWithConstraints(t *testing.T) {
50 vec := V2.NewGaugeVec(GaugeVecOpts{
51 GaugeOpts{
52 Name: "test",
53 Help: "helpless",
54 },
55 ConstrainedLabels{
56 {Name: "l1"},
57 {Name: "l2", Constraint: func(s string) string { return "x" + s }},
58 },
59 })
60 testDelete(t, vec)
61 }
62
63 func testDelete(t *testing.T, vec *GaugeVec) {
64 if got, want := vec.Delete(Labels{"l1": "v1", "l2": "v2"}), false; got != want {
65 t.Errorf("got %v, want %v", got, want)
66 }
67
68 vec.With(Labels{"l1": "v1", "l2": "v2"}).Set(42)
69 if got, want := vec.Delete(Labels{"l1": "v1", "l2": "v2"}), true; got != want {
70 t.Errorf("got %v, want %v", got, want)
71 }
72 if got, want := vec.Delete(Labels{"l1": "v1", "l2": "v2"}), false; got != want {
73 t.Errorf("got %v, want %v", got, want)
74 }
75
76 vec.With(Labels{"l1": "v1", "l2": "v2"}).Set(42)
77 if got, want := vec.Delete(Labels{"l2": "v2", "l1": "v1"}), true; got != want {
78 t.Errorf("got %v, want %v", got, want)
79 }
80 if got, want := vec.Delete(Labels{"l2": "v2", "l1": "v1"}), false; got != want {
81 t.Errorf("got %v, want %v", got, want)
82 }
83
84 vec.With(Labels{"l1": "v1", "l2": "v2"}).Set(42)
85 if got, want := vec.Delete(Labels{"l2": "v1", "l1": "v2"}), false; got != want {
86 t.Errorf("got %v, want %v", got, want)
87 }
88 if got, want := vec.Delete(Labels{"l1": "v1"}), false; got != want {
89 t.Errorf("got %v, want %v", got, want)
90 }
91 }
92
93 func TestDeleteLabelValues(t *testing.T) {
94 vec := NewGaugeVec(
95 GaugeOpts{
96 Name: "test",
97 Help: "helpless",
98 },
99 []string{"l1", "l2"},
100 )
101 testDeleteLabelValues(t, vec)
102 }
103
104 func TestDeleteLabelValuesWithCollisions(t *testing.T) {
105 vec := NewGaugeVec(
106 GaugeOpts{
107 Name: "test",
108 Help: "helpless",
109 },
110 []string{"l1", "l2"},
111 )
112 vec.hashAdd = func(h uint64, s string) uint64 { return 1 }
113 vec.hashAddByte = func(h uint64, b byte) uint64 { return 1 }
114 testDeleteLabelValues(t, vec)
115 }
116
117 func TestDeleteLabelValuesWithConstraints(t *testing.T) {
118 vec := V2.NewGaugeVec(GaugeVecOpts{
119 GaugeOpts{
120 Name: "test",
121 Help: "helpless",
122 },
123 ConstrainedLabels{
124 {Name: "l1"},
125 {Name: "l2", Constraint: func(s string) string { return "x" + s }},
126 },
127 })
128 testDeleteLabelValues(t, vec)
129 }
130
131 func testDeleteLabelValues(t *testing.T, vec *GaugeVec) {
132 if got, want := vec.DeleteLabelValues("v1", "v2"), false; got != want {
133 t.Errorf("got %v, want %v", got, want)
134 }
135
136 vec.With(Labels{"l1": "v1", "l2": "v2"}).Set(42)
137 vec.With(Labels{"l1": "v1", "l2": "v3"}).Set(42) // Add junk data for collision.
138 if got, want := vec.DeleteLabelValues("v1", "v2"), true; got != want {
139 t.Errorf("got %v, want %v", got, want)
140 }
141 if got, want := vec.DeleteLabelValues("v1", "v2"), false; got != want {
142 t.Errorf("got %v, want %v", got, want)
143 }
144 if got, want := vec.DeleteLabelValues("v1", "v3"), true; got != want {
145 t.Errorf("got %v, want %v", got, want)
146 }
147
148 vec.With(Labels{"l1": "v1", "l2": "v2"}).Set(42)
149 // Delete out of order.
150 if got, want := vec.DeleteLabelValues("v2", "v1"), false; got != want {
151 t.Errorf("got %v, want %v", got, want)
152 }
153 if got, want := vec.DeleteLabelValues("v1"), false; got != want {
154 t.Errorf("got %v, want %v", got, want)
155 }
156 }
157
158 func TestDeletePartialMatch(t *testing.T) {
159 vec := NewGaugeVec(
160 GaugeOpts{
161 Name: "test",
162 Help: "helpless",
163 },
164 []string{"l1", "l2", "l3"},
165 )
166 testDeletePartialMatch(t, vec)
167 }
168
169 func TestDeletePartialMatchWithConstraints(t *testing.T) {
170 vec := V2.NewGaugeVec(GaugeVecOpts{
171 GaugeOpts{
172 Name: "test",
173 Help: "helpless",
174 },
175 ConstrainedLabels{
176 {Name: "l1"},
177 {Name: "l2", Constraint: func(s string) string { return "x" + s }},
178 {Name: "l3"},
179 },
180 })
181 testDeletePartialMatch(t, vec)
182 }
183
184 func testDeletePartialMatch(t *testing.T, baseVec *GaugeVec) {
185 assertNoMetric := func(t *testing.T) {
186 if n := len(baseVec.metrics); n != 0 {
187 t.Error("expected no metrics, got", n)
188 }
189 }
190
191 // No metric value is set.
192 if got, want := baseVec.DeletePartialMatch(Labels{"l1": "v1", "l2": "v2"}), 0; got != want {
193 t.Errorf("got %v, want %v", got, want)
194 }
195
196 baseVec.With(Labels{"l1": "baseValue1", "l2": "baseValue2", "l3": "baseValue3"}).Inc()
197 baseVec.With(Labels{"l1": "multiDeleteV1", "l2": "diff1BaseValue2", "l3": "v3"}).Set(42)
198 baseVec.With(Labels{"l1": "multiDeleteV1", "l2": "diff2BaseValue2", "l3": "v3"}).Set(84)
199 baseVec.With(Labels{"l1": "multiDeleteV1", "l2": "diff3BaseValue2", "l3": "v3"}).Set(168)
200
201 curriedVec := baseVec.MustCurryWith(Labels{"l2": "curriedValue2"})
202 curriedVec.WithLabelValues("curriedValue1", "curriedValue3").Inc()
203 curriedVec.WithLabelValues("curriedValue1", "differentCurriedValue3").Inc()
204 curriedVec.WithLabelValues("differentCurriedValue1", "differentCurriedValue3").Inc()
205
206 // Try to delete nonexistent label with existent value from curried vector.
207 if got, want := curriedVec.DeletePartialMatch(Labels{"lx": "curriedValue1"}), 0; got != want {
208 t.Errorf("got %v, want %v", got, want)
209 }
210
211 // Try to delete valid label with nonexistent value from curried vector.
212 if got, want := curriedVec.DeletePartialMatch(Labels{"l1": "badValue1"}), 0; got != want {
213 t.Errorf("got %v, want %v", got, want)
214 }
215
216 // Try to delete from a curried vector based on labels which were curried.
217 // This operation succeeds when run against the base vector below.
218 if got, want := curriedVec.DeletePartialMatch(Labels{"l2": "curriedValue2"}), 0; got != want {
219 t.Errorf("got %v, want %v", got, want)
220 }
221
222 // Try to delete from a curried vector based on labels which were curried,
223 // but the value actually exists in the base vector.
224 if got, want := curriedVec.DeletePartialMatch(Labels{"l2": "baseValue2"}), 0; got != want {
225 t.Errorf("got %v, want %v", got, want)
226 }
227
228 // Delete multiple matching metrics from a curried vector based on partial values.
229 if got, want := curriedVec.DeletePartialMatch(Labels{"l1": "curriedValue1"}), 2; got != want {
230 t.Errorf("got %v, want %v", got, want)
231 }
232
233 // Try to delete nonexistent label with existent value from base vector.
234 if got, want := baseVec.DeletePartialMatch(Labels{"lx": "curriedValue1"}), 0; got != want {
235 t.Errorf("got %v, want %v", got, want)
236 }
237
238 // Try to delete partially invalid labels from base vector.
239 if got, want := baseVec.DeletePartialMatch(Labels{"l1": "baseValue1", "l2": "badValue2"}), 0; got != want {
240 t.Errorf("got %v, want %v", got, want)
241 }
242
243 // Delete from the base vector based on values which were curried.
244 // This operation fails when run against a curried vector above.
245 if got, want := baseVec.DeletePartialMatch(Labels{"l2": "curriedValue2"}), 1; got != want {
246 t.Errorf("got %v, want %v", got, want)
247 }
248
249 // Delete multiple metrics from the base vector based on a single valid label.
250 if got, want := baseVec.DeletePartialMatch(Labels{"l1": "multiDeleteV1"}), 3; got != want {
251 t.Errorf("got %v, want %v", got, want)
252 }
253
254 // Delete from the base vector based on values which were not curried.
255 if got, want := baseVec.DeletePartialMatch(Labels{"l3": "baseValue3"}), 1; got != want {
256 t.Errorf("got %v, want %v", got, want)
257 }
258
259 // All metrics should have been deleted now.
260 assertNoMetric(t)
261 }
262
263 func TestMetricVec(t *testing.T) {
264 vec := NewGaugeVec(
265 GaugeOpts{
266 Name: "test",
267 Help: "helpless",
268 },
269 []string{"l1", "l2"},
270 )
271 testMetricVec(t, vec)
272 }
273
274 func TestMetricVecWithCollisions(t *testing.T) {
275 vec := NewGaugeVec(
276 GaugeOpts{
277 Name: "test",
278 Help: "helpless",
279 },
280 []string{"l1", "l2"},
281 )
282 vec.hashAdd = func(h uint64, s string) uint64 { return 1 }
283 vec.hashAddByte = func(h uint64, b byte) uint64 { return 1 }
284 testMetricVec(t, vec)
285 }
286
287 func testMetricVec(t *testing.T, vec *GaugeVec) {
288 vec.Reset() // Actually test Reset now!
289
290 var pair [2]string
291 // Keep track of metrics.
292 expected := map[[2]string]int{}
293
294 for i := 0; i < 1000; i++ {
295 pair[0], pair[1] = strconv.Itoa(i%4), strconv.Itoa(i%5) // Varying combinations multiples.
296 expected[pair]++
297 vec.WithLabelValues(pair[0], pair[1]).Inc()
298
299 expected[[2]string{"v1", "v2"}]++
300 vec.WithLabelValues("v1", "v2").Inc()
301 }
302
303 var total int
304 for _, metrics := range vec.metrics {
305 for _, metric := range metrics {
306 total++
307 copy(pair[:], metric.values)
308
309 var metricOut dto.Metric
310 if err := metric.metric.Write(&metricOut); err != nil {
311 t.Fatal(err)
312 }
313 actual := *metricOut.Gauge.Value
314
315 var actualPair [2]string
316 for i, label := range metricOut.Label {
317 actualPair[i] = *label.Value
318 }
319
320 // Test output pair against metric.values to ensure we've selected
321 // the right one. We check this to ensure the below check means
322 // anything at all.
323 if actualPair != pair {
324 t.Fatalf("unexpected pair association in metric map: %v != %v", actualPair, pair)
325 }
326
327 if actual != float64(expected[pair]) {
328 t.Fatalf("incorrect counter value for %v: %v != %v", pair, actual, expected[pair])
329 }
330 }
331 }
332
333 if total != len(expected) {
334 t.Fatalf("unexpected number of metrics: %v != %v", total, len(expected))
335 }
336
337 vec.Reset()
338
339 if len(vec.metrics) > 0 {
340 t.Fatalf("reset failed")
341 }
342 }
343
344 func TestMetricVecWithConstraints(t *testing.T) {
345 constraint := func(s string) string { return "x" + s }
346 vec := V2.NewGaugeVec(GaugeVecOpts{
347 GaugeOpts{
348 Name: "test",
349 Help: "helpless",
350 },
351 ConstrainedLabels{
352 {Name: "l1"},
353 {Name: "l2", Constraint: constraint},
354 },
355 })
356 testConstrainedMetricVec(t, vec, constraint)
357 }
358
359 func testConstrainedMetricVec(t *testing.T, vec *GaugeVec, constrain func(string) string) {
360 vec.Reset() // Actually test Reset now!
361
362 var pair [2]string
363 // Keep track of metrics.
364 expected := map[[2]string]int{}
365
366 for i := 0; i < 1000; i++ {
367 pair[0], pair[1] = strconv.Itoa(i%4), strconv.Itoa(i%5) // Varying combinations multiples.
368 expected[[2]string{pair[0], constrain(pair[1])}]++
369 vec.WithLabelValues(pair[0], pair[1]).Inc()
370
371 expected[[2]string{"v1", constrain("v2")}]++
372 vec.WithLabelValues("v1", "v2").Inc()
373 }
374
375 var total int
376 for _, metrics := range vec.metrics {
377 for _, metric := range metrics {
378 total++
379 copy(pair[:], metric.values)
380
381 var metricOut dto.Metric
382 if err := metric.metric.Write(&metricOut); err != nil {
383 t.Fatal(err)
384 }
385 actual := *metricOut.Gauge.Value
386
387 var actualPair [2]string
388 for i, label := range metricOut.Label {
389 actualPair[i] = *label.Value
390 }
391
392 // Test output pair against metric.values to ensure we've selected
393 // the right one. We check this to ensure the below check means
394 // anything at all.
395 if actualPair != pair {
396 t.Fatalf("unexpected pair association in metric map: %v != %v", actualPair, pair)
397 }
398
399 if actual != float64(expected[pair]) {
400 t.Fatalf("incorrect counter value for %v: %v != %v", pair, actual, expected[pair])
401 }
402 }
403 }
404
405 if total != len(expected) {
406 t.Fatalf("unexpected number of metrics: %v != %v", total, len(expected))
407 }
408
409 vec.Reset()
410
411 if len(vec.metrics) > 0 {
412 t.Fatalf("reset failed")
413 }
414 }
415
416 func TestCounterVecEndToEndWithCollision(t *testing.T) {
417 vec := NewCounterVec(
418 CounterOpts{
419 Name: "test",
420 Help: "helpless",
421 },
422 []string{"labelname"},
423 )
424 vec.WithLabelValues("77kepQFQ8Kl").Inc()
425 vec.WithLabelValues("!0IC=VloaY").Add(2)
426
427 m := &dto.Metric{}
428 if err := vec.WithLabelValues("77kepQFQ8Kl").Write(m); err != nil {
429 t.Fatal(err)
430 }
431 if got, want := m.GetLabel()[0].GetValue(), "77kepQFQ8Kl"; got != want {
432 t.Errorf("got label value %q, want %q", got, want)
433 }
434 if got, want := m.GetCounter().GetValue(), 1.; got != want {
435 t.Errorf("got value %f, want %f", got, want)
436 }
437 m.Reset()
438 if err := vec.WithLabelValues("!0IC=VloaY").Write(m); err != nil {
439 t.Fatal(err)
440 }
441 if got, want := m.GetLabel()[0].GetValue(), "!0IC=VloaY"; got != want {
442 t.Errorf("got label value %q, want %q", got, want)
443 }
444 if got, want := m.GetCounter().GetValue(), 2.; got != want {
445 t.Errorf("got value %f, want %f", got, want)
446 }
447 }
448
449 func TestCurryVec(t *testing.T) {
450 vec := NewCounterVec(
451 CounterOpts{
452 Name: "test",
453 Help: "helpless",
454 },
455 []string{"one", "two", "three"},
456 )
457 testCurryVec(t, vec)
458 }
459
460 func TestCurryVecWithCollisions(t *testing.T) {
461 vec := NewCounterVec(
462 CounterOpts{
463 Name: "test",
464 Help: "helpless",
465 },
466 []string{"one", "two", "three"},
467 )
468 vec.hashAdd = func(h uint64, s string) uint64 { return 1 }
469 vec.hashAddByte = func(h uint64, b byte) uint64 { return 1 }
470 testCurryVec(t, vec)
471 }
472
473 func TestCurryVecWithConstraints(t *testing.T) {
474 constraint := func(s string) string { return "x" + s }
475 t.Run("constrainedLabels overlap variableLabels", func(t *testing.T) {
476 vec := V2.NewCounterVec(CounterVecOpts{
477 CounterOpts{
478 Name: "test",
479 Help: "helpless",
480 },
481 ConstrainedLabels{
482 {Name: "one"},
483 {Name: "two"},
484 {Name: "three", Constraint: constraint},
485 },
486 })
487 testCurryVec(t, vec)
488 })
489 t.Run("constrainedLabels reducing cardinality", func(t *testing.T) {
490 constraint := func(s string) string { return "x" }
491 vec := V2.NewCounterVec(CounterVecOpts{
492 CounterOpts{
493 Name: "test",
494 Help: "helpless",
495 },
496 ConstrainedLabels{
497 {Name: "one"},
498 {Name: "two"},
499 {Name: "three", Constraint: constraint},
500 },
501 })
502 testConstrainedCurryVec(t, vec, constraint)
503 })
504 }
505
506 func testCurryVec(t *testing.T, vec *CounterVec) {
507 assertMetrics := func(t *testing.T) {
508 n := 0
509 for _, m := range vec.metrics {
510 n += len(m)
511 }
512 if n != 2 {
513 t.Error("expected two metrics, got", n)
514 }
515 m := &dto.Metric{}
516 c1, err := vec.GetMetricWithLabelValues("1", "2", "3")
517 if err != nil {
518 t.Fatal("unexpected error getting metric:", err)
519 }
520 c1.Write(m)
521 if want, got := 1., m.GetCounter().GetValue(); want != got {
522 t.Errorf("want %f as counter value, got %f", want, got)
523 }
524 m.Reset()
525 c2, err := vec.GetMetricWithLabelValues("11", "22", "33")
526 if err != nil {
527 t.Fatal("unexpected error getting metric:", err)
528 }
529 c2.Write(m)
530 if want, got := 1., m.GetCounter().GetValue(); want != got {
531 t.Errorf("want %f as counter value, got %f", want, got)
532 }
533 }
534
535 assertNoMetric := func(t *testing.T) {
536 if n := len(vec.metrics); n != 0 {
537 t.Error("expected no metrics, got", n)
538 }
539 }
540
541 t.Run("zero labels", func(t *testing.T) {
542 c1 := vec.MustCurryWith(nil)
543 c2 := vec.MustCurryWith(nil)
544 c1.WithLabelValues("1", "2", "3").Inc()
545 c2.With(Labels{"one": "11", "two": "22", "three": "33"}).Inc()
546 assertMetrics(t)
547 if !c1.Delete(Labels{"one": "1", "two": "2", "three": "3"}) {
548 t.Error("deletion failed")
549 }
550 if !c2.DeleteLabelValues("11", "22", "33") {
551 t.Error("deletion failed")
552 }
553 assertNoMetric(t)
554 })
555 t.Run("first label", func(t *testing.T) {
556 c1 := vec.MustCurryWith(Labels{"one": "1"})
557 c2 := vec.MustCurryWith(Labels{"one": "11"})
558 c1.WithLabelValues("2", "3").Inc()
559 c2.With(Labels{"two": "22", "three": "33"}).Inc()
560 assertMetrics(t)
561 if c1.Delete(Labels{"two": "22", "three": "33"}) {
562 t.Error("deletion unexpectedly succeeded")
563 }
564 if c2.DeleteLabelValues("2", "3") {
565 t.Error("deletion unexpectedly succeeded")
566 }
567 if !c1.Delete(Labels{"two": "2", "three": "3"}) {
568 t.Error("deletion failed")
569 }
570 if !c2.DeleteLabelValues("22", "33") {
571 t.Error("deletion failed")
572 }
573 assertNoMetric(t)
574 })
575 t.Run("middle label", func(t *testing.T) {
576 c1 := vec.MustCurryWith(Labels{"two": "2"})
577 c2 := vec.MustCurryWith(Labels{"two": "22"})
578 c1.WithLabelValues("1", "3").Inc()
579 c2.With(Labels{"one": "11", "three": "33"}).Inc()
580 assertMetrics(t)
581 if c1.Delete(Labels{"one": "11", "three": "33"}) {
582 t.Error("deletion unexpectedly succeeded")
583 }
584 if c2.DeleteLabelValues("1", "3") {
585 t.Error("deletion unexpectedly succeeded")
586 }
587 if !c1.Delete(Labels{"one": "1", "three": "3"}) {
588 t.Error("deletion failed")
589 }
590 if !c2.DeleteLabelValues("11", "33") {
591 t.Error("deletion failed")
592 }
593 assertNoMetric(t)
594 })
595 t.Run("last label", func(t *testing.T) {
596 c1 := vec.MustCurryWith(Labels{"three": "3"})
597 c2 := vec.MustCurryWith(Labels{"three": "33"})
598 c1.WithLabelValues("1", "2").Inc()
599 c2.With(Labels{"one": "11", "two": "22"}).Inc()
600 assertMetrics(t)
601 if c1.Delete(Labels{"two": "22", "one": "11"}) {
602 t.Error("deletion unexpectedly succeeded")
603 }
604 if c2.DeleteLabelValues("1", "2") {
605 t.Error("deletion unexpectedly succeeded")
606 }
607 if !c1.Delete(Labels{"two": "2", "one": "1"}) {
608 t.Error("deletion failed")
609 }
610 if !c2.DeleteLabelValues("11", "22") {
611 t.Error("deletion failed")
612 }
613 assertNoMetric(t)
614 })
615 t.Run("two labels", func(t *testing.T) {
616 c1 := vec.MustCurryWith(Labels{"three": "3", "one": "1"})
617 c2 := vec.MustCurryWith(Labels{"three": "33", "one": "11"})
618 c1.WithLabelValues("2").Inc()
619 c2.With(Labels{"two": "22"}).Inc()
620 assertMetrics(t)
621 if c1.Delete(Labels{"two": "22"}) {
622 t.Error("deletion unexpectedly succeeded")
623 }
624 if c2.DeleteLabelValues("2") {
625 t.Error("deletion unexpectedly succeeded")
626 }
627 if !c1.Delete(Labels{"two": "2"}) {
628 t.Error("deletion failed")
629 }
630 if !c2.DeleteLabelValues("22") {
631 t.Error("deletion failed")
632 }
633 assertNoMetric(t)
634 })
635 t.Run("all labels", func(t *testing.T) {
636 c1 := vec.MustCurryWith(Labels{"three": "3", "two": "2", "one": "1"})
637 c2 := vec.MustCurryWith(Labels{"three": "33", "one": "11", "two": "22"})
638 c1.WithLabelValues().Inc()
639 c2.With(nil).Inc()
640 assertMetrics(t)
641 if !c1.Delete(Labels{}) {
642 t.Error("deletion failed")
643 }
644 if !c2.DeleteLabelValues() {
645 t.Error("deletion failed")
646 }
647 assertNoMetric(t)
648 })
649 t.Run("double curry", func(t *testing.T) {
650 c1 := vec.MustCurryWith(Labels{"three": "3"}).MustCurryWith(Labels{"one": "1"})
651 c2 := vec.MustCurryWith(Labels{"three": "33"}).MustCurryWith(Labels{"one": "11"})
652 c1.WithLabelValues("2").Inc()
653 c2.With(Labels{"two": "22"}).Inc()
654 assertMetrics(t)
655 if c1.Delete(Labels{"two": "22"}) {
656 t.Error("deletion unexpectedly succeeded")
657 }
658 if c2.DeleteLabelValues("2") {
659 t.Error("deletion unexpectedly succeeded")
660 }
661 if !c1.Delete(Labels{"two": "2"}) {
662 t.Error("deletion failed")
663 }
664 if !c2.DeleteLabelValues("22") {
665 t.Error("deletion failed")
666 }
667 assertNoMetric(t)
668 })
669 t.Run("use already curried label", func(t *testing.T) {
670 c1 := vec.MustCurryWith(Labels{"three": "3"})
671 if _, err := c1.GetMetricWithLabelValues("1", "2", "3"); err == nil {
672 t.Error("expected error when using already curried label")
673 }
674 if _, err := c1.GetMetricWith(Labels{"one": "1", "two": "2", "three": "3"}); err == nil {
675 t.Error("expected error when using already curried label")
676 }
677 assertNoMetric(t)
678 c1.WithLabelValues("1", "2").Inc()
679 if c1.Delete(Labels{"one": "1", "two": "2", "three": "3"}) {
680 t.Error("deletion unexpectedly succeeded")
681 }
682 if !c1.Delete(Labels{"one": "1", "two": "2"}) {
683 t.Error("deletion failed")
684 }
685 assertNoMetric(t)
686 })
687 t.Run("curry already curried label", func(t *testing.T) {
688 if _, err := vec.MustCurryWith(Labels{"three": "3"}).CurryWith(Labels{"three": "33"}); err == nil {
689 t.Error("currying unexpectedly succeeded")
690 } else if err.Error() != `label name "three" is already curried` {
691 t.Error("currying returned unexpected error:", err)
692 }
693 })
694 t.Run("unknown label", func(t *testing.T) {
695 if _, err := vec.CurryWith(Labels{"foo": "bar"}); err == nil {
696 t.Error("currying unexpectedly succeeded")
697 } else if err.Error() != "1 unknown label(s) found during currying" {
698 t.Error("currying returned unexpected error:", err)
699 }
700 })
701 }
702
703 func testConstrainedCurryVec(t *testing.T, vec *CounterVec, constraint func(string) string) {
704 assertMetrics := func(t *testing.T) {
705 n := 0
706 for _, m := range vec.metrics {
707 n += len(m)
708 }
709 if n != 2 {
710 t.Error("expected two metrics, got", n)
711 }
712 m := &dto.Metric{}
713 c1, err := vec.GetMetricWithLabelValues("1", "2", "3")
714 if err != nil {
715 t.Fatal("unexpected error getting metric:", err)
716 }
717 c1.Write(m)
718 if want, got := 1., m.GetCounter().GetValue(); want != got {
719 t.Errorf("want %f as counter value, got %f", want, got)
720 }
721 values := map[string]string{}
722 for _, label := range m.Label {
723 values[*label.Name] = *label.Value
724 }
725 if want, got := map[string]string{"one": "1", "two": "2", "three": constraint("3")}, values; !reflect.DeepEqual(want, got) {
726 t.Errorf("want %v as label values, got %v", want, got)
727 }
728 m.Reset()
729 c2, err := vec.GetMetricWithLabelValues("11", "22", "33")
730 if err != nil {
731 t.Fatal("unexpected error getting metric:", err)
732 }
733 c2.Write(m)
734 if want, got := 1., m.GetCounter().GetValue(); want != got {
735 t.Errorf("want %f as counter value, got %f", want, got)
736 }
737 values = map[string]string{}
738 for _, label := range m.Label {
739 values[*label.Name] = *label.Value
740 }
741 if want, got := map[string]string{"one": "11", "two": "22", "three": constraint("33")}, values; !reflect.DeepEqual(want, got) {
742 t.Errorf("want %v as label values, got %v", want, got)
743 }
744 }
745
746 assertNoMetric := func(t *testing.T) {
747 if n := len(vec.metrics); n != 0 {
748 t.Error("expected no metrics, got", n)
749 }
750 }
751
752 t.Run("zero labels", func(t *testing.T) {
753 c1 := vec.MustCurryWith(nil)
754 c2 := vec.MustCurryWith(nil)
755 c1.WithLabelValues("1", "2", "3").Inc()
756 c2.With(Labels{"one": "11", "two": "22", "three": "33"}).Inc()
757 assertMetrics(t)
758 if !c1.Delete(Labels{"one": "1", "two": "2", "three": "3"}) {
759 t.Error("deletion failed")
760 }
761 if !c2.DeleteLabelValues("11", "22", "33") {
762 t.Error("deletion failed")
763 }
764 assertNoMetric(t)
765 })
766 t.Run("first label", func(t *testing.T) {
767 c1 := vec.MustCurryWith(Labels{"one": "1"})
768 c2 := vec.MustCurryWith(Labels{"one": "11"})
769 c1.WithLabelValues("2", "3").Inc()
770 c2.With(Labels{"two": "22", "three": "33"}).Inc()
771 assertMetrics(t)
772 if c1.Delete(Labels{"two": "22", "three": "33"}) {
773 t.Error("deletion unexpectedly succeeded")
774 }
775 if c2.DeleteLabelValues("2", "3") {
776 t.Error("deletion unexpectedly succeeded")
777 }
778 if !c1.Delete(Labels{"two": "2", "three": "3"}) {
779 t.Error("deletion failed")
780 }
781 if !c2.DeleteLabelValues("22", "33") {
782 t.Error("deletion failed")
783 }
784 assertNoMetric(t)
785 })
786 t.Run("middle label", func(t *testing.T) {
787 c1 := vec.MustCurryWith(Labels{"two": "2"})
788 c2 := vec.MustCurryWith(Labels{"two": "22"})
789 c1.WithLabelValues("1", "3").Inc()
790 c2.With(Labels{"one": "11", "three": "33"}).Inc()
791 assertMetrics(t)
792 if c1.Delete(Labels{"one": "11", "three": "33"}) {
793 t.Error("deletion unexpectedly succeeded")
794 }
795 if c2.DeleteLabelValues("1", "3") {
796 t.Error("deletion unexpectedly succeeded")
797 }
798 if !c1.Delete(Labels{"one": "1", "three": "3"}) {
799 t.Error("deletion failed")
800 }
801 if !c2.DeleteLabelValues("11", "33") {
802 t.Error("deletion failed")
803 }
804 assertNoMetric(t)
805 })
806 t.Run("last label (constrained to static value)", func(t *testing.T) {
807 c1 := vec.MustCurryWith(Labels{"three": "3"})
808 c2 := vec.MustCurryWith(Labels{"three": "33"})
809 c1.WithLabelValues("1", "2").Inc()
810 c2.With(Labels{"one": "11", "two": "22"}).Inc()
811 assertMetrics(t)
812 if !c1.Delete(Labels{"two": "22", "one": "11"}) {
813 t.Error("deletion failed")
814 }
815 if !c2.DeleteLabelValues("1", "2") {
816 t.Error("deletion failed")
817 }
818 assertNoMetric(t)
819 })
820 t.Run("two labels", func(t *testing.T) {
821 c1 := vec.MustCurryWith(Labels{"three": "3", "one": "1"})
822 c2 := vec.MustCurryWith(Labels{"three": "33", "one": "11"})
823 c1.WithLabelValues("2").Inc()
824 c2.With(Labels{"two": "22"}).Inc()
825 assertMetrics(t)
826 if c1.Delete(Labels{"two": "22"}) {
827 t.Error("deletion unexpectedly succeeded")
828 }
829 if c2.DeleteLabelValues("2") {
830 t.Error("deletion unexpectedly succeeded")
831 }
832 if !c1.Delete(Labels{"two": "2"}) {
833 t.Error("deletion failed")
834 }
835 if !c2.DeleteLabelValues("22") {
836 t.Error("deletion failed")
837 }
838 assertNoMetric(t)
839 })
840 t.Run("all labels", func(t *testing.T) {
841 c1 := vec.MustCurryWith(Labels{"three": "3", "two": "2", "one": "1"})
842 c2 := vec.MustCurryWith(Labels{"three": "33", "one": "11", "two": "22"})
843 c1.WithLabelValues().Inc()
844 c2.With(nil).Inc()
845 assertMetrics(t)
846 if !c1.Delete(Labels{}) {
847 t.Error("deletion failed")
848 }
849 if !c2.DeleteLabelValues() {
850 t.Error("deletion failed")
851 }
852 assertNoMetric(t)
853 })
854 t.Run("double curry", func(t *testing.T) {
855 c1 := vec.MustCurryWith(Labels{"three": "3"}).MustCurryWith(Labels{"one": "1"})
856 c2 := vec.MustCurryWith(Labels{"three": "33"}).MustCurryWith(Labels{"one": "11"})
857 c1.WithLabelValues("2").Inc()
858 c2.With(Labels{"two": "22"}).Inc()
859 assertMetrics(t)
860 if c1.Delete(Labels{"two": "22"}) {
861 t.Error("deletion unexpectedly succeeded")
862 }
863 if c2.DeleteLabelValues("2") {
864 t.Error("deletion unexpectedly succeeded")
865 }
866 if !c1.Delete(Labels{"two": "2"}) {
867 t.Error("deletion failed")
868 }
869 if !c2.DeleteLabelValues("22") {
870 t.Error("deletion failed")
871 }
872 assertNoMetric(t)
873 })
874 t.Run("use already curried label", func(t *testing.T) {
875 c1 := vec.MustCurryWith(Labels{"three": "3"})
876 if _, err := c1.GetMetricWithLabelValues("1", "2", "3"); err == nil {
877 t.Error("expected error when using already curried label")
878 }
879 if _, err := c1.GetMetricWith(Labels{"one": "1", "two": "2", "three": "3"}); err == nil {
880 t.Error("expected error when using already curried label")
881 }
882 assertNoMetric(t)
883 c1.WithLabelValues("1", "2").Inc()
884 if c1.Delete(Labels{"one": "1", "two": "2", "three": "3"}) {
885 t.Error("deletion unexpectedly succeeded")
886 }
887 if !c1.Delete(Labels{"one": "1", "two": "2"}) {
888 t.Error("deletion failed")
889 }
890 assertNoMetric(t)
891 })
892 t.Run("curry already curried label", func(t *testing.T) {
893 if _, err := vec.MustCurryWith(Labels{"three": "3"}).CurryWith(Labels{"three": "33"}); err == nil {
894 t.Error("currying unexpectedly succeeded")
895 } else if err.Error() != `label name "three" is already curried` {
896 t.Error("currying returned unexpected error:", err)
897 }
898 })
899 t.Run("unknown label", func(t *testing.T) {
900 if _, err := vec.CurryWith(Labels{"foo": "bar"}); err == nil {
901 t.Error("currying unexpectedly succeeded")
902 } else if err.Error() != "1 unknown label(s) found during currying" {
903 t.Error("currying returned unexpected error:", err)
904 }
905 })
906 }
907
908 func BenchmarkMetricVecWithBasic(b *testing.B) {
909 benchmarkMetricVecWith(b, Labels{
910 "l1": "onevalue",
911 "l2": "twovalue",
912 })
913 }
914
915 func BenchmarkMetricVecWithLabelValuesBasic(b *testing.B) {
916 benchmarkMetricVecWithLabelValues(b, map[string][]string{
917 "l1": {"onevalue"},
918 "l2": {"twovalue"},
919 })
920 }
921
922 func BenchmarkMetricVecWithLabelValues2Keys10ValueCardinality(b *testing.B) {
923 benchmarkMetricVecWithLabelValuesCardinality(b, 2, 10)
924 }
925
926 func BenchmarkMetricVecWithLabelValues4Keys10ValueCardinality(b *testing.B) {
927 benchmarkMetricVecWithLabelValuesCardinality(b, 4, 10)
928 }
929
930 func BenchmarkMetricVecWithLabelValues2Keys100ValueCardinality(b *testing.B) {
931 benchmarkMetricVecWithLabelValuesCardinality(b, 2, 100)
932 }
933
934 func BenchmarkMetricVecWithLabelValues10Keys100ValueCardinality(b *testing.B) {
935 benchmarkMetricVecWithLabelValuesCardinality(b, 10, 100)
936 }
937
938 func BenchmarkMetricVecWithLabelValues10Keys1000ValueCardinality(b *testing.B) {
939 benchmarkMetricVecWithLabelValuesCardinality(b, 10, 1000)
940 }
941
942 func benchmarkMetricVecWithLabelValuesCardinality(b *testing.B, nkeys, nvalues int) {
943 labels := map[string][]string{}
944
945 for i := 0; i < nkeys; i++ {
946 var (
947 k = fmt.Sprintf("key-%v", i)
948 vs = make([]string, 0, nvalues)
949 )
950 for j := 0; j < nvalues; j++ {
951 vs = append(vs, fmt.Sprintf("value-%v", j))
952 }
953 labels[k] = vs
954 }
955
956 benchmarkMetricVecWithLabelValues(b, labels)
957 }
958
959 func benchmarkMetricVecWith(b *testing.B, labels map[string]string) {
960 var keys []string
961 for k := range labels {
962 keys = append(keys, k)
963 }
964
965 vec := NewGaugeVec(
966 GaugeOpts{
967 Name: "test",
968 Help: "helpless",
969 },
970 keys,
971 )
972
973 b.ReportAllocs()
974 b.ResetTimer()
975 for i := 0; i < b.N; i++ {
976 vec.With(labels)
977 }
978 }
979
980 func benchmarkMetricVecWithLabelValues(b *testing.B, labels map[string][]string) {
981 var keys []string
982 for k := range labels { // Map order dependent, who cares though.
983 keys = append(keys, k)
984 }
985
986 values := make([]string, len(labels)) // Value cache for permutations.
987 vec := NewGaugeVec(
988 GaugeOpts{
989 Name: "test",
990 Help: "helpless",
991 },
992 keys,
993 )
994
995 b.ReportAllocs()
996 b.ResetTimer()
997 for i := 0; i < b.N; i++ {
998 // Varies input across provide map entries based on key size.
999 for j, k := range keys {
1000 candidates := labels[k]
1001 values[j] = candidates[i%len(candidates)]
1002 }
1003
1004 vec.WithLabelValues(values...)
1005 }
1006 }