How do I listen to the topic ‘amq.rabbitmq.trace’ using rabbitmq and python?

As I understand, the topic ‘amq.rabbitmq.trace’ is for tracing all messages sent to rabbitmq, without affecting the existing consumers for that message.

I am using the following code

import pickle

import pika

TOPIC_EXCHANGE = 'amq.rabbitmq.trace'


def handle_message(channel, method, properties, body):
    # Handle the message here
    print("Received message:", pickle.loads(body))


def main():
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.exchange_declare(exchange=TOPIC_EXCHANGE, exchange_type="topic", durable=True, internal=True)
    queue_name = channel.queue_declare(queue="", durable=True).method.queue
    channel.queue_bind(exchange=TOPIC_EXCHANGE, queue=queue_name, routing_key='#')
    channel.basic_consume(queue=queue_name, on_message_callback=handle_message, auto_ack=True)

    print("Waiting for messages...")
    channel.start_consuming()


if __name__ == '__main__':
    main()

This ends up receiving no messages.

If, instead of the temporary queue, I use the queue name already being used by another consumer, this program and the original consumer do receive the messages. But that happens in a round-robin.

So, how do I get all events, without affecting current consumers?

Read more here: Source link