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

Don't report anything from GaugeBucketCollector metrics until data is present #8926

Merged
merged 8 commits into from
Apr 6, 2021
1 change: 1 addition & 0 deletions changelog.d/8926.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prevent `synapse_forward_extremities` and `synapse_excess_extremity_events` metrics from initially reporting zero-values after startup.
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 15 additions & 2 deletions synapse/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,14 @@ class GaugeBucketCollector:
Prometheus, and optimise for that case.
"""

__slots__ = ("_name", "_documentation", "_bucket_bounds", "_metric")
__slots__ = (
"_name",
"_documentation",
"_bucket_bounds",
"_metric",
"_registry",
"_registered",
)

def __init__(
self,
Expand All @@ -234,6 +241,8 @@ def __init__(
"""
self._name = name
self._documentation = documentation
self._registry = registry
self._registered = False

# the tops of the buckets
self._bucket_bounds = [float(b) for b in buckets]
Expand All @@ -244,7 +253,6 @@ def __init__(
self._bucket_bounds.append(float("inf"))

self._metric = self._values_to_metric([])
registry.register(self)

def collect(self):
yield self._metric
Expand All @@ -255,6 +263,11 @@ def update_data(self, values: Iterable[float]):
The existing data is cleared, and each measurement in the input is assigned
to the relevant bucket.
"""
# Wait until we have data to report before registering
if not self._registered:
self._registry.register(self)
self._registered = True

self._metric = self._values_to_metric(values)

def _values_to_metric(self, values: Iterable[float]) -> GaugeHistogramMetricFamily:
Expand Down