]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
prometheus: Format metrics in the collect function
authorBoris Ranto <branto@redhat.com>
Thu, 12 Jul 2018 17:29:07 +0000 (19:29 +0200)
committerBoris Ranto <branto@redhat.com>
Tue, 17 Jul 2018 20:28:59 +0000 (22:28 +0200)
This allows us to clear metrics upon formatting in the collect function.

Signed-off-by: Boris Ranto <branto@redhat.com>
src/pybind/mgr/prometheus/module.py

index c140816dc427d26a83aeb9500bb1b6d606600d9f..4aa8be6a6979b9301665ff657f10e9ea6ea42333 100644 (file)
@@ -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 '''<!DOCTYPE html>
@@ -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')