diff --git a/synapse/metrics/_twisted_exposition.py b/synapse/metrics/_twisted_exposition.py index 2de97e3713..2804bfd35c 100644 --- a/synapse/metrics/_twisted_exposition.py +++ b/synapse/metrics/_twisted_exposition.py @@ -20,6 +20,7 @@ # # +import logging from prometheus_client import ( REGISTRY, CollectorRegistry, @@ -30,14 +31,34 @@ from prometheus_client import ( from twisted.web.resource import Resource from twisted.web.server import Request +logger = logging.getLogger(__name__) + CONTENT_TYPE_LATEST = "text/plain; version=0.0.4; charset=utf-8" -# Sets whether `prometheus_client` should expose `_created`-suffixed metrics for all -# gauges, histograms and summaries. -# -# The motivation for disabling these `_created` metrics is that they're a waste of space -# as they're not useful but they take up space in Prometheus. -disable_created_metrics() + +def _set_prometheus_client_use_created_metrics(new_value: bool) -> None: + """ + Sets whether prometheus_client should expose `_created`-suffixed metrics for + all gauges, histograms and summaries. + + There is no programmatic way to disable this without poking at internals; + the proper way is to use an environment variable which prometheus_client + loads at import time. + + The motivation for disabling these `_created` metrics is that they're + a waste of space as they're not useful but they take up space in Prometheus. + """ + import prometheus_client.metrics + + if hasattr(prometheus_client.metrics, "_use_created"): + prometheus_client.metrics._use_created = new_value + else: + logger.error( + "Can't disable `_created` metrics in prometheus_client (brittle hack broken?)" + ) + + +_set_prometheus_client_use_created_metrics(False) class MetricsResource(Resource):