Fix: PrometheusMiddleware exports metrics from only one worker when --workers > 1#594
Merged
s3rius merged 1 commit intotaskiq-python:masterfrom Feb 19, 2026
Conversation
s3rius
approved these changes
Feb 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #590
Problem
start_http_server()instartup()uses the defaultREGISTRY, which only contains metrics from the current process. With--workers 2, one worker binds port 9000 and serves only its own metrics, the other workers' mmap files are never read.Root Cause
Each worker process writes its metrics to a separate mmap file (
counter_{pid}.db) inPROMETHEUS_MULTIPROC_DIR- this part works correctly. However,start_http_server()without theregistry=parameter serves from the defaultREGISTRY, which only callsCounter.collect()/Histogram.collect()on the current process's values (source).Per prometheus_client multiprocess docs, a separate
CollectorRegistrywithMultiProcessCollectoris required to aggregate mmap files from all processes.Fix
CollectorRegistry+MultiProcessCollectortostart_http_server()instartup()os.environassignment (line 42)Testing
Tested with
--workers 2on taskiq 0.12.1:received_tasks_total = 3.0(one worker only)received_tasks_total = 12.0(all workers aggregated)