|
| 1 | +from typing import Iterable, Sequence, Optional, Tuple |
| 2 | + |
| 3 | +import prometheus_client |
| 4 | +from prometheus_client.samples import Sample |
| 5 | +from quantile_estimator import TimeWindowEstimator |
| 6 | + |
| 7 | +from .version import __version__ |
| 8 | + |
| 9 | + |
| 10 | +class Summary(prometheus_client.Summary): |
| 11 | + # pairs of (quantile, allowed error) |
| 12 | + DEFAULT_INVARIANTS = ((0.50, 0.05), (0.90, 0.01), (0.99, 0.001)) |
| 13 | + |
| 14 | + def __init__( |
| 15 | + self, |
| 16 | + name: str, |
| 17 | + documentation: str, |
| 18 | + labelnames: Iterable[str] = (), |
| 19 | + namespace: str = '', |
| 20 | + subsystem: str = '', |
| 21 | + unit: str = '', |
| 22 | + registry: Optional[prometheus_client.CollectorRegistry] = prometheus_client.REGISTRY, |
| 23 | + _labelvalues: Optional[Sequence[str]] = None, |
| 24 | + invariants: Sequence[Tuple[float, float]] = DEFAULT_INVARIANTS, |
| 25 | + max_age_seconds: int = 10 * 60, |
| 26 | + age_buckets: int = 5, |
| 27 | + ) -> None: |
| 28 | + self._invariants = invariants |
| 29 | + self._max_age_seconds = max_age_seconds |
| 30 | + self._age_buckets = age_buckets |
| 31 | + super().__init__( |
| 32 | + name, |
| 33 | + documentation, |
| 34 | + labelnames=labelnames, |
| 35 | + namespace=namespace, |
| 36 | + subsystem=subsystem, |
| 37 | + unit=unit, |
| 38 | + registry=registry, |
| 39 | + _labelvalues=_labelvalues, |
| 40 | + ) |
| 41 | + |
| 42 | + def _metric_init(self): |
| 43 | + super()._metric_init() |
| 44 | + self._estimator = TimeWindowEstimator( |
| 45 | + *self._invariants, |
| 46 | + max_age_seconds=self._max_age_seconds, |
| 47 | + age_buckets=self._age_buckets, |
| 48 | + ) |
| 49 | + |
| 50 | + def observe(self, amount): |
| 51 | + super().observe(amount) |
| 52 | + self._estimator.observe(amount) |
| 53 | + |
| 54 | + def _child_samples(self): |
| 55 | + default_samples = super()._child_samples() |
| 56 | + quantile_samples = tuple( |
| 57 | + Sample("", {"quantile": str(quantile)}, self._estimator.query(quantile), None, None) |
| 58 | + for quantile, _ in self._invariants |
| 59 | + ) |
| 60 | + return [*default_samples, *quantile_samples] |
0 commit comments