diff options
author | reivilibre <oliverw@matrix.org> | 2022-09-08 14:30:48 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-08 15:30:48 +0100 |
commit | cf11919ddd4f48b2f59062542ba62969042f80aa (patch) | |
tree | 31ec773143715571cf2154c50f04be8f1ddc2a42 /synapse/util/metrics.py | |
parent | Fix Prometheus recording rules to not use legacy metric names. (#13718) (diff) | |
download | synapse-cf11919ddd4f48b2f59062542ba62969042f80aa.tar.xz |
Fix cache metrics not being updated when not using the legacy exposition module. (#13717)
Diffstat (limited to '')
-rw-r--r-- | synapse/util/metrics.py | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/synapse/util/metrics.py b/synapse/util/metrics.py index bc3b4938ea..9687120ebf 100644 --- a/synapse/util/metrics.py +++ b/synapse/util/metrics.py @@ -15,9 +15,9 @@ import logging from functools import wraps from types import TracebackType -from typing import Awaitable, Callable, Optional, Type, TypeVar +from typing import Awaitable, Callable, Generator, List, Optional, Type, TypeVar -from prometheus_client import Counter +from prometheus_client import CollectorRegistry, Counter, Metric from typing_extensions import Concatenate, ParamSpec, Protocol from synapse.logging.context import ( @@ -208,3 +208,33 @@ class Measure: metrics.real_time_sum += duration # TODO: Add other in flight metrics. + + +class DynamicCollectorRegistry(CollectorRegistry): + """ + Custom Prometheus Collector registry that calls a hook first, allowing you + to update metrics on-demand. + + Don't forget to register this registry with the main registry! + """ + + def __init__(self) -> None: + super().__init__() + self._pre_update_hooks: List[Callable[[], None]] = [] + + def collect(self) -> Generator[Metric, None, None]: + """ + Collects metrics, calling pre-update hooks first. + """ + + for pre_update_hook in self._pre_update_hooks: + pre_update_hook() + + yield from super().collect() + + def register_hook(self, hook: Callable[[], None]) -> None: + """ + Registers a hook that is called before metric collection. + """ + + self._pre_update_hooks.append(hook) |