Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 0d87c6b

Browse files
Don't report anything from GaugeBucketCollector metrics until data is present (#8926)
This PR modifies `GaugeBucketCollector` to only report data once it has been updated, rather than initially reporting a value of 0. Fixes zero values being reported for some metrics on startup until a background job to update the metric's value runs later.
1 parent 0481923 commit 0d87c6b

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

changelog.d/8926.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Prevent `synapse_forward_extremities` and `synapse_excess_extremity_events` Prometheus metrics from initially reporting zero-values after startup.

synapse/metrics/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,12 @@ class GaugeBucketCollector:
214214
Prometheus, and optimise for that case.
215215
"""
216216

217-
__slots__ = ("_name", "_documentation", "_bucket_bounds", "_metric")
217+
__slots__ = (
218+
"_name",
219+
"_documentation",
220+
"_bucket_bounds",
221+
"_metric",
222+
)
218223

219224
def __init__(
220225
self,
@@ -242,11 +247,16 @@ def __init__(
242247
if self._bucket_bounds[-1] != float("inf"):
243248
self._bucket_bounds.append(float("inf"))
244249

245-
self._metric = self._values_to_metric([])
250+
# We initially set this to None. We won't report metrics until
251+
# this has been initialised after a successful data update
252+
self._metric = None # type: Optional[GaugeHistogramMetricFamily]
253+
246254
registry.register(self)
247255

248256
def collect(self):
249-
yield self._metric
257+
# Don't report metrics unless we've already collected some data
258+
if self._metric is not None:
259+
yield self._metric
250260

251261
def update_data(self, values: Iterable[float]):
252262
"""Update the data to be reported by the metric

0 commit comments

Comments
 (0)