2
2
from threading import Lock
3
3
import time
4
4
import types
5
+ from typing import (
6
+ Any , Callable , Dict , Optional , Sequence , Tuple , Type , TypeVar ,
7
+ )
5
8
6
9
from . import values # retain this import style for testability
7
10
from .context_managers import ExceptionCounter , InprogressTracker , Timer
8
11
from .metrics_core import (
9
12
Metric , METRIC_LABEL_NAME_RE , METRIC_NAME_RE ,
10
13
RESERVED_METRIC_LABEL_NAME_RE ,
11
14
)
12
- from .registry import REGISTRY
15
+ from .registry import CollectorRegistry , REGISTRY
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
if sys .version_info > (3 ,):
16
22
unicode = str
17
23
create_bound_method = types .MethodType
@@ -97,6 +103,7 @@ def __init__(self,
97
103
registry = REGISTRY ,
98
104
_labelvalues = None ,
99
105
):
106
+ # type: (str, str, Sequence[str], str, str, str, CollectorRegistry, Optional[Sequence[str]]) -> None
100
107
self ._name = _build_full_name (self ._type , name , namespace , subsystem , unit )
101
108
self ._labelnames = _validate_labelnames (self , labelnames )
102
109
self ._labelvalues = tuple (_labelvalues or ())
@@ -121,6 +128,7 @@ def __init__(self,
121
128
registry .register (self )
122
129
123
130
def labels (self , * labelvalues , ** labelkwargs ):
131
+ # type: (T, *str, **str) -> T
124
132
"""Return the child for the given labelset.
125
133
126
134
All metrics can have labels, allowing grouping of related time series.
@@ -187,6 +195,7 @@ def remove(self, *labelvalues):
187
195
del self ._metrics [labelvalues ]
188
196
189
197
def clear (self ):
198
+ # type: () -> None
190
199
"""Remove all labelsets from the metric"""
191
200
with self ._lock :
192
201
self ._metrics = {}
@@ -206,6 +215,7 @@ def _multi_samples(self):
206
215
yield (suffix , dict (series_labels + list (sample_labels .items ())), value )
207
216
208
217
def _child_samples (self ): # pragma: no cover
218
+ # type: () -> Sequence[Tuple[str, Dict[str, str], float]]
209
219
raise NotImplementedError ('_child_samples() must be implemented by %r' % self )
210
220
211
221
def _metric_init (self ): # pragma: no cover
@@ -252,18 +262,21 @@ def f():
252
262
_type = 'counter'
253
263
254
264
def _metric_init (self ):
265
+ # type: () -> None
255
266
self ._value = values .ValueClass (self ._type , self ._name , self ._name + '_total' , self ._labelnames ,
256
267
self ._labelvalues )
257
268
self ._created = time .time ()
258
269
259
270
def inc (self , amount = 1 ):
271
+ # type: (float) -> None
260
272
"""Increment counter by the given amount."""
261
273
self ._raise_if_not_observable ()
262
274
if amount < 0 :
263
275
raise ValueError ('Counters can only be incremented by non-negative amounts.' )
264
276
self ._value .inc (amount )
265
277
266
278
def count_exceptions (self , exception = Exception ):
279
+ # type: (Type[BaseException]) -> ExceptionCounter
267
280
"""Count exceptions in a block of code or function.
268
281
269
282
Can be used as a function decorator or context manager.
@@ -663,6 +676,7 @@ def __init__(self,
663
676
_labelvalues = None ,
664
677
states = None ,
665
678
):
679
+ # type: (str, str, Sequence[str], str, str, str, CollectorRegistry, Optional[Sequence[str]], Optional[Sequence[str]]) -> None
666
680
super (Enum , self ).__init__ (
667
681
name = name ,
668
682
documentation = documentation ,
@@ -684,6 +698,7 @@ def _metric_init(self):
684
698
self ._lock = Lock ()
685
699
686
700
def state (self , state ):
701
+ # type: (str) -> None
687
702
"""Set enum metric state."""
688
703
self ._raise_if_not_observable ()
689
704
with self ._lock :
0 commit comments