Monitor RabbitMQ queue length from Elixir?

Has anybody used Elixir to check the length of a RabbitMQ queue? It can be done with declare_queue, but it’s ugly - you have to pass the exact option you originally declared the queue with, plus :passive.

The use case is to detect when the system is backing up and respond accordingly.

1 Like

If you can use GenRmq then it has message count, message_count functions.

1 Like

@collegeimprovements Oh thanks - that led me to the underlying function: https://hexdocs.pm/amqp/1.4.2/AMQP.Queue.html#message_count/2 . Works great!

1 Like

Glad to see this :slight_smile:

For anyone who stumbles on this later: AMQP only supports getting the number of messages that are “Ready”, not those that are “unacked”. For the latter, you might have to use RabbitMQ’s REST API. See https://stackoverflow.com/questions/63018151/can-i-see-the-count-of-unacked-messages-in-rabbitmq-via-an-amqp-client

My system that consumes from the queue is using Broadway. In normal load, I basically never see any messages as “ready” because they’re quickly grabbed up by my Broadway pipeline. By queueing a lot of messages and putting a Process.sleep(:infinity) in my handle_message, I can see that my system will have at most N unacked messages at a time. So if I see any messages in the “ready” count, I can basically assume that there are N more being handled at the moment, which is a good enough indicator to decide whether the system is getting overloaded.

1 Like