From 01b9f23f718193ae9a6f2d047a272abdd42a4b35 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Wed, 30 Oct 2024 14:46:51 +0100 Subject: [PATCH] Reinitialize metrics on plugin initialize to ease testing --- kinto/plugins/prometheus.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/kinto/plugins/prometheus.py b/kinto/plugins/prometheus.py index f08b83ebc..fed971ae1 100644 --- a/kinto/plugins/prometheus.py +++ b/kinto/plugins/prometheus.py @@ -179,4 +179,15 @@ def includeme(config): config.add_route("prometheus_metrics", "/__metrics__") config.add_view(metrics_view, route_name="prometheus_metrics") + # Reinitialize the registry on initialization. + # This is mainly useful in tests, where we plugins is included + # several times with different settings. + registry = get_registry() + for collector in _METRICS.values(): + try: + registry.unregister(collector) + except KeyError: # pragma: no cover + pass + _METRICS.clear() + config.registry.registerUtility(PrometheusService(), metrics.IMetricsService)