From: Alexander Sushko Date: Fri, 27 Nov 2020 11:04:13 +0000 (+0300) Subject: pybind/mgr/prometheus/module.py: defaultdict for num_by_state X-Git-Tag: v16.1.0~315^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=refs%2Fpull%2F37909%2Fhead;p=ceph.git pybind/mgr/prometheus/module.py: defaultdict for num_by_state 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 --- diff --git a/src/pybind/mgr/prometheus/module.py b/src/pybind/mgr/prometheus/module.py index 71d451337d12..8e9b9317d993 100644 --- a/src/pybind/mgr/prometheus/module.py +++ b/src/pybind/mgr/prometheus/module.py @@ -1,4 +1,5 @@ import cherrypy +from collections import defaultdict from distutils.version import StrictVersion import json import errno @@ -13,7 +14,7 @@ from mgr_util import get_default_addr, profile_method from rbd import RBD from collections import namedtuple try: - from typing import Optional, Dict, Any, Set + from typing import DefaultDict, Optional, Dict, Any, Set except ImportError: pass @@ -634,8 +635,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('+'):