| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 423dfa3 commit e490cb8
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | import re | |
| 2 | + from typing import Dict, List, Optional, Sequence, Tuple, Union | ||
| 2 | 3 | ||
| 3 | - from .samples import Sample | ||
| 4 | + from .samples import Exemplar, Sample, Timestamp | ||
| 4 | 5 | ||
| 5 | 6 | METRIC_TYPES = ( | |
| 6 | 7 | 'counter', 'gauge', 'summary', 'histogram', | |
@@ -20,36 +21,36 @@ class Metric: | |||
| 20 | 21 | and SummaryMetricFamily instead. | |
| 21 | 22 | """ | |
| 22 | 23 | ||
| 23 | - def __init__(self, name, documentation, typ, unit=''): | ||
| 24 | + def __init__(self, name: str, documentation: str, typ: str, unit: str = ''): | ||
| 24 | 25 | if unit and not name.endswith("_" + unit): | |
| 25 | 26 | name += "_" + unit | |
| 26 | 27 | if not METRIC_NAME_RE.match(name): | |
| 27 | 28 | raise ValueError('Invalid metric name: ' + name) | |
| 28 | - self.name = name | ||
| 29 | - self.documentation = documentation | ||
| 30 | - self.unit = unit | ||
| 29 | + self.name: str = name | ||
| 30 | + self.documentation: str = documentation | ||
| 31 | + self.unit: str = unit | ||
| 31 | 32 | if typ == 'untyped': | |
| 32 | 33 | typ = 'unknown' | |
| 33 | 34 | if typ not in METRIC_TYPES: | |
| 34 | 35 | raise ValueError('Invalid metric type: ' + typ) | |
| 35 | - self.type = typ | ||
| 36 | - self.samples = [] | ||
| 36 | + self.type: str = typ | ||
| 37 | + self.samples: List[Sample] = [] | ||
| 37 | 38 | ||
| 38 | - def add_sample(self, name, labels, value, timestamp=None, exemplar=None): | ||
| 39 | + def add_sample(self, name: str, labels: Dict[str, str], value: float, timestamp: Optional[Union[Timestamp, float]] = None, exemplar: Optional[Exemplar] = None) -> None: | ||
| 39 | 40 | """Add a sample to the metric. | |
| 40 | 41 | ||
| 41 | 42 | Internal-only, do not use.""" | |
| 42 | 43 | self.samples.append(Sample(name, labels, value, timestamp, exemplar)) | |
| 43 | 44 | ||
| 44 | - def __eq__(self, other): | ||
| 45 | + def __eq__(self, other: object) -> bool: | ||
| 45 | 46 | return (isinstance(other, Metric) | |
| 46 | - and self.name == other.name | ||
| 47 | + and self.name == other.name | ||
| 47 | 48 | and self.documentation == other.documentation | |
| 48 | 49 | and self.type == other.type | |
| 49 | 50 | and self.unit == other.unit | |
| 50 | 51 | and self.samples == other.samples) | |
| 51 | 52 | ||
| 52 | - def __repr__(self): | ||
| 53 | + def __repr__(self) -> str: | ||
| 53 | 54 | return "Metric({}, {}, {}, {}, {})".format( | |
| 54 | 55 | self.name, | |
| 55 | 56 | self.documentation, | |
@@ -73,7 +74,13 @@ class UnknownMetricFamily(Metric): | |||
| 73 | 74 | For use by custom collectors. | |
| 74 | 75 | """ | |
| 75 | 76 | ||
| 76 | - def __init__(self, name, documentation, value=None, labels=None, unit=''): | ||
| 77 | + def __init__(self, | ||
| 78 | + name: str, | ||
| 79 | + documentation: str, | ||
| 80 | + value: Optional[float] = None, | ||
| 81 | + labels: Optional[Sequence[str]] = None, | ||
| 82 | + unit: str = '', | ||
| 83 | + ): | ||
| 77 | 84 | Metric.__init__(self, name, documentation, 'unknown', unit) | |
| 78 | 85 | if labels is not None and value is not None: | |
| 79 | 86 | raise ValueError('Can only specify at most one of value and labels.') | |
@@ -83,7 +90,7 @@ def __init__(self, name, documentation, value=None, labels=None, unit=''): | |||
| 83 | 90 | if value is not None: | |
| 84 | 91 | self.add_metric([], value) | |
| 85 | 92 | ||
| 86 | - def add_metric(self, labels, value, timestamp=None): | ||
| 93 | + def add_metric(self, labels: Sequence[str], value: float, timestamp: Optional[Union[Timestamp, float]] = None) -> None: | ||
| 87 | 94 | """Add a metric to the metric family. | |
| 88 | 95 | Args: | |
| 89 | 96 | labels: A list of label values | |
@@ -102,7 +109,14 @@ class CounterMetricFamily(Metric): | |||
| 102 | 109 | For use by custom collectors. | |
| 103 | 110 | """ | |
| 104 | 111 | ||
| 105 | - def __init__(self, name, documentation, value=None, labels=None, created=None, unit=''): | ||
| 112 | + def __init__(self, | ||
| 113 | + name: str, | ||
| 114 | + documentation: str, | ||
| 115 | + value: Optional[float] = None, | ||
| 116 | + labels: Sequence[str] = None, | ||
| 117 | + created: Optional[float] = None, | ||
| 118 | + unit: str = '', | ||
| 119 | + ): | ||
| 106 | 120 | # Glue code for pre-OpenMetrics metrics. | |
| 107 | 121 | if name.endswith('_total'): | |
| 108 | 122 | name = name[:-6] | |
@@ -115,7 +129,12 @@ def __init__(self, name, documentation, value=None, labels=None, created=None, u | |||
| 115 | 129 | if value is not None: | |
| 116 | 130 | self.add_metric([], value, created) | |
| 117 | 131 | ||
| 118 | - def add_metric(self, labels, value, created=None, timestamp=None): | ||
| 132 | + def add_metric(self, | ||
| 133 | + labels: Sequence[str], | ||
| 134 | + value: float, | ||
| 135 | + created: Optional[float] = None, | ||
| 136 | + timestamp: Optional[Union[Timestamp, float]] = None, | ||
| 137 | + ) -> None: | ||
| 119 | 138 | """Add a metric to the metric family. | |
| 120 | 139 | ||
| 121 | 140 | Args: | |
@@ -134,7 +153,13 @@ class GaugeMetricFamily(Metric): | |||
| 134 | 153 | For use by custom collectors. | |
| 135 | 154 | """ | |
| 136 | 155 | ||
| 137 | - def __init__(self, name, documentation, value=None, labels=None, unit=''): | ||
| 156 | + def __init__(self, | ||
| 157 | + name: str, | ||
| 158 | + documentation: str, | ||
| 159 | + value: Optional[float] = None, | ||
| 160 | + labels: Optional[Sequence[str]] = None, | ||
| 161 | + unit: str = '', | ||
| 162 | + ): | ||
| 138 | 163 | Metric.__init__(self, name, documentation, 'gauge', unit) | |
| 139 | 164 | if labels is not None and value is not None: | |
| 140 | 165 | raise ValueError('Can only specify at most one of value and labels.') | |
@@ -144,7 +169,7 @@ def __init__(self, name, documentation, value=None, labels=None, unit=''): | |||
| 144 | 169 | if value is not None: | |
| 145 | 170 | self.add_metric([], value) | |
| 146 | 171 | ||
| 147 | - def add_metric(self, labels, value, timestamp=None): | ||
| 172 | + def add_metric(self, labels: Sequence[str], value: float, timestamp: Optional[Union[Timestamp, float]] = None) -> None: | ||
| 148 | 173 | """Add a metric to the metric family. | |
| 149 | 174 | ||
| 150 | 175 | Args: | |
@@ -160,7 +185,14 @@ class SummaryMetricFamily(Metric): | |||
| 160 | 185 | For use by custom collectors. | |
| 161 | 186 | """ | |
| 162 | 187 | ||
| 163 | - def __init__(self, name, documentation, count_value=None, sum_value=None, labels=None, unit=''): | ||
| 188 | + def __init__(self, | ||
| 189 | + name: str, | ||
| 190 | + documentation: str, | ||
| 191 | + count_value: Optional[float] = None, | ||
| 192 | + sum_value: Optional[float] = None, | ||
| 193 | + labels: Optional[Sequence[str]] = None, | ||
| 194 | + unit: str = '', | ||
| 195 | + ): | ||
| 164 | 196 | Metric.__init__(self, name, documentation, 'summary', unit) | |
| 165 | 197 | if (sum_value is None) != (count_value is None): | |
| 166 | 198 | raise ValueError('count_value and sum_value must be provided together.') | |
@@ -169,10 +201,17 @@ def __init__(self, name, documentation, count_value=None, sum_value=None, labels | |||
| 169 | 201 | if labels is None: | |
| 170 | 202 | labels = [] | |
| 171 | 203 | self._labelnames = tuple(labels) | |
| 172 | - if count_value is not None: | ||
| 204 | + # The and clause is necessary only for typing, the above ValueError will raise if only one is set. | ||
| 205 | + if count_value is not None and sum_value is not None: | ||
| 173 | 206 | self.add_metric([], count_value, sum_value) | |
| 174 | 207 | ||
| 175 | - def add_metric(self, labels, count_value, sum_value, timestamp=None): | ||
| 208 | + def add_metric(self, | ||
| 209 | + labels: Sequence[str], | ||
| 210 | + count_value: float, | ||
| 211 | + sum_value: float, | ||
| 212 | + timestamp: | ||
| 213 | + Optional[Union[float, Timestamp]] = None | ||
| 214 | + ) -> None: | ||
| 176 | 215 | """Add a metric to the metric family. | |
| 177 | 216 | ||
| 178 | 217 | Args: | |
@@ -190,7 +229,14 @@ class HistogramMetricFamily(Metric): | |||
| 190 | 229 | For use by custom collectors. | |
| 191 | 230 | """ | |
| 192 | 231 | ||
| 193 | - def __init__(self, name, documentation, buckets=None, sum_value=None, labels=None, unit=''): | ||
| 232 | + def __init__(self, | ||
| 233 | + name: str, | ||
| 234 | + documentation: str, | ||
| 235 | + buckets: Optional[Sequence[Tuple[str, float, Optional[Exemplar]]]] = None, | ||
| 236 | + sum_value: Optional[float] = None, | ||
| 237 | + labels: Optional[Sequence[str]] = None, | ||
| 238 | + unit: str = '', | ||
| 239 | + ): | ||
| 194 | 240 | Metric.__init__(self, name, documentation, 'histogram', unit) | |
| 195 | 241 | if sum_value is not None and buckets is None: | |
| 196 | 242 | raise ValueError('sum value cannot be provided without buckets.') | |
@@ -202,7 +248,11 @@ def __init__(self, name, documentation, buckets=None, sum_value=None, labels=Non | |||
| 202 | 248 | if buckets is not None: | |
| 203 | 249 | self.add_metric([], buckets, sum_value) | |
| 204 | 250 | ||
| 205 | - def add_metric(self, labels, buckets, sum_value, timestamp=None): | ||
| 251 | + def add_metric(self, | ||
| 252 | + labels: Sequence[str], | ||
| 253 | + buckets: Sequence[Tuple[str, float, Optional[Exemplar]]], | ||
| 254 | + sum_value: Optional[float], | ||
| 255 | + timestamp: Optional[Union[Timestamp, float]] = None) -> None: | ||
| 206 | 256 | """Add a metric to the metric family. | |
| 207 | 257 | ||
| 208 | 258 | Args: | |
@@ -241,7 +291,14 @@ class GaugeHistogramMetricFamily(Metric): | |||
| 241 | 291 | For use by custom collectors. | |
| 242 | 292 | """ | |
| 243 | 293 | ||
| 244 | - def __init__(self, name, documentation, buckets=None, gsum_value=None, labels=None, unit=''): | ||
| 294 | + def __init__(self, | ||
| 295 | + name: str, | ||
| 296 | + documentation: str, | ||
| 297 | + buckets: Optional[Sequence[Tuple[str, float]]] = None, | ||
| 298 | + gsum_value: Optional[float] = None, | ||
| 299 | + labels: Optional[Sequence[str]] = None, | ||
| 300 | + unit: str = '', | ||
| 301 | + ): | ||
| 245 | 302 | Metric.__init__(self, name, documentation, 'gaugehistogram', unit) | |
| 246 | 303 | if labels is not None and buckets is not None: | |
| 247 | 304 | raise ValueError('Can only specify at most one of buckets and labels.') | |
@@ -251,7 +308,12 @@ def __init__(self, name, documentation, buckets=None, gsum_value=None, labels=No | |||
| 251 | 308 | if buckets is not None: | |
| 252 | 309 | self.add_metric([], buckets, gsum_value) | |
| 253 | 310 | ||
| 254 | - def add_metric(self, labels, buckets, gsum_value, timestamp=None): | ||
| 311 | + def add_metric(self, | ||
| 312 | + labels: Sequence[str], | ||
| 313 | + buckets: Sequence[Tuple[str, float]], | ||
| 314 | + gsum_value: Optional[float], | ||
| 315 | + timestamp: Optional[Union[float, Timestamp]] = None, | ||
| 316 | + ) -> None: | ||
| 255 | 317 | """Add a metric to the metric family. | |
| 256 | 318 | ||
| 257 | 319 | Args: | |
@@ -268,7 +330,8 @@ def add_metric(self, labels, buckets, gsum_value, timestamp=None): | |||
| 268 | 330 | # +Inf is last and provides the count value. | |
| 269 | 331 | self.samples.extend([ | |
| 270 | 332 | Sample(self.name + '_gcount', dict(zip(self._labelnames, labels)), buckets[-1][1], timestamp), | |
| 271 | - Sample(self.name + '_gsum', dict(zip(self._labelnames, labels)), gsum_value, timestamp), | ||
| 333 | + # TODO: Handle None gsum_value correctly. Currently a None will fail exposition but is allowed here. | ||
| 334 | + Sample(self.name + '_gsum', dict(zip(self._labelnames, labels)), gsum_value, timestamp), # type: ignore | ||
| 272 | 335 | ]) | |
| 273 | 336 | ||
| 274 | 337 | ||
@@ -278,7 +341,12 @@ class InfoMetricFamily(Metric): | |||
| 278 | 341 | For use by custom collectors. | |
| 279 | 342 | """ | |
| 280 | 343 | ||
| 281 | - def __init__(self, name, documentation, value=None, labels=None): | ||
| 344 | + def __init__(self, | ||
| 345 | + name: str, | ||
| 346 | + documentation: str, | ||
| 347 | + value: Optional[Dict[str, str]] = None, | ||
| 348 | + labels: Optional[Sequence[str]] = None, | ||
| 349 | + ): | ||
| 282 | 350 | Metric.__init__(self, name, documentation, 'info') | |
| 283 | 351 | if labels is not None and value is not None: | |
| 284 | 352 | raise ValueError('Can only specify at most one of value and labels.') | |
@@ -288,7 +356,11 @@ def __init__(self, name, documentation, value=None, labels=None): | |||
| 288 | 356 | if value is not None: | |
| 289 | 357 | self.add_metric([], value) | |
| 290 | 358 | ||
| 291 | - def add_metric(self, labels, value, timestamp=None): | ||
| 359 | + def add_metric(self, | ||
| 360 | + labels: Sequence[str], | ||
| 361 | + value: Dict[str, str], | ||
| 362 | + timestamp: Optional[Union[Timestamp, float]] = None, | ||
| 363 | + ) -> None: | ||
| 292 | 364 | """Add a metric to the metric family. | |
| 293 | 365 | ||
| 294 | 366 | Args: | |
@@ -309,7 +381,12 @@ class StateSetMetricFamily(Metric): | |||
| 309 | 381 | For use by custom collectors. | |
| 310 | 382 | """ | |
| 311 | 383 | ||
| 312 | - def __init__(self, name, documentation, value=None, labels=None): | ||
| 384 | + def __init__(self, | ||
| 385 | + name: str, | ||
| 386 | + documentation: str, | ||
| 387 | + value: Optional[Dict[str, bool]] = None, | ||
| 388 | + labels: Optional[Sequence[str]] = None, | ||
| 389 | + ): | ||
| 313 | 390 | Metric.__init__(self, name, documentation, 'stateset') | |
| 314 | 391 | if labels is not None and value is not None: | |
| 315 | 392 | raise ValueError('Can only specify at most one of value and labels.') | |
@@ -319,7 +396,11 @@ def __init__(self, name, documentation, value=None, labels=None): | |||
| 319 | 396 | if value is not None: | |
| 320 | 397 | self.add_metric([], value) | |
| 321 | 398 | ||
| 322 | - def add_metric(self, labels, value, timestamp=None): | ||
| 399 | + def add_metric(self, | ||
| 400 | + labels: Sequence[str], | ||
| 401 | + value: Dict[str, bool], | ||
| 402 | + timestamp: Optional[Union[Timestamp, float]] = None, | ||
| 403 | + ) -> None: | ||
| 323 | 404 | """Add a metric to the metric family. | |
| 324 | 405 | ||
| 325 | 406 | Args: | |
| Back | FazBrowse Home | New Git URL |
0 commit comments