How to Retrieve RabbitMQ Queue Consumer Count via HTTP API (When consumer_details is Empty)


12 views

When working with RabbitMQ's management API, you might encounter a situation where rabbitmqctl reports consumers but the HTTP API returns an empty consumer_details array. This typically occurs because:

GET /api/queues/%2F/your-queue-name

The HTTP API behavior depends on several factors:

  • RabbitMQ version (3.8+ has improved consumer tracking)
  • Management plugin configuration
  • Permission settings for the API user

If consumer_details remains empty, try these methods:

Method 1: Use the Consumers Endpoint

GET /api/consumers
# Then filter by queue name programmatically

Method 2: Check Queue Metrics

GET /api/queues/%2F/your-queue-name
# Look for "consumer_utilisation" or "consumers" fields

Method 3: Direct Node Query

GET /api/nodes/rabbit@your-node
# Check connection counts that might indicate consumers
import requests
from requests.auth import HTTPBasicAuth

def get_queue_consumers(host, queue_name, vhost="%2F"):
    url = f"http://{host}:15672/api/queues/{vhost}/{queue_name}"
    response = requests.get(
        url,
        auth=HTTPBasicAuth("guest", "guest"),
        headers={"Content-Type": "application/json"}
    )
    
    if response.status_code == 200:
        data = response.json()
        # Try different fields
        return {
            "consumers": data.get("consumers", 0),
            "consumer_details": data.get("consumer_details", []),
            "consumer_utilisation": data.get("consumer_utilisation", 0.0)
        }
    return None
  • Verify the user has monitoring tag in RabbitMQ
  • Check for firewalls blocking the management port (15672)
  • Try with different API clients (curl, Postman) to isolate issues

Many developers encounter this situation when working with RabbitMQ's HTTP API - the list_queues command through rabbitmqctl shows consumers, but the API returns an empty consumer_details array. This typically happens due to permission issues or API version differences.

The HTTP API requires proper authentication headers. Unlike rabbitmqctl which runs locally with admin privileges, the HTTP API needs explicit permissions:

GET /api/queues/%2F/q-firewall-plugin HTTP/1.1
Host: rabbitmq-server:15672
Authorization: Basic dXNlcjpwYXNzd29yZA==
Accept: application/json

For detailed consumer information, you should use the dedicated consumers endpoint:

GET /api/queues/%2F/q-firewall-plugin/consumers

This will return an array with all consumer details, including:

  • Consumer tag
  • Channel details
  • Connection information
  • Queue name

Here's how to properly query consumer count using Python:

import requests
from requests.auth import HTTPBasicAuth

url = "http://rabbitmq-server:15672/api/queues/%2F/q-firewall-plugin"
response = requests.get(
    url,
    auth=HTTPBasicAuth('user', 'password'),
    headers={'Content-Type': 'application/json'}
)

if response.status_code == 200:
    queue_data = response.json()
    print(f"Consumer count: {queue_data.get('consumers', 0)}")
else:
    print(f"Error: {response.status_code} - {response.text}")

If you're still getting empty consumer details:

  1. Verify the user has monitoring tag in RabbitMQ
  2. Check if the management plugin is properly installed
  3. Ensure you're hitting the correct node if in a cluster
  4. Try the /api/consumers endpoint for system-wide view

For more robust access, consider the rabbitmq-admin package:

from rabbitmq_admin import AdminAPI

api = AdminAPI(url='http://localhost:15672', auth=('user', 'password'))
queue = api.get_queue(vhost='%2F', name='q-firewall-plugin')
print(queue.consumers)