A Counter tracks counts of events or running totals. Example use cases for Counters: - Number of requests processed - Number of items that were inserted into a queue - Total amount of data that a system has processed Counters can only go up (and be reset when the process restar
| 288 | |
| 289 | |
| 290 | class Counter(MetricWrapperBase): |
| 291 | """A Counter tracks counts of events or running totals. |
| 292 | |
| 293 | Example use cases for Counters: |
| 294 | - Number of requests processed |
| 295 | - Number of items that were inserted into a queue |
| 296 | - Total amount of data that a system has processed |
| 297 | |
| 298 | Counters can only go up (and be reset when the process restarts). If your use case can go down, |
| 299 | you should use a Gauge instead. |
| 300 | |
| 301 | An example for a Counter: |
| 302 | |
| 303 | from prometheus_client import Counter |
| 304 | |
| 305 | c = Counter('my_failures_total', 'Description of counter') |
| 306 | c.inc() # Increment by 1 |
| 307 | c.inc(1.6) # Increment by given value |
| 308 | |
| 309 | There are utilities to count exceptions raised: |
| 310 | |
| 311 | @c.count_exceptions() |
| 312 | def f(): |
| 313 | pass |
| 314 | |
| 315 | with c.count_exceptions(): |
| 316 | pass |
| 317 | |
| 318 | # Count only one type of exception |
| 319 | with c.count_exceptions(ValueError): |
| 320 | pass |
| 321 | |
| 322 | You can also reset the counter to zero in case your logical "process" restarts |
| 323 | without restarting the actual python process. |
| 324 | |
| 325 | c.reset() |
| 326 | |
| 327 | """ |
| 328 | _type = 'counter' |
| 329 | |
| 330 | def _metric_init(self) -> None: |
| 331 | self._value = values.ValueClass(self._type, self._name, self._name + '_total', self._labelnames, |
| 332 | self._labelvalues, self._documentation) |
| 333 | self._created = time.time() |
| 334 | |
| 335 | def inc(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> None: |
| 336 | """Increment counter by the given amount.""" |
| 337 | self._raise_if_not_observable() |
| 338 | if amount < 0: |
| 339 | raise ValueError('Counters can only be incremented by non-negative amounts.') |
| 340 | self._value.inc(amount) |
| 341 | if exemplar: |
| 342 | _validate_exemplar(exemplar) |
| 343 | self._value.set_exemplar(Exemplar(exemplar, amount, time.time())) |
| 344 | |
| 345 | def reset(self) -> None: |
| 346 | """Reset the counter to zero. Use this when a logical process restarts without restarting the actual python process.""" |
| 347 | self._value.set(0) |
no outgoing calls