Skip to content

Initialize Prometheus middleware metrics on worker_boot too - #891

Open
afonsojanu wants to merge 1 commit into
Bogdanp:masterfrom
afonsojanu:fix/prometheus-middleware-worker-boot-703
Open

Initialize Prometheus middleware metrics on worker_boot too#891
afonsojanu wants to merge 1 commit into
Bogdanp:masterfrom
afonsojanu:fix/prometheus-middleware-worker-boot-703

Conversation

@afonsojanu

Copy link
Copy Markdown

Fixes #703

dramatiq.worker.Worker emits worker_boot itself, calling after_worker_boot on every middleware, but the Prometheus middleware only defined after_process_boot, which the dramatiq CLI's forked worker processes emit separately before ever constructing a Worker. A broker driven directly through the Worker API instead of the CLI never reaches that hook, so message_durations, inprogress_messages, and the rest of the metrics attributes stay unset, and every other hook that reads them raises AttributeError the moment a message comes in.

Added after_worker_boot as a thin call into the existing after_process_boot setup. Under the CLI, both events still land in the same process one after the other before any message is processed, so the metrics just get built twice in a row, which is harmless.

Added a test that constructs the middleware directly and calls after_worker_boot on its own, confirming the metrics attributes are actually set afterward. Ran the full tests/middleware/ suite (54 passed) plus flake8, isort, and mypy on the changed files, all clean.

The Worker class emits worker_boot itself, not process_boot, so a
broker driven directly through the Worker API rather than the
dramatiq CLI's forked processes (which emit process_boot before ever
constructing a Worker) never reached after_process_boot, and every
metric attribute the middleware records against stayed unset.

Added after_worker_boot as a thin call into the existing
after_process_boot setup. Under the CLI, both events still fire in
the same process, so the metrics just get set up twice in a row
before any message is processed, which is harmless.

Added a test constructing the middleware directly and calling
after_worker_boot on its own, confirming the metrics attributes are
set afterward.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prometheus middleware fails with AttributeError due to missing after_worker_boot

1 participant