Step 4: Setting Up Alerts
Learn how to get notified when things go wrong
๐ What Are Alerts?
Alerts notify you when your application has problems:
| Alert | When It Fires |
|---|---|
| DjangoDown | Django is unreachable |
| High5xxRate | Too many server errors |
| HighCPUUsage | CPU usage > 85% |
| DBSlowQueries | Database queries taking too long |
๐ How Alerts Work
- Prometheus evaluates alert rules every 15 seconds
- If condition met, alert is sent to Alertmanager
- Alertmanager groups and routes alerts
- Slack receives formatted notification
๐ Default Alert Rules
This stack includes these alerts:
Database Alerts
| Alert | Condition | Severity |
|---|---|---|
DBHighErrorRate |
DB errors > 0.1/s | Critical |
DBSlowQueries |
p95 query time > 1s | Warning |
DBHighQueryRate |
Queries > 100/s | Warning |
HTTP Alerts
| Alert | Condition | Severity |
|---|---|---|
DjangoDown |
Target unreachable | Critical |
Django5xxError |
Any 5xx response | Critical |
Django4xxError |
Any 4xx response | Warning |
HighHTTP5xxRate |
5xx errors > 0.5/s | Critical |
Infrastructure Alerts
| Alert | Condition | Severity |
|---|---|---|
HighCPUUsage |
CPU > 85% | Warning |
CriticalCPUUsage |
CPU > 95% | Critical |
HighMemoryUsage |
Memory > 85% | Warning |
HighDiskUsage |
Disk > 85% | Warning |
๐งช Testing Alerts
View Alert Rules in Prometheus
- Open http://localhost:9090/alerts
- You'll see all configured alert rules
- Check their current state (Inactive/Firing)
View Alertmanager
- Open http://localhost:9093
- You'll see alert routing configuration
- Check active alerts
Trigger a Test Alert
# Stop Django to trigger DjangoDown alert
docker compose -f django_app/docker-compose.yml stop obs-django
# Wait 30 seconds, then check Alertmanager
curl http://localhost:9093/api/v1/alerts | jq
# Start Django again
docker compose -f django_app/docker-compose.yml start obs-django
โ๏ธ Configuring Slack Alerts
Step 1: Get Slack Webhook URL
- Go to https://api.slack.com/apps
- Create a new app
- Add "Incoming Webhooks" feature
- Create webhook for your channel
- Copy the webhook URL
Step 2: Add to Environment
Edit django_app/.env:
Step 3: Restart Alertmanager
Step 4: Test Alert
# Stop Django to trigger alert
docker compose -f django_app/docker-compose.yml stop obs-django
# Check Slack for notification!
๐ง Troubleshooting
Alerts not firing
Check Prometheus rules:
Check alert state:
Slack notifications not working
Check webhook URL:
Test webhook manually:
๐ What You Learned
In this step, you learned:
- โ How alerts work in the stack
- โ Default alert rules configured
- โ How to test alerts
- โ How to configure Slack notifications
๐ Next Steps
Now that you understand alerts, let's create dashboards:
- Alertmanager Module - Advanced alert routing
- Prometheus Module - Create custom alerts
Ready for Step 5? ๐