28

We're using amqplib to publish/consume messages. I want to be able to read the number of messages on a queue (ideally both acknowledged and unacknowledged). This will allow me to show a nice status diagram to the admin users and detect if a certain component is not keeping up with the load.

I can't find any information in the amqplib docs about reading queue status.

Can someone point me in the right direction?

Basic
  • 26,321
  • 24
  • 115
  • 201
  • 1
    Check this answer http://stackoverflow.com/questions/8192584/get-queue-size-in-pika-amqp-python/13629296#13629296 – mike Nov 13 '15 at 11:21
  • Thanks @mike, that's largely what I ended up doing when I had to reimplement some of this in C#. For the Python approach, I ended up hitting the rabbitmq-admin plugin and querying that instead. In any case, I appreciate the pointer. – Basic Nov 13 '15 at 13:07

3 Answers3

60

Using pika:

import pika

pika_conn_params = pika.ConnectionParameters(
    host='localhost', port=5672,
    credentials=pika.credentials.PlainCredentials('guest', 'guest'),
)
connection = pika.BlockingConnection(pika_conn_params)
channel = connection.channel()
queue = channel.queue_declare(
    queue="your_queue", durable=True,
    exclusive=False, auto_delete=False
)

print(queue.method.message_count)

Using PyRabbit:

from pyrabbit.api import Client
cl = Client('localhost:55672', 'guest', 'guest')
cl.get_messages('example_vhost', 'example_queue')[0]['message_count']

Using HTTP

Syntax:

curl -i -u user:password http://localhost:15672/api/queues/vhost/queue

Example:

curl -i -u guest:guest http://localhost:15672/api/queues/%2f/celery           

Note: Default vhost is / which needs to be escaped as %2f

Using CLI:

$ sudo rabbitmqctl list_queues | grep 'my_queue'
Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
  • 3
    The `PyRabbit` solution retrieves a message from the queue. I think you want to use `cl.get_queue("example_vhost", "example_queue")['messages']` instead. – Bill Jul 26 '17 at 17:37
  • 4
    How would you get the number of unack'd messages in a queue? – DUDANF Sep 05 '19 at 12:51
  • For pika: Just leaving this here because it bit me and cost me a couple of hours. It's important to understand that this is only the "true" number of messages if the consumers haven't prefetched too many messages. Refer to https://pika.readthedocs.io/en/stable/modules/channel.html#pika.channel.Channel.basic_qos for the `prefetch_count`. – Tobias Jan 09 '20 at 21:59
  • 1
    I believe queue declare with pika should be done this way: `queue = channel.queue_declare(queue="your_queue", passive=True)` This won't fail if say, the durable value of your declare statement mismatches that of the currently existing queue. – M. Burkard Mar 03 '22 at 21:35
8

following the answer of ChillarAnand you can get the value easily. the data is in the object.

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
            host='localhost',
            port=5672,
            credentials=pika.credentials.PlainCredentials('guest', 'guest'),
        )
channel = connection.channel()
print(channel.queue_declare(queue="your_queue", durable=True,  exclusive=False,
                  auto_delete=False).method.message_count)

and you will get the exact message number

OhadBasan
  • 797
  • 6
  • 15
2

Using the Java API, you can do the following :

channel.queueDeclarePassive(queueName).getMessageCount()

I believe this is also available with amqplib (according to https://code.google.com/p/py-amqplib/source/browse/amqplib/client_0_8/channel.py#1356 it seems that queue_declare() returns a tuple with the message count)

If you need more precise metrics (especially nack message count), you need to use rabbitmqctl or rabbitmq_management. Rabbitmq_management is probably a good choice due to its HTTP API. More info : http://www.rabbitmq.com/management.html

mguillermin
  • 4,143
  • 19
  • 25