28
28
class TestCounter (unittest .TestCase ):
29
29
def setUp (self ):
30
30
self .registry = CollectorRegistry ()
31
- self .counter = Counter ('c ' , 'help' , registry = self .registry )
31
+ self .counter = Counter ('c_total ' , 'help' , registry = self .registry )
32
32
33
33
def test_increment (self ):
34
- self .assertEqual (0 , self .registry .get_sample_value ('c ' ))
34
+ self .assertEqual (0 , self .registry .get_sample_value ('c_total ' ))
35
35
self .counter .inc ()
36
- self .assertEqual (1 , self .registry .get_sample_value ('c ' ))
36
+ self .assertEqual (1 , self .registry .get_sample_value ('c_total ' ))
37
37
self .counter .inc (7 )
38
- self .assertEqual (8 , self .registry .get_sample_value ('c ' ))
38
+ self .assertEqual (8 , self .registry .get_sample_value ('c_total ' ))
39
39
40
40
def test_negative_increment_raises (self ):
41
41
self .assertRaises (ValueError , self .counter .inc , - 1 )
@@ -54,18 +54,18 @@ def f(r):
54
54
f (False )
55
55
except TypeError :
56
56
pass
57
- self .assertEqual (0 , self .registry .get_sample_value ('c ' ))
57
+ self .assertEqual (0 , self .registry .get_sample_value ('c_total ' ))
58
58
59
59
try :
60
60
f (True )
61
61
except ValueError :
62
62
pass
63
- self .assertEqual (1 , self .registry .get_sample_value ('c ' ))
63
+ self .assertEqual (1 , self .registry .get_sample_value ('c_total ' ))
64
64
65
65
def test_block_decorator (self ):
66
66
with self .counter .count_exceptions ():
67
67
pass
68
- self .assertEqual (0 , self .registry .get_sample_value ('c ' ))
68
+ self .assertEqual (0 , self .registry .get_sample_value ('c_total ' ))
69
69
70
70
raised = False
71
71
try :
@@ -74,7 +74,7 @@ def test_block_decorator(self):
74
74
except :
75
75
raised = True
76
76
self .assertTrue (raised )
77
- self .assertEqual (1 , self .registry .get_sample_value ('c ' ))
77
+ self .assertEqual (1 , self .registry .get_sample_value ('c_total ' ))
78
78
79
79
80
80
class TestGauge (unittest .TestCase ):
@@ -342,23 +342,23 @@ def test_block_decorator(self):
342
342
class TestMetricWrapper (unittest .TestCase ):
343
343
def setUp (self ):
344
344
self .registry = CollectorRegistry ()
345
- self .counter = Counter ('c ' , 'help' , labelnames = ['l' ], registry = self .registry )
345
+ self .counter = Counter ('c_total ' , 'help' , labelnames = ['l' ], registry = self .registry )
346
346
self .two_labels = Counter ('two' , 'help' , labelnames = ['a' , 'b' ], registry = self .registry )
347
347
348
348
def test_child (self ):
349
349
self .counter .labels ('x' ).inc ()
350
- self .assertEqual (1 , self .registry .get_sample_value ('c ' , {'l' : 'x' }))
350
+ self .assertEqual (1 , self .registry .get_sample_value ('c_total ' , {'l' : 'x' }))
351
351
self .two_labels .labels ('x' , 'y' ).inc (2 )
352
- self .assertEqual (2 , self .registry .get_sample_value ('two ' , {'a' : 'x' , 'b' : 'y' }))
352
+ self .assertEqual (2 , self .registry .get_sample_value ('two_total ' , {'a' : 'x' , 'b' : 'y' }))
353
353
354
354
def test_remove (self ):
355
355
self .counter .labels ('x' ).inc ()
356
356
self .counter .labels ('y' ).inc (2 )
357
- self .assertEqual (1 , self .registry .get_sample_value ('c ' , {'l' : 'x' }))
358
- self .assertEqual (2 , self .registry .get_sample_value ('c ' , {'l' : 'y' }))
357
+ self .assertEqual (1 , self .registry .get_sample_value ('c_total ' , {'l' : 'x' }))
358
+ self .assertEqual (2 , self .registry .get_sample_value ('c_total ' , {'l' : 'y' }))
359
359
self .counter .remove ('x' )
360
- self .assertEqual (None , self .registry .get_sample_value ('c ' , {'l' : 'x' }))
361
- self .assertEqual (2 , self .registry .get_sample_value ('c ' , {'l' : 'y' }))
360
+ self .assertEqual (None , self .registry .get_sample_value ('c_total ' , {'l' : 'x' }))
361
+ self .assertEqual (2 , self .registry .get_sample_value ('c_total ' , {'l' : 'y' }))
362
362
363
363
def test_incorrect_label_count_raises (self ):
364
364
self .assertRaises (ValueError , self .counter .labels )
@@ -369,10 +369,10 @@ def test_incorrect_label_count_raises(self):
369
369
def test_labels_coerced_to_string (self ):
370
370
self .counter .labels (None ).inc ()
371
371
self .counter .labels (l = None ).inc ()
372
- self .assertEqual (2 , self .registry .get_sample_value ('c ' , {'l' : 'None' }))
372
+ self .assertEqual (2 , self .registry .get_sample_value ('c_total ' , {'l' : 'None' }))
373
373
374
374
self .counter .remove (None )
375
- self .assertEqual (None , self .registry .get_sample_value ('c ' , {'l' : 'None' }))
375
+ self .assertEqual (None , self .registry .get_sample_value ('c_total ' , {'l' : 'None' }))
376
376
377
377
def test_non_string_labels_raises (self ):
378
378
class Test (object ):
@@ -381,18 +381,18 @@ class Test(object):
381
381
self .assertRaises (TypeError , self .counter .labels , l = Test ())
382
382
383
383
def test_namespace_subsystem_concatenated (self ):
384
- c = Counter ('c ' , 'help' , namespace = 'a' , subsystem = 'b' , registry = self .registry )
384
+ c = Counter ('c_total ' , 'help' , namespace = 'a' , subsystem = 'b' , registry = self .registry )
385
385
c .inc ()
386
- self .assertEqual (1 , self .registry .get_sample_value ('a_b_c ' ))
386
+ self .assertEqual (1 , self .registry .get_sample_value ('a_b_c_total ' ))
387
387
388
388
def test_labels_by_kwarg (self ):
389
389
self .counter .labels (l = 'x' ).inc ()
390
- self .assertEqual (1 , self .registry .get_sample_value ('c ' , {'l' : 'x' }))
390
+ self .assertEqual (1 , self .registry .get_sample_value ('c_total ' , {'l' : 'x' }))
391
391
self .assertRaises (ValueError , self .counter .labels , l = 'x' , m = 'y' )
392
392
self .assertRaises (ValueError , self .counter .labels , m = 'y' )
393
393
self .assertRaises (ValueError , self .counter .labels )
394
394
self .two_labels .labels (a = 'x' , b = 'y' ).inc ()
395
- self .assertEqual (1 , self .registry .get_sample_value ('two ' , {'a' : 'x' , 'b' : 'y' }))
395
+ self .assertEqual (1 , self .registry .get_sample_value ('two_total ' , {'a' : 'x' , 'b' : 'y' }))
396
396
self .assertRaises (ValueError , self .two_labels .labels , a = 'x' , b = 'y' , c = 'z' )
397
397
self .assertRaises (ValueError , self .two_labels .labels , a = 'x' , c = 'z' )
398
398
self .assertRaises (ValueError , self .two_labels .labels , b = 'y' , c = 'z' )
@@ -405,10 +405,10 @@ def test_invalid_names_raise(self):
405
405
self .assertRaises (ValueError , Counter , '^' , 'help' )
406
406
self .assertRaises (ValueError , Counter , '' , 'help' , namespace = '&' )
407
407
self .assertRaises (ValueError , Counter , '' , 'help' , subsystem = '(' )
408
- self .assertRaises (ValueError , Counter , 'c ' , '' , labelnames = ['^' ])
409
- self .assertRaises (ValueError , Counter , 'c ' , '' , labelnames = ['a:b' ])
410
- self .assertRaises (ValueError , Counter , 'c ' , '' , labelnames = ['__reserved' ])
411
- self .assertRaises (ValueError , Summary , 'c ' , '' , labelnames = ['quantile' ])
408
+ self .assertRaises (ValueError , Counter , 'c_total ' , '' , labelnames = ['^' ])
409
+ self .assertRaises (ValueError , Counter , 'c_total ' , '' , labelnames = ['a:b' ])
410
+ self .assertRaises (ValueError , Counter , 'c_total ' , '' , labelnames = ['__reserved' ])
411
+ self .assertRaises (ValueError , Summary , 'c_total ' , '' , labelnames = ['quantile' ])
412
412
413
413
def test_empty_labels_list (self ):
414
414
Histogram ('h' , 'help' , [], registry = self .registry )
@@ -439,14 +439,18 @@ def test_untyped_labels(self):
439
439
self .assertEqual (2 , self .registry .get_sample_value ('u' , {'a' : 'b' , 'c' : 'd' }))
440
440
441
441
def test_counter (self ):
442
- self .custom_collector (CounterMetricFamily ('c' , 'help' , value = 1 ))
443
- self .assertEqual (1 , self .registry .get_sample_value ('c' , {}))
442
+ self .custom_collector (CounterMetricFamily ('c_total' , 'help' , value = 1 ))
443
+ self .assertEqual (1 , self .registry .get_sample_value ('c_total' , {}))
444
+
445
+ def test_counter_total (self ):
446
+ self .custom_collector (CounterMetricFamily ('c_total' , 'help' , value = 1 ))
447
+ self .assertEqual (1 , self .registry .get_sample_value ('c_total' , {}))
444
448
445
449
def test_counter_labels (self ):
446
- cmf = CounterMetricFamily ('c ' , 'help' , labels = ['a' , 'c ' ])
450
+ cmf = CounterMetricFamily ('c_total ' , 'help' , labels = ['a' , 'c_total ' ])
447
451
cmf .add_metric (['b' , 'd' ], 2 )
448
452
self .custom_collector (cmf )
449
- self .assertEqual (2 , self .registry .get_sample_value ('c ' , {'a' : 'b' , 'c ' : 'd' }))
453
+ self .assertEqual (2 , self .registry .get_sample_value ('c_total ' , {'a' : 'b' , 'c_total ' : 'd' }))
450
454
451
455
def test_gauge (self ):
452
456
self .custom_collector (GaugeMetricFamily ('g' , 'help' , value = 1 ))
@@ -490,8 +494,8 @@ def test_bad_constructors(self):
490
494
self .assertRaises (ValueError , UntypedMetricFamily , 'u' , 'help' , value = 1 , labels = [])
491
495
self .assertRaises (ValueError , UntypedMetricFamily , 'u' , 'help' , value = 1 , labels = ['a' ])
492
496
493
- self .assertRaises (ValueError , CounterMetricFamily , 'c ' , 'help' , value = 1 , labels = [])
494
- self .assertRaises (ValueError , CounterMetricFamily , 'c ' , 'help' , value = 1 , labels = ['a' ])
497
+ self .assertRaises (ValueError , CounterMetricFamily , 'c_total ' , 'help' , value = 1 , labels = [])
498
+ self .assertRaises (ValueError , CounterMetricFamily , 'c_total ' , 'help' , value = 1 , labels = ['a' ])
495
499
496
500
self .assertRaises (ValueError , GaugeMetricFamily , 'g' , 'help' , value = 1 , labels = [])
497
501
self .assertRaises (ValueError , GaugeMetricFamily , 'g' , 'help' , value = 1 , labels = ['a' ])
@@ -512,7 +516,7 @@ def test_bad_constructors(self):
512
516
def test_labelnames (self ):
513
517
cmf = UntypedMetricFamily ('u' , 'help' , labels = iter (['a' ]))
514
518
self .assertEqual (('a' ,), cmf ._labelnames )
515
- cmf = CounterMetricFamily ('c ' , 'help' , labels = iter (['a' ]))
519
+ cmf = CounterMetricFamily ('c_total ' , 'help' , labels = iter (['a' ]))
516
520
self .assertEqual (('a' ,), cmf ._labelnames )
517
521
gmf = GaugeMetricFamily ('g' , 'help' , labels = iter (['a' ]))
518
522
self .assertEqual (('a' ,), gmf ._labelnames )
@@ -525,16 +529,20 @@ def test_labelnames(self):
525
529
class TestCollectorRegistry (unittest .TestCase ):
526
530
def test_duplicate_metrics_raises (self ):
527
531
registry = CollectorRegistry ()
528
- Counter ('c' , 'help' , registry = registry )
529
- self .assertRaises (ValueError , Counter , 'c' , 'help' , registry = registry )
530
- self .assertRaises (ValueError , Gauge , 'c' , 'help' , registry = registry )
532
+ Counter ('c_total' , 'help' , registry = registry )
533
+ self .assertRaises (ValueError , Counter , 'c_total' , 'help' , registry = registry )
534
+ self .assertRaises (ValueError , Gauge , 'c_total' , 'help' , registry = registry )
535
+ self .assertRaises (ValueError , Gauge , 'c_created' , 'help' , registry = registry )
531
536
532
- Gauge ('g ' , 'help' , registry = registry )
533
- self .assertRaises (ValueError , Gauge , 'g ' , 'help' , registry = registry )
537
+ Gauge ('g_created ' , 'help' , registry = registry )
538
+ self .assertRaises (ValueError , Gauge , 'g_created ' , 'help' , registry = registry )
534
539
self .assertRaises (ValueError , Counter , 'g' , 'help' , registry = registry )
535
540
536
541
Summary ('s' , 'help' , registry = registry )
537
542
self .assertRaises (ValueError , Summary , 's' , 'help' , registry = registry )
543
+ self .assertRaises (ValueError , Gauge , 's_created' , 'help' , registry = registry )
544
+ self .assertRaises (ValueError , Gauge , 's_sum' , 'help' , registry = registry )
545
+ self .assertRaises (ValueError , Gauge , 's_count' , 'help' , registry = registry )
538
546
# We don't currently expose quantiles, but let's prevent future
539
547
# clashes anyway.
540
548
self .assertRaises (ValueError , Gauge , 's' , 'help' , registry = registry )
@@ -543,18 +551,19 @@ def test_duplicate_metrics_raises(self):
543
551
self .assertRaises (ValueError , Histogram , 'h' , 'help' , registry = registry )
544
552
# Clashes aggaint various suffixes.
545
553
self .assertRaises (ValueError , Summary , 'h' , 'help' , registry = registry )
546
- self .assertRaises (ValueError , Counter , 'h_count' , 'help' , registry = registry )
547
- self .assertRaises (ValueError , Counter , 'h_sum' , 'help' , registry = registry )
548
- self .assertRaises (ValueError , Counter , 'h_bucket' , 'help' , registry = registry )
554
+ self .assertRaises (ValueError , Gauge , 'h_count' , 'help' , registry = registry )
555
+ self .assertRaises (ValueError , Gauge , 'h_sum' , 'help' , registry = registry )
556
+ self .assertRaises (ValueError , Gauge , 'h_bucket' , 'help' , registry = registry )
557
+ self .assertRaises (ValueError , Gauge , 'h_created' , 'help' , registry = registry )
549
558
# The name of the histogram itself isn't taken.
550
- Counter ('h' , 'help' , registry = registry )
559
+ Gauge ('h' , 'help' , registry = registry )
551
560
552
561
def test_unregister_works (self ):
553
562
registry = CollectorRegistry ()
554
563
s = Summary ('s' , 'help' , registry = registry )
555
- self .assertRaises (ValueError , Counter , 's_count' , 'help' , registry = registry )
564
+ self .assertRaises (ValueError , Gauge , 's_count' , 'help' , registry = registry )
556
565
registry .unregister (s )
557
- Counter ('s_count' , 'help' , registry = registry )
566
+ Gauge ('s_count' , 'help' , registry = registry )
558
567
559
568
def custom_collector (self , metric_family , registry ):
560
569
class CustomCollector (object ):
@@ -564,16 +573,16 @@ def collect(self):
564
573
565
574
def test_autodescribe_disabled_by_default (self ):
566
575
registry = CollectorRegistry ()
567
- self .custom_collector (CounterMetricFamily ('c ' , 'help' , value = 1 ), registry )
568
- self .custom_collector (CounterMetricFamily ('c ' , 'help' , value = 1 ), registry )
576
+ self .custom_collector (CounterMetricFamily ('c_total ' , 'help' , value = 1 ), registry )
577
+ self .custom_collector (CounterMetricFamily ('c_total ' , 'help' , value = 1 ), registry )
569
578
570
579
registry = CollectorRegistry (auto_describe = True )
571
- self .custom_collector (CounterMetricFamily ('c ' , 'help' , value = 1 ), registry )
572
- self .assertRaises (ValueError , self .custom_collector , CounterMetricFamily ('c ' , 'help' , value = 1 ), registry )
580
+ self .custom_collector (CounterMetricFamily ('c_total ' , 'help' , value = 1 ), registry )
581
+ self .assertRaises (ValueError , self .custom_collector , CounterMetricFamily ('c_total ' , 'help' , value = 1 ), registry )
573
582
574
583
def test_restricted_registry (self ):
575
584
registry = CollectorRegistry ()
576
- Counter ('c ' , 'help' , registry = registry )
585
+ Counter ('c_total ' , 'help' , registry = registry )
577
586
Summary ('s' , 'help' , registry = registry ).observe (7 )
578
587
579
588
m = Metric ('s' , 'help' , 'summary' )
0 commit comments