---
# System prepended metadata

title: Monitor Setup for Azure Service Bus Alerts and Dead Letter Queue Simulation

---

# Monitor Setup for Azure Service Bus Alerts and Dead Letter Queue Simulation

## Objective
Set up Azure Monitor alerts to track queue length and dead-lettered messages for an Azure Service Bus queue. Simulate and trigger alerts to understand monitoring capabilities.

---

## Prerequisites
1. Azure Subscription with access to Service Bus and Azure Monitor.
2. Service Bus namespace and queue details:
   - Namespace: **ordernamespace**
   - Queue: **orderqueue**
3. Tools:
   - Azure Portal
   - Service Bus Explorer
   - Code samples from:
     - [ASBDLQDemo](https://github.com/CodeCraftersGPT/ASBDLQDemo)
     - [ASBQSender](https://github.com/CodeCraftersGPT/ASBQSender)

---

## Steps

### 1. Create Service Bus Namespace and Queue
1. Log in to the [Azure Portal](https://portal.azure.com).
2. Navigate to **Service Bus** and create a new namespace:
   - **Name**: ordernamespace
   - **Pricing Tier**: Standard
   - **Region**: East US/Select closest to you.
3. Inside the namespace, create a new queue:
   - **Name**: orderqueue
   - **Max Delivery Count**: 3
   - **Enable Dead Letter Queue (DLQ)**: Enabled.
4. Save the queue settings.

---

### 2. Configure Azure Monitor Alerts
1. Navigate to **Azure Monitor** in the Azure Portal.
2. Select **Alerts** and click **+ New Alert Rule**.
3. Define the scope:
   - Select the Service Bus **ordernamespace** or the specific **orderqueue**.
4. Add conditions:
   - **Queue Length Alert**:
     - **Metric**: Queue Length
     - **Condition**: Greater than 10
     - **Action**: Send email to `kumar@jatanix.com` / your email.
   - **Dead Letter Queue Alert**:
     - **Metric**: Dead Letter Messages
     - **Condition**: Greater than 2
     - **Action**: Send email to `kumar@jatanix.com` / your email.
5. Save and activate the alerts.

---

### 3. Send Messages to the Queue
1. Clone the [ASBQSender repository](https://github.com/CodeCraftersGPT/ASBQSender) locally.
2. Replace the connection string and queue name in the configuration file.
3. Run the sender application to send **15 messages** to the queue:
   - Commands:
     ```bash
     dotnet run
     ```
4. Verify the messages in the queue using **Service Bus Explorer**.

---

### 4. Simulate Dead Letter Queue
1. Clone the [ASBDLQDemo repository](https://github.com/CodeCraftersGPT/ASBDLQDemo) locally.
2. Replace the connection string and queue name in the source code with the details of **ordernamespace** and **orderqueue**.
3. Run the simulation to send and fail two messages, causing them to be moved to the DLQ.

---

### 5. Observe Alerts
1. Monitor the Azure Portal for alerts triggered by:
   - Queue length exceeding 10.
   - Dead-lettered messages exceeding 2.
2. Verify email notifications sent to `kumar@jatanix.com`.

---

## Expected Outcome
1. Alerts are triggered when thresholds are breached.
2. Email notifications are sent to the specified email address.
3. Messages can be observed in **Service Bus Explorer**, including the DLQ.

---

## Notes
- Ensure proper permissions for Azure Monitor and Service Bus.
- Delete or disable alerts post-demo to avoid unintended notifications.
- Ensure Azure CLI or Portal is used correctly for setting up the environment.

---

## References
1. [Azure Service Bus Documentation](https://learn.microsoft.com/en-us/azure/service-bus-messaging/)
2. [Azure Monitor Documentation](https://learn.microsoft.com/en-us/azure/azure-monitor/)
3. [ASBDLQDemo Repository](https://github.com/CodeCraftersGPT/ASBDLQDemo)
4. [ASBQSender Repository](https://github.com/CodeCraftersGPT/ASBQSender)
