2
2
import time
3
3
import types
4
4
from typing import (
5
- Any , Callable , Dict , Iterable , Optional , Sequence , Type , TypeVar ,
5
+ Any , Callable , Dict , Iterable , Optional , Sequence , Type , TypeVar , Union ,
6
6
)
7
7
8
8
from . import values # retain this import style for testability
@@ -337,15 +337,15 @@ def f():
337
337
_MULTIPROC_MODES = frozenset (('min' , 'max' , 'livesum' , 'liveall' , 'all' ))
338
338
339
339
def __init__ (self ,
340
- name ,
341
- documentation ,
342
- labelnames = (),
343
- namespace = '' ,
344
- subsystem = '' ,
345
- unit = '' ,
346
- registry = REGISTRY ,
347
- _labelvalues = None ,
348
- multiprocess_mode = 'all' ,
340
+ name : str ,
341
+ documentation : str ,
342
+ labelnames : Iterable [ str ] = (),
343
+ namespace : str = '' ,
344
+ subsystem : str = '' ,
345
+ unit : str = '' ,
346
+ registry : Optional [ CollectorRegistry ] = REGISTRY ,
347
+ _labelvalues : Optional [ Sequence [ str ]] = None ,
348
+ multiprocess_mode : str = 'all' ,
349
349
):
350
350
self ._multiprocess_mode = multiprocess_mode
351
351
if multiprocess_mode not in self ._MULTIPROC_MODES :
@@ -362,32 +362,32 @@ def __init__(self,
362
362
)
363
363
self ._kwargs ['multiprocess_mode' ] = self ._multiprocess_mode
364
364
365
- def _metric_init (self ):
365
+ def _metric_init (self ) -> None :
366
366
self ._value = values .ValueClass (
367
367
self ._type , self ._name , self ._name , self ._labelnames , self ._labelvalues ,
368
368
multiprocess_mode = self ._multiprocess_mode
369
369
)
370
370
371
- def inc (self , amount = 1 ) :
371
+ def inc (self , amount : float = 1 ) -> None :
372
372
"""Increment gauge by the given amount."""
373
373
self ._raise_if_not_observable ()
374
374
self ._value .inc (amount )
375
375
376
- def dec (self , amount = 1 ) :
376
+ def dec (self , amount : float = 1 ) -> None :
377
377
"""Decrement gauge by the given amount."""
378
378
self ._raise_if_not_observable ()
379
379
self ._value .inc (- amount )
380
380
381
- def set (self , value ) :
381
+ def set (self , value : float ) -> None :
382
382
"""Set gauge to the given value."""
383
383
self ._raise_if_not_observable ()
384
384
self ._value .set (float (value ))
385
385
386
- def set_to_current_time (self ):
386
+ def set_to_current_time (self ) -> None :
387
387
"""Set gauge to the current unixtime."""
388
388
self .set (time .time ())
389
389
390
- def track_inprogress (self ):
390
+ def track_inprogress (self ) -> InprogressTracker :
391
391
"""Track inprogress blocks of code or functions.
392
392
393
393
Can be used as a function decorator or context manager.
@@ -397,14 +397,14 @@ def track_inprogress(self):
397
397
self ._raise_if_not_observable ()
398
398
return InprogressTracker (self )
399
399
400
- def time (self ):
400
+ def time (self ) -> Timer :
401
401
"""Time a block of code or function, and set the duration in seconds.
402
402
403
403
Can be used as a function decorator or context manager.
404
404
"""
405
405
return Timer (self , 'set' )
406
406
407
- def set_function (self , f ) :
407
+ def set_function (self , f : Callable [[], float ]) -> None :
408
408
"""Call the provided function to return the Gauge value.
409
409
410
410
The function must return a float, and may be called from
@@ -413,10 +413,10 @@ def set_function(self, f):
413
413
414
414
self ._raise_if_not_observable ()
415
415
416
- def samples (self ) -> Iterable [Sample ]:
416
+ def samples (_ : Gauge ) -> Iterable [Sample ]:
417
417
return (Sample ('' , {}, float (f ()), None , None ),)
418
418
419
- self ._child_samples = types .MethodType (samples , self )
419
+ self ._child_samples = types .MethodType (samples , self ) # type: ignore
420
420
421
421
def _child_samples (self ) -> Iterable [Sample ]:
422
422
return (Sample ('' , {}, self ._value .get (), None , None ),)
@@ -455,13 +455,13 @@ def create_response(request):
455
455
_type = 'summary'
456
456
_reserved_labelnames = ['quantile' ]
457
457
458
- def _metric_init (self ):
458
+ def _metric_init (self ) -> None :
459
459
self ._count = values .ValueClass (self ._type , self ._name , self ._name + '_count' , self ._labelnames ,
460
460
self ._labelvalues )
461
461
self ._sum = values .ValueClass (self ._type , self ._name , self ._name + '_sum' , self ._labelnames , self ._labelvalues )
462
462
self ._created = time .time ()
463
463
464
- def observe (self , amount ) :
464
+ def observe (self , amount : float ) -> None :
465
465
"""Observe the given amount.
466
466
467
467
The amount is usually positive or zero. Negative values are
@@ -475,7 +475,7 @@ def observe(self, amount):
475
475
self ._count .inc (1 )
476
476
self ._sum .inc (amount )
477
477
478
- def time (self ):
478
+ def time (self ) -> Timer :
479
479
"""Time a block of code or function, and observe the duration in seconds.
480
480
481
481
Can be used as a function decorator or context manager.
@@ -530,15 +530,15 @@ def create_response(request):
530
530
DEFAULT_BUCKETS = (.005 , .01 , .025 , .05 , .075 , .1 , .25 , .5 , .75 , 1.0 , 2.5 , 5.0 , 7.5 , 10.0 , INF )
531
531
532
532
def __init__ (self ,
533
- name ,
534
- documentation ,
535
- labelnames = (),
536
- namespace = '' ,
537
- subsystem = '' ,
538
- unit = '' ,
539
- registry = REGISTRY ,
540
- _labelvalues = None ,
541
- buckets = DEFAULT_BUCKETS ,
533
+ name : str ,
534
+ documentation : str ,
535
+ labelnames : Iterable [ str ] = (),
536
+ namespace : str = '' ,
537
+ subsystem : str = '' ,
538
+ unit : str = '' ,
539
+ registry : Optional [ CollectorRegistry ] = REGISTRY ,
540
+ _labelvalues : Optional [ Sequence [ str ]] = None ,
541
+ buckets : Sequence [ Union [ float , int , str ]] = DEFAULT_BUCKETS ,
542
542
):
543
543
self ._prepare_buckets (buckets )
544
544
super ().__init__ (
@@ -553,8 +553,8 @@ def __init__(self,
553
553
)
554
554
self ._kwargs ['buckets' ] = buckets
555
555
556
- def _prepare_buckets (self , buckets ) :
557
- buckets = [float (b ) for b in buckets ]
556
+ def _prepare_buckets (self , source_buckets : Sequence [ Union [ float , int , str ]]) -> None :
557
+ buckets = [float (b ) for b in source_buckets ]
558
558
if buckets != sorted (buckets ):
559
559
# This is probably an error on the part of the user,
560
560
# so raise rather than sorting for them.
@@ -565,7 +565,7 @@ def _prepare_buckets(self, buckets):
565
565
raise ValueError ('Must have at least two buckets' )
566
566
self ._upper_bounds = buckets
567
567
568
- def _metric_init (self ):
568
+ def _metric_init (self ) -> None :
569
569
self ._buckets = []
570
570
self ._created = time .time ()
571
571
bucket_labelnames = self ._labelnames + ('le' ,)
@@ -579,7 +579,7 @@ def _metric_init(self):
579
579
self ._labelvalues + (floatToGoString (b ),))
580
580
)
581
581
582
- def observe (self , amount , exemplar = None ):
582
+ def observe (self , amount : float , exemplar : Optional [ Dict [ str , str ]] = None ) -> None :
583
583
"""Observe the given amount.
584
584
585
585
The amount is usually positive or zero. Negative values are
@@ -599,7 +599,7 @@ def observe(self, amount, exemplar=None):
599
599
self ._buckets [i ].set_exemplar (Exemplar (exemplar , amount , time .time ()))
600
600
break
601
601
602
- def time (self ):
602
+ def time (self ) -> Timer :
603
603
"""Time a block of code or function, and observe the duration in seconds.
604
604
605
605
Can be used as a function decorator or context manager.
0 commit comments