File tree Expand file tree Collapse file tree 1 file changed +33
-1
lines changed
Expand file tree Collapse file tree 1 file changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -102,7 +102,33 @@ def _cleanup_multiprocess_dir():
102102atexit .register (_cleanup_multiprocess_dir )
103103
104104# Now safe to import prometheus_client — it will detect the env var.
105- from prometheus_client import Counter , Gauge , Histogram # noqa: E402
105+ _prometheus_available = True
106+ try :
107+ from prometheus_client import Counter , Gauge , Histogram # noqa: E402
108+ except Exception :
109+ _prometheus_available = False
110+
111+ class _NoOpMetric :
112+ def labels (self , ** kwargs ):
113+ return self
114+
115+ def inc (self , amount : float = 1 ):
116+ return None
117+
118+ def observe (self , amount : float ):
119+ return None
120+
121+ def set (self , value : float ):
122+ return None
123+
124+ def Counter (* args , ** kwargs ): # type: ignore
125+ return _NoOpMetric ()
126+
127+ def Gauge (* args , ** kwargs ): # type: ignore
128+ return _NoOpMetric ()
129+
130+ def Histogram (* args , ** kwargs ): # type: ignore
131+ return _NoOpMetric ()
106132
107133
108134# ---------------------------------------------------------------------------
@@ -473,6 +499,12 @@ def start_metrics_server(
473499 """
474500 global _config
475501
502+ if not _prometheus_available :
503+ logger .warning (
504+ "Prometheus metrics are unavailable because prometheus_client could not be imported."
505+ )
506+ return
507+
476508 if metrics_config is not None :
477509 _config = metrics_config
478510 else :
You can’t perform that action at this time.
0 commit comments