Skip to content

Commit 1f164f2

Browse files
committed
improvement
1 parent 026bfcc commit 1f164f2

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/common/prometheus/utils.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import importlib
2-
import typing
32

43
import prometheus_client
54
from django.conf import settings
65
from prometheus_client.metrics import MetricWrapperBase
76
from prometheus_client.multiprocess import MultiProcessCollector
87

9-
T = typing.TypeVar("T", bound=MetricWrapperBase)
10-
118

129
class Histogram(prometheus_client.Histogram):
1310
DEFAULT_BUCKETS = settings.PROMETHEUS_HISTOGRAM_BUCKETS
@@ -28,15 +25,15 @@ def reload_metrics(*metric_module_names: str) -> None:
2825
when needed.
2926
"""
3027

28+
registry = prometheus_client.REGISTRY
29+
3130
for module_name in metric_module_names:
3231
metrics_module = importlib.import_module(module_name)
3332

34-
_collectors = metrics_module.__dict__.values()
35-
36-
for collector in [
37-
*(registry := prometheus_client.REGISTRY)._collector_to_names
38-
]:
39-
if collector in _collectors:
40-
registry.unregister(collector)
33+
for module_attr in vars(metrics_module).values():
34+
if isinstance(module_attr, MetricWrapperBase):
35+
# Unregister the collector from the registry
36+
print("unregistering", module_attr)
37+
registry.unregister(module_attr)
4138

4239
importlib.reload(metrics_module)

0 commit comments

Comments
 (0)