From 2583672dba808b126580b0ba3e961d274a162d11 Mon Sep 17 00:00:00 2001 From: Boris Ranto Date: Thu, 12 Jul 2018 19:29:07 +0200 Subject: [PATCH] prometheus: Format metrics in the collect function This allows us to clear metrics upon formatting in the collect function. Signed-off-by: Boris Ranto --- src/pybind/mgr/prometheus/module.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/pybind/mgr/prometheus/module.py b/src/pybind/mgr/prometheus/module.py index c140816dc427d..4aa8be6a6979b 100644 --- a/src/pybind/mgr/prometheus/module.py +++ b/src/pybind/mgr/prometheus/module.py @@ -553,7 +553,12 @@ class Module(MgrModule): ) self.metrics[path].set(value, (daemon,)) - return self.metrics + # Return formatted metrics and clear no longer used data + _metrics = [m.str_expfmt() for m in self.metrics.values()] + for k in self.metrics.keys(): + self.metrics[k].clear() + + return ''.join(_metrics) + '\n' def get_file_sd_config(self): servers = self.list_servers() @@ -611,10 +616,6 @@ class Module(MgrModule): cherrypy.request.path = '' return self - def format_metrics(self, metrics): - _metrics = [m.str_expfmt() for m in metrics.values()] - return ''.join(_metrics) + '\n' - @cherrypy.expose def index(self): return ''' @@ -642,11 +643,9 @@ class Module(MgrModule): return instance.collect_cache if instance.have_mon_connection(): - metrics = instance.collect() + instance.collect_cache = instance.collect() cherrypy.response.headers['Content-Type'] = 'text/plain' - if metrics: - instance.collect_cache = self.format_metrics(metrics) - return instance.collect_cache + return instance.collect_cache else: raise cherrypy.HTTPError(503, 'No MON connection') -- 2.39.5