]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr/prometheus/module.py: defaultdict for num_by_state 38596/head
authorAlexander Sushko <alexandrsushko@gmail.com>
Fri, 27 Nov 2020 11:04:13 +0000 (14:04 +0300)
committerLaura Paduano <lpaduano@suse.com>
Thu, 17 Dec 2020 14:04:38 +0000 (15:04 +0100)
num_by_state[state] += count in get_pg_status method raises KeyError
if pg state is not in PG_STATES list. PG_STATES should be synced with
osd_types.cc:pg_state_string(). But sometimes it is not. After the
KeyError raise mgr metrics are not available at all.

Fixes: https://tracker.ceph.com/issues/46142
Signed-off-by: Alexander Sushko <alexandrsushko@gmail.com>
(cherry picked from commit 3f7ee9cbd335e4b8686688b79ec6110d73a7390e)

 Conflicts:
src/pybind/mgr/prometheus/module.py
 - Resolved import conflict

src/pybind/mgr/prometheus/module.py

index 390828b07bb31f3adc76fb4f151f60bacf7e4aa6..cf9eb67b60c438b14b2b04f7c385614d4992fe4a 100644 (file)
@@ -1,4 +1,5 @@
 import cherrypy
+from collections import defaultdict
 from distutils.version import StrictVersion
 import json
 import errno
@@ -12,8 +13,8 @@ from mgr_module import MgrModule, MgrStandbyModule, CommandResult, PG_STATES
 from mgr_util import get_default_addr, profile_method
 from rbd import RBD
 try:
-    from typing import Optional, Dict, Any, Set
-except:
+    from typing import DefaultDict, Optional, Dict, Any, Set
+except ImportError:
     pass
 
 # Defaults for the Prometheus HTTP server.  Can also set in config-key
@@ -571,8 +572,7 @@ class Module(MgrModule):
         pg_summary = self.get('pg_summary')
 
         for pool in pg_summary['by_pool']:
-            num_by_state = dict((state, 0) for state in PG_STATES)
-            num_by_state['total'] = 0
+            num_by_state = defaultdict(int)  # type: DefaultDict[str, int]
 
             for state_name, count in pg_summary['by_pool'][pool].items():
                 for state in state_name.split('+'):