Skip to content

Commit d1fa98f

Browse files
authored
Fix: Use MultiProcessCollector in PrometheusMiddleware for multi-worker support (#594)
1 parent eb29304 commit d1fa98f

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

taskiq/middlewares/prometheus_middleware.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def __init__(
3838

3939
logger.debug(f"Setting up multiproc dir to {metrics_path}")
4040

41-
os.environ["PROMETHEUS_MULTIPROC_DIR"] = str(metrics_path)
4241
os.environ["PROMETHEUS_MULTIPROC_DIR"] = str(metrics_path)
4342

4443
logger.debug("Initializing metrics")
@@ -85,11 +84,23 @@ def startup(self) -> None:
8584
This function starts prometheus server.
8685
It starts it only in case if it's a worker process.
8786
"""
88-
from prometheus_client import start_http_server # noqa: PLC0415
87+
from prometheus_client import ( # noqa: PLC0415
88+
CollectorRegistry,
89+
start_http_server,
90+
)
91+
from prometheus_client.multiprocess import ( # noqa: PLC0415
92+
MultiProcessCollector,
93+
)
8994

9095
if self.broker.is_worker_process:
9196
try:
92-
start_http_server(port=self.server_port, addr=self.server_addr)
97+
registry = CollectorRegistry()
98+
MultiProcessCollector(registry)
99+
start_http_server(
100+
port=self.server_port,
101+
addr=self.server_addr,
102+
registry=registry,
103+
)
93104
except OSError as exc:
94105
logger.debug("Cannot start prometheus server: %s", exc)
95106

0 commit comments

Comments
 (0)