Skip to content

Commit cdfb737

Browse files
committed
Inherit from GaugeHistogramMetricFamily
See #18715 (comment)
1 parent 3fdac55 commit cdfb737

1 file changed

Lines changed: 3 additions & 48 deletions

File tree

synapse/metrics/__init__.py

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@
5454
)
5555
from prometheus_client.core import (
5656
REGISTRY,
57+
GaugeHistogramMetricFamily,
5758
GaugeMetricFamily,
58-
Sample,
59-
Timestamp,
6059
)
6160

6261
from twisted.python.threadpool import ThreadPool
@@ -345,7 +344,7 @@ def _register_with_collector(self) -> None:
345344
all_gauges[self.name] = self
346345

347346

348-
class GaugeHistogramMetricFamilyWithLabels(Metric):
347+
class GaugeHistogramMetricFamilyWithLabels(GaugeHistogramMetricFamily):
349348
"""
350349
Custom version of `GaugeHistogramMetricFamily` from `prometheus_client` that allows
351350
specifying labels and label values.
@@ -376,51 +375,7 @@ def __init__(
376375

377376
# Create a gauge for each bucket.
378377
if buckets is not None:
379-
self.add_metric(
380-
labelvalues=labelvalues, buckets=buckets, gsum_value=gsum_value
381-
)
382-
383-
def add_metric(
384-
self,
385-
labelvalues: StrSequence,
386-
buckets: Sequence[Tuple[str, float]],
387-
gsum_value: float,
388-
timestamp: Optional[Union[float, Timestamp]] = None,
389-
) -> None:
390-
"""Add a metric to the metric family.
391-
392-
Args:
393-
labelvalues: A list of label values
394-
buckets: A list of pairs of bucket names and values.
395-
The buckets must be sorted, and +Inf present.
396-
gsum_value: The sum value of the metric.
397-
"""
398-
for bucket, value in buckets:
399-
self.samples.append(
400-
Sample(
401-
self.name + "_bucket",
402-
dict(list(zip(self._labelnames, labelvalues)) + [("le", bucket)]),
403-
value,
404-
timestamp,
405-
)
406-
)
407-
# +Inf is last and provides the count value.
408-
self.samples.extend(
409-
[
410-
Sample(
411-
self.name + "_gcount",
412-
dict(zip(self._labelnames, labelvalues)),
413-
buckets[-1][1],
414-
timestamp,
415-
),
416-
Sample(
417-
self.name + "_gsum",
418-
dict(zip(self._labelnames, labelvalues)),
419-
gsum_value,
420-
timestamp,
421-
),
422-
]
423-
)
378+
self.add_metric(labels=labelvalues, buckets=buckets, gsum_value=gsum_value)
424379

425380

426381
class GaugeBucketCollector(Collector):

0 commit comments

Comments
 (0)