Fix Prometheus delayed_messages_inprogress gauge never returning to zero (#672)#876
Closed
apoorvdarshan wants to merge 1 commit into
Closed
Conversation
The Prometheus middleware tracks dramatiq_delayed_messages_inprogress by incrementing the gauge in before_delay_message and decrementing it in before_process_message. A delayed message is held on the ".DQ" queue, so before_delay_message labels the gauge with the ".DQ" queue name. When the eta passes, the worker re-enqueues the message onto the canonical queue (via q_name()), and before_process_message decrements the gauge using that canonical queue name. Because the increment and decrement use different queue_name labels, the ".DQ" series is only ever incremented and never settles back to zero (which is exactly the "metric never goes down" behavior reported), while the canonical series drifts negative. Normalize the queue name with q_name() in before_delay_message so the increment and decrement land on the same label set, matching the queue name the worker uses when re-enqueueing the delayed message. Closes Bogdanp#672
Collaborator
|
You claim
Yet you opened 67 Pull requests with similar claims, on the same day as this one, in a 4-hour period. Please don't include us in your LLM-spam list. |
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.
Closes #672
Root cause
The Prometheus middleware tracks
dramatiq_delayed_messages_inprogressby incrementing the gauge inbefore_delay_messageand decrementing it inbefore_process_message, keyed on(queue_name, actor_name)labels.A delayed message lives on the
".DQ"queue while it waits for its eta, so whenbefore_delay_messagefires the message'squeue_nameis e.g.default.DQ, and the gauge is incremented for the label("default.DQ", actor).When the eta passes,
Worker.handle_delayed_messagesre-enqueues the message onto the canonical queue usingq_name()(which strips the.DQsuffix). The re-enqueued message keeps itsmessage_id, sobefore_process_messagematches it and decrements the gauge — but now the message'squeue_nameisdefault, so the decrement lands on the label("default", actor).Because the increment and decrement use different
queue_namelabels, the".DQ"series is only ever incremented and never settles back to zero (the "metric never goes down" behavior reported in the issue), while the canonical series drifts negative.Reproduction
Driving a real
Workerwith aStubBrokerand the Prometheus middleware, sending one delayed message and letting it run to completion, then dumping every child of the gauge:Observed on
master(before the fix) — the message is delayed ondefault.DQbut processed ondefault, so the two events hit different labels:After the fix (both events recorded against the canonical queue name):
Fix
In
before_delay_message, normalize the queue name with the existingq_name()helper before building the label tuple — the same helper the worker already uses when re-enqueueing the delayed message. This makes the increment and decrement land on the same label set. The change is a one-line label normalization plus the import; it does not alter the id-tracking or any other metric.Tests
Added
test_prometheus_middleware_delayed_messages_inprogress_returns_to_zerointests/middleware/test_prometheus.py. It runs a delayed message through a real worker and asserts everyinprogress_delayed_messageschild returns to0(and that the in-memorydelayed_messagesset does not leak). The test fails onmaster(the('default.DQ', 'do_work')child is stuck at1.0) and passes with this change.The middleware test module and a broad stub/worker/message/common smoke subset pass locally, and
flake8,black,isortandmypyare clean on the modified files.Disclosure: prepared with AI assistance; reviewed and verified locally.