|
31 | 31 | Dict, |
32 | 32 | Generic, |
33 | 33 | Iterable, |
34 | | - List, |
35 | 34 | Mapping, |
36 | 35 | Optional, |
37 | 36 | Sequence, |
|
74 | 73 |
|
75 | 74 | METRICS_PREFIX = "/_synapse/metrics" |
76 | 75 |
|
| 76 | +all_gauges: Dict[str, Collector] = {} |
| 77 | + |
77 | 78 | HAVE_PROC_SELF_STAT = os.path.exists("/proc/self/stat") |
78 | 79 |
|
79 | 80 | SERVER_NAME_LABEL = "server_name" |
@@ -162,47 +163,42 @@ class LaterGauge(Collector): |
162 | 163 | name: str |
163 | 164 | desc: str |
164 | 165 | labelnames: Optional[StrSequence] = attr.ib(hash=False) |
165 | | - # List of callbacks: each callback should either return a value (if there are no |
166 | | - # labels for this metric), or dict mapping from a label tuple to a value |
167 | | - _hooks: List[ |
168 | | - Callable[ |
169 | | - [], Union[Mapping[Tuple[str, ...], Union[int, float]], Union[int, float]] |
170 | | - ] |
171 | | - ] = attr.ib(factory=list, hash=False) |
| 166 | + # callback: should either return a value (if there are no labels for this metric), |
| 167 | + # or dict mapping from a label tuple to a value |
| 168 | + caller: Callable[ |
| 169 | + [], Union[Mapping[Tuple[str, ...], Union[int, float]], Union[int, float]] |
| 170 | + ] |
172 | 171 |
|
173 | 172 | def collect(self) -> Iterable[Metric]: |
174 | 173 | # The decision to add `SERVER_NAME_LABEL` is from the `LaterGauge` usage itself |
175 | 174 | # (we don't enforce it here, one level up). |
176 | 175 | g = GaugeMetricFamily(self.name, self.desc, labels=self.labelnames) # type: ignore[missing-server-name-label] |
177 | 176 |
|
178 | | - for hook in self._hooks: |
179 | | - try: |
180 | | - hook_result = hook() |
181 | | - except Exception: |
182 | | - logger.exception( |
183 | | - "Exception running callback for LaterGauge(%s)", self.name |
184 | | - ) |
185 | | - yield g |
186 | | - return |
187 | | - |
188 | | - if isinstance(hook_result, (int, float)): |
189 | | - g.add_metric([], hook_result) |
190 | | - else: |
191 | | - for k, v in hook_result.items(): |
192 | | - g.add_metric(k, v) |
193 | | - |
| 177 | + try: |
| 178 | + calls = self.caller() |
| 179 | + except Exception: |
| 180 | + logger.exception("Exception running callback for LaterGauge(%s)", self.name) |
194 | 181 | yield g |
| 182 | + return |
195 | 183 |
|
196 | | - def register_hook( |
197 | | - self, |
198 | | - hook: Callable[ |
199 | | - [], Union[Mapping[Tuple[str, ...], Union[int, float]], Union[int, float]] |
200 | | - ], |
201 | | - ) -> None: |
202 | | - self._hooks.append(hook) |
| 184 | + if isinstance(calls, (int, float)): |
| 185 | + g.add_metric([], calls) |
| 186 | + else: |
| 187 | + for k, v in calls.items(): |
| 188 | + g.add_metric(k, v) |
| 189 | + |
| 190 | + yield g |
203 | 191 |
|
204 | 192 | def __attrs_post_init__(self) -> None: |
| 193 | + self._register() |
| 194 | + |
| 195 | + def _register(self) -> None: |
| 196 | + if self.name in all_gauges.keys(): |
| 197 | + logger.warning("%s already registered, reregistering", self.name) |
| 198 | + REGISTRY.unregister(all_gauges.pop(self.name)) |
| 199 | + |
205 | 200 | REGISTRY.register(self) |
| 201 | + all_gauges[self.name] = self |
206 | 202 |
|
207 | 203 |
|
208 | 204 | # `MetricsEntry` only makes sense when it is a `Protocol`, |
@@ -254,7 +250,7 @@ def __init__( |
254 | 250 | # Protects access to _registrations |
255 | 251 | self._lock = threading.Lock() |
256 | 252 |
|
257 | | - REGISTRY.register(self) |
| 253 | + self._register_with_collector() |
258 | 254 |
|
259 | 255 | def register( |
260 | 256 | self, |
@@ -345,6 +341,14 @@ def collect(self) -> Iterable[Metric]: |
345 | 341 | gauge.add_metric(labels=key, value=getattr(metrics, name)) |
346 | 342 | yield gauge |
347 | 343 |
|
| 344 | + def _register_with_collector(self) -> None: |
| 345 | + if self.name in all_gauges.keys(): |
| 346 | + logger.warning("%s already registered, reregistering", self.name) |
| 347 | + REGISTRY.unregister(all_gauges.pop(self.name)) |
| 348 | + |
| 349 | + REGISTRY.register(self) |
| 350 | + all_gauges[self.name] = self |
| 351 | + |
348 | 352 |
|
349 | 353 | class GaugeHistogramMetricFamilyWithLabels(GaugeHistogramMetricFamily): |
350 | 354 | """ |
|
0 commit comments