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

Commit

Permalink
Don't report anything from GaugeBucketCollector metrics until data is…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
anoadragon453 authored Apr 6, 2021
1 parent 0481923 commit 0d87c6b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
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` Prometheus metrics from initially reporting zero-values after startup.
16 changes: 13 additions & 3 deletions synapse/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ class GaugeBucketCollector:
Prometheus, and optimise for that case.
"""

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

def __init__(
self,
Expand Down Expand Up @@ -242,11 +247,16 @@ def __init__(
if self._bucket_bounds[-1] != float("inf"):
self._bucket_bounds.append(float("inf"))

self._metric = self._values_to_metric([])
# We initially set this to None. We won't report metrics until
# this has been initialised after a successful data update
self._metric = None # type: Optional[GaugeHistogramMetricFamily]

registry.register(self)

def collect(self):
yield self._metric
# Don't report metrics unless we've already collected some data
if self._metric is not None:
yield self._metric

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

0 comments on commit 0d87c6b

Please sign in to comment.