]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/prometheus: enable mypy type checking for prometheus module
authorPatrick Seidensal <pseidensal@suse.com>
Tue, 9 Jun 2020 12:46:34 +0000 (14:46 +0200)
committerPatrick Seidensal <pseidensal@suse.com>
Mon, 22 Jun 2020 13:54:40 +0000 (15:54 +0200)
Signed-off-by: Patrick Seidensal <pseidensal@suse.com>
src/pybind/mgr/prometheus/module.py
src/pybind/mgr/tox.ini

index cdb647d8810e5109af4de59eebaed1ff731223de..1fdf08debab337bd5a8601b5474844aa4a325e75 100644 (file)
@@ -13,7 +13,7 @@ from mgr_util import get_default_addr
 from rbd import RBD
 
 try:
-    from typing import Optional
+    from typing import Optional, Dict, Any, Set
 except:
     pass
 
@@ -161,8 +161,8 @@ class Metric(object):
 
         for labelvalues, value in self.value.items():
             if self.labelnames:
-                labels = zip(self.labelnames, labelvalues)
-                labels = ','.join('%s="%s"' % (k, v) for k, v in labels)
+                labels_list = zip(self.labelnames, labelvalues)
+                labels = ','.join('%s="%s"' % (k, v) for k, v in labels_list)
             else:
                 labels = ''
             if labels:
@@ -178,41 +178,44 @@ class Metric(object):
 
 
 class MetricCollectionThread(threading.Thread):
-    def __init__(self):
+    def __init__(self, module):
+        # type: (Module) -> None
+        self.mod = module
         super(MetricCollectionThread, self).__init__(target=self.collect)
 
-    @staticmethod
-    def collect():
-        inst = _global_instance
-        inst.log.info('starting metric collection thread')
+    def collect(self):
+        self.mod.log.info('starting metric collection thread')
         while True:
-            if inst.have_mon_connection():
+            self.mod.log.debug('collecting cache in thread')
+            if self.mod.have_mon_connection():
                 start_time = time.time()
-                data = inst.collect()
+                data = self.mod.collect()
                 duration = time.time() - start_time
+
+                self.mod.log.debug('collecting cache in thread done')
                 
-                sleep_time = inst.scrape_interval - duration
+                sleep_time = self.mod.scrape_interval - duration
                 if sleep_time < 0:
-                    inst.log.warning(
+                    self.mod.log.warning(
                         'Collecting data took more time than configured scrape interval. '
                         'This possibly results in stale data. Please check the '
                         '`stale_cache_strategy` configuration option. '
                         'Collecting data took {:.2f} seconds but scrape interval is configured '
                         'to be {:.0f} seconds.'.format(
                             duration,
-                            inst.scrape_interval,
+                            self.mod.scrape_interval,
                         )
                     )
                     sleep_time = 0
 
-                with inst.collect_lock:
-                    inst.collect_cache = data
-                    inst.collect_time = duration
+                with self.mod.collect_lock:
+                    self.mod.collect_cache = data
+                    self.mod.collect_time = duration
 
                 time.sleep(sleep_time)
             else:
-                inst.log.error('No MON connection')
-                time.sleep(inst.scrape_interval)
+                self.mod.log.error('No MON connection')
+                time.sleep(self.mod.scrape_interval)
 
 
 class Module(MgrModule):
@@ -241,7 +244,7 @@ class Module(MgrModule):
         self.metrics = self._setup_static_metrics()
         self.shutdown_event = threading.Event()
         self.collect_lock = threading.Lock()
-        self.collect_time = 0
+        self.collect_time = 0.0
         self.scrape_interval = 15.0
         self.stale_cache_strategy = self.STALE_CACHE_FAIL
         self.collect_cache = None
@@ -262,10 +265,10 @@ class Module(MgrModule):
                 'read_latency': {'type': self.PERFCOUNTER_LONGRUNAVG,
                                  'desc': 'RBD image reads latency (msec)'},
             },
-        }
+        }  # type: Dict[str, Any]
         global _global_instance
         _global_instance = self
-        MetricCollectionThread().start()
+        MetricCollectionThread(_global_instance).start()
 
     def _setup_static_metrics(self):
         metrics = {}
@@ -763,7 +766,7 @@ class Module(MgrModule):
             else:
                 pool_keys.append((pool_name, namespace_name))
 
-        pools = {}
+        pools = {}  # type: Dict[str, Set[str]]
         for pool_key in pool_keys:
             pool_name = pool_key[0]
             namespace_name = pool_key[1]
@@ -779,7 +782,7 @@ class Module(MgrModule):
             pools[pool_name].add(namespace_name)
 
         rbd_stats_pools = {}
-        for pool_id in list(self.rbd_stats['pools']):
+        for pool_id in self.rbd_stats['pools'].keys():
             name = self.rbd_stats['pools'][pool_id]['name']
             if name not in pools:
                 del self.rbd_stats['pools'][pool_id]
@@ -1080,6 +1083,7 @@ class Module(MgrModule):
                 # TODO use get_config_prefix or get_config here once
                 # https://github.com/ceph/ceph/pull/20458 is merged
                 result = CommandResult("")
+                assert isinstance(_global_instance, Module)
                 _global_instance.send_command(
                     result, "mon", '',
                     json.dumps({
@@ -1137,16 +1141,19 @@ class Module(MgrModule):
             @cherrypy.expose
             def metrics(self):
                 # Lock the function execution
+                assert isinstance(_global_instance, Module)
                 with _global_instance.collect_lock:
                     return self._metrics(_global_instance)
 
             @staticmethod
             def _metrics(instance):
+                # type: (Module) -> Any
                 # Return cached data if available
                 if not instance.collect_cache:
                     raise cherrypy.HTTPError(503, 'No cached data available yet')
 
                 def respond():
+                    assert isinstance(instance, Module)
                     cherrypy.response.headers['Content-Type'] = 'text/plain'
                     return instance.collect_cache
 
index 21777e6478bdf66e06bd10d269e4b08ca957a1c4..2995a4bd964b79dffc207491a50d894acab8149f 100644 (file)
@@ -31,6 +31,7 @@ commands = mypy --config-file=../../mypy.ini \
            cephadm/module.py \
            mgr_module.py \
            dashboard/module.py \
+           prometheus/module.py \
            mgr_util.py \
            orchestrator/__init__.py \
            progress/module.py \