Not a customer? Click the 'Start a free trial' link to begin a 30-day SaaS trial of our product and to join our community.
Existing Cisco AppDynamics customers should click the 'Sign In' button to authenticate to access the community
on 12-04-2024 11:56 AM
RabbitMQ is a powerful open-source message broker that supports a variety of messaging protocols, including AMQP. It allows developers to build robust, scalable, and asynchronous messaging systems. However, to ensure optimal performance, monitoring RabbitMQ metrics is crucial. This tutorial walks you through setting up RabbitMQ, integrating it with a Python application, and monitoring its metrics using AppDynamics.
To quickly get RabbitMQ up and running, use the official RabbitMQ Docker image with the management plugin enabled.
Run the following command to start RabbitMQ:
docker run -d --hostname my-rabbit --name rabbitmq \
-e RABBITMQ_DEFAULT_USER=guest \
-e RABBITMQ_DEFAULT_PASS=guest \
-p 5672:5672 -p 15672:15672 \
rabbitmq:management
http://localhost:15672
.Username: guest
Password: guest
Once the container is running, verify the RabbitMQ server by accessing the Management Console in your browser. Alternatively, test the API endpoint:
curl -u guest:guest http://localhost:15672/api/overview
This should return RabbitMQ metrics in JSON format.
Install the pika
library for Python, which is used to interact with RabbitMQ:
pip install pika
2.2 Create the Producer Script (send.py
)
This script connects to RabbitMQ, declares a queue, and sends a message.
import pika
# Connect to RabbitMQ
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# Declare a queue
channel.queue_declare(queue='hello')
# Publish a message
channel.basic_publish(exchange='', routing_key='hello', body='Hello RabbitMQ!')
print(" [x] Sent 'Hello RabbitMQ!'")
connection.close()
receive.py
)This script connects to RabbitMQ, consumes messages from the queue, and prints them.
import pika
# Connect to RabbitMQ
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()
# Declare a queue
channel.queue_declare(queue='hello')
# Define a callback to process messages
def callback(ch, method, properties, body):
print(f" [x] Received {body}")
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
2.4 Test the Application
a. Run the consumer in one terminal:
python3 receive.py
b. Send a message from another terminal:
python3 send.py
c. Observe the message output in the consumer terminal.
[x] Sent 'Hello RabbitMQ!'
[x] Received b'Hello RabbitMQ!'
Ensure that the RabbitMQ Management Plugin is enabled (default in the Docker image). It exposes an HTTP API that provides metrics.
Use a shell script to fetch RabbitMQ metrics and send them to the AppDynamics Machine Agent.
script.sh
#!/bin/bash
# RabbitMQ Management API credentials
USERNAME="guest"
PASSWORD="guest"
URL="http://localhost:15672/api/overview"
# Fetch metrics from RabbitMQ Management API
RESPONSE=$(curl -s -u $USERNAME:$PASSWORD $URL)
if [[ $? -ne 0 || -z "$RESPONSE" ]]; then
echo "Error: Unable to fetch RabbitMQ metrics"
exit 1
fi
MESSAGES=$(echo "$RESPONSE" | jq '.queue_totals.messages // 0')
MESSAGES_READY=$(echo "$RESPONSE" | jq '.queue_totals.messages_ready // 0')
DELIVER_GET=$(echo "$RESPONSE" | jq '.message_stats.deliver_get // 0')
echo "name=Custom Metrics|RabbitMQ|Total Messages, value=$MESSAGES"
echo "name=Custom Metrics|RabbitMQ|Messages Ready, value=$MESSAGES_READY"
echo "name=Custom Metrics|RabbitMQ|Deliver Get, value=$DELIVER_GET"
script.sh
script to the Machine Agent monitors directory:cp script.sh <MachineAgent_Dir>/monitors/RabbitMQMonitor/
2. Create monitor.xml
: Create a monitor.xml
file to configure the Machine Agent:
<monitor>
<name>RabbitMQ</name>
<type>managed</type>
<enabled>true</enabled>
<enable-override os-type="linux">true</enable-override>
<description>RabbitMQ
</description>
<monitor-configuration>
</monitor-configuration>
<monitor-run-task>
<execution-style>periodic</execution-style>
<name>Run</name>
<type>executable</type>
<task-arguments>
</task-arguments>
<executable-task>
<type>file</type>
<file>script.sh</file>
</executable-task>
</monitor-run-task>
</monitor>
3. Restart the Machine Agent: Restart the agent to apply the changes:
cd <MachineAgent_Dir>/bin
./machine-agent &
You should see metrics like:
Thank you! Your submission has been received!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form