1
1
from threading import Lock
2
2
import time
3
3
import types
4
+ from typing import (
5
+ Any , Callable , Dict , Optional , Sequence , Tuple , Type , TypeVar ,
6
+ )
4
7
5
8
from . import values # retain this import style for testability
6
9
from .context_managers import ExceptionCounter , InprogressTracker , Timer
7
10
from .metrics_core import (
8
11
Metric , METRIC_LABEL_NAME_RE , METRIC_NAME_RE ,
9
12
RESERVED_METRIC_LABEL_NAME_RE ,
10
13
)
11
- from .registry import REGISTRY
14
+ from .registry import CollectorRegistry , REGISTRY
12
15
from .samples import Exemplar
13
16
from .utils import floatToGoString , INF
14
17
18
+ T = TypeVar ('T' , bound = 'MetricWrapperBase' )
19
+ F = TypeVar ("F" , bound = Callable [..., Any ])
20
+
15
21
16
22
def _build_full_name (metric_type , name , namespace , subsystem , unit ):
17
23
full_name = ''
@@ -95,15 +101,15 @@ def __repr__(self):
95
101
return f"{ metric_type .__module__ } .{ metric_type .__name__ } ({ self ._name } )"
96
102
97
103
def __init__ (self ,
98
- name ,
99
- documentation ,
100
- labelnames = (),
101
- namespace = '' ,
102
- subsystem = '' ,
103
- unit = '' ,
104
- registry = REGISTRY ,
105
- _labelvalues = None ,
106
- ):
104
+ name : str ,
105
+ documentation : str ,
106
+ labelnames : Sequence [ str ] = (),
107
+ namespace : str = '' ,
108
+ subsystem : str = '' ,
109
+ unit : str = '' ,
110
+ registry : CollectorRegistry = REGISTRY ,
111
+ _labelvalues : Optional [ Sequence [ str ]] = None ,
112
+ ) -> None :
107
113
self ._name = _build_full_name (self ._type , name , namespace , subsystem , unit )
108
114
self ._labelnames = _validate_labelnames (self , labelnames )
109
115
self ._labelvalues = tuple (_labelvalues or ())
@@ -127,7 +133,7 @@ def __init__(self,
127
133
if registry :
128
134
registry .register (self )
129
135
130
- def labels (self , * labelvalues , ** labelkwargs ) :
136
+ def labels (self : T , * labelvalues : str , ** labelkwargs : str ) -> T :
131
137
"""Return the child for the given labelset.
132
138
133
139
All metrics can have labels, allowing grouping of related time series.
@@ -193,7 +199,7 @@ def remove(self, *labelvalues):
193
199
with self ._lock :
194
200
del self ._metrics [labelvalues ]
195
201
196
- def clear (self ):
202
+ def clear (self ) -> None :
197
203
"""Remove all labelsets from the metric"""
198
204
with self ._lock :
199
205
self ._metrics = {}
@@ -213,6 +219,7 @@ def _multi_samples(self):
213
219
yield (suffix , dict (series_labels + list (sample_labels .items ())), value , timestamp , exemplar )
214
220
215
221
def _child_samples (self ): # pragma: no cover
222
+ # type: () -> Sequence[Tuple[str, Dict[str, str], float]]
216
223
raise NotImplementedError ('_child_samples() must be implemented by %r' % self )
217
224
218
225
def _metric_init (self ): # pragma: no cover
@@ -258,12 +265,12 @@ def f():
258
265
"""
259
266
_type = 'counter'
260
267
261
- def _metric_init (self ):
268
+ def _metric_init (self ) -> None :
262
269
self ._value = values .ValueClass (self ._type , self ._name , self ._name + '_total' , self ._labelnames ,
263
270
self ._labelvalues )
264
271
self ._created = time .time ()
265
272
266
- def inc (self , amount = 1 , exemplar = None ):
273
+ def inc (self , amount : float = 1 , exemplar : Optional [ Dict [ str , str ]] = None ) -> None :
267
274
"""Increment counter by the given amount."""
268
275
self ._raise_if_not_observable ()
269
276
if amount < 0 :
@@ -273,7 +280,7 @@ def inc(self, amount=1, exemplar=None):
273
280
_validate_exemplar (exemplar )
274
281
self ._value .set_exemplar (Exemplar (exemplar , amount , time .time ()))
275
282
276
- def count_exceptions (self , exception = Exception ):
283
+ def count_exceptions (self , exception : Type [ BaseException ] = Exception ) -> ExceptionCounter :
277
284
"""Count exceptions in a block of code or function.
278
285
279
286
Can be used as a function decorator or context manager.
@@ -667,15 +674,15 @@ class Enum(MetricWrapperBase):
667
674
_type = 'stateset'
668
675
669
676
def __init__ (self ,
670
- name ,
671
- documentation ,
672
- labelnames = (),
673
- namespace = '' ,
674
- subsystem = '' ,
675
- unit = '' ,
676
- registry = REGISTRY ,
677
- _labelvalues = None ,
678
- states = None ,
677
+ name : str ,
678
+ documentation : str ,
679
+ labelnames : Sequence [ str ] = (),
680
+ namespace : str = '' ,
681
+ subsystem : str = '' ,
682
+ unit : str = '' ,
683
+ registry : CollectorRegistry = REGISTRY ,
684
+ _labelvalues : Optional [ Sequence [ str ]] = None ,
685
+ states : Optional [ Sequence [ str ]] = None ,
679
686
):
680
687
super ().__init__ (
681
688
name = name ,
@@ -693,11 +700,11 @@ def __init__(self,
693
700
raise ValueError (f'No states provided for Enum metric: { name } ' )
694
701
self ._kwargs ['states' ] = self ._states = states
695
702
696
- def _metric_init (self ):
703
+ def _metric_init (self ) -> None :
697
704
self ._value = 0
698
705
self ._lock = Lock ()
699
706
700
- def state (self , state ) :
707
+ def state (self , state : str ) -> None :
701
708
"""Set enum metric state."""
702
709
self ._raise_if_not_observable ()
703
710
with self ._lock :
0 commit comments