Daya

Alertmanager Configuration

Enhanced Alertmanager configuration with hierarchical routing, multiple receivers, and best practices for production use.

Features

Configuration

Environment Variables

Set these environment variables before starting Alertmanager:

# Slack Configuration
export SLACK_WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"

# Email Configuration (SMTP)
export SMTP_HOST="smtp.gmail.com"
export SMTP_PORT="587"
export SMTP_USER="your-email@gmail.com"
export SMTP_PASSWORD=""
export ALERT_EMAIL_FROM="alerts@yourdomain.com"
export ALERT_EMAIL_TO="team@yourdomain.com"
export ALERT_EMAIL_CRITICAL="oncall@yourdomain.com"
export ALERT_EMAIL_WARNING="devops@yourdomain.com"
export ALERT_EMAIL_ERRORS="backend-team@yourdomain.com"
export ONCALL_EMAIL="oncall@yourdomain.com"
export BACKEND_TEAM_EMAIL="backend@yourdomain.com"

# Webhook Configuration (optional)
export WEBHOOK_URL="https://your-webhook-url.com/alerts"
export WEBHOOK_BEARER_TOKEN=""

# PagerDuty Configuration (optional)
export PAGERDUTY_SERVICE_KEY=""

Using Docker Compose

Update docker-compose.observability.yml to include environment variables:

alertmanager:
  image: prom/alertmanager:latest
  container_name: alertmanager
  environment:
    - SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL}
    - ALERT_EMAIL_TO=${ALERT_EMAIL_TO}
    # ... other variables
  volumes:
    - ./monitoring/alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml:ro
  command:
    - --config.file=/etc/alertmanager/alertmanager.yml
  ports:
    - "9093:9093"

Alert Routing

Critical Alerts

Warning Alerts

Info Alerts

Service-Specific Routes

Inhibition Rules

The configuration includes inhibition rules to reduce alert noise:

  1. Warning suppression: When a critical alert fires, suppress related warning alerts
  2. Info suppression: When warning or critical alerts fire, suppress related info alerts

Receivers

Default Receiver

Critical Alerts Receiver

Critical On-Call Receiver

Warning Alerts Receiver

Info Alerts Receiver

Backend Team Receiver

Error Rate Receiver

Customization

Adding New Routes

Add new routes in the routes section:

routes:
  - match:
      service: your-service
    receiver: your-service-receiver
    continue: true

Adding New Receivers

Add new receivers in the receivers section:

receivers:
  - name: your-receiver
    slack_configs:
      - api_url: '${SLACK_WEBHOOK_URL}'
        channel: '#your-channel'

Custom Templates

Create custom templates in /etc/alertmanager/templates/ and reference them in the templates section.

Testing

Test Alert Configuration

  1. Start Alertmanager:
    docker-compose -f docker-compose.observability.yml up -d alertmanager
    
  2. Access Alertmanager UI:
    http://localhost:9093
    
  3. Test alert routing:
    # Send test alert via Prometheus
    curl -X POST http://localhost:9093/api/v1/alerts -d '[{
      "labels": {
     "alertname": "TestAlert",
     "severity": "critical",
     "service": "github-ai-search-backend"
      },
      "annotations": {
     "summary": "Test alert",
     "description": "This is a test alert"
      }
    }]'
    

Validate Configuration

# Check configuration syntax
docker exec alertmanager amtool check-config /etc/alertmanager/alertmanager.yml

# Test routing
docker exec alertmanager amtool config routes show

Best Practices

  1. Use Environment Variables: Never hardcode sensitive information
  2. Group Related Alerts: Use group_by to reduce notification noise
  3. Set Appropriate Intervals: Balance between alerting speed and noise reduction
  4. Use Inhibition Rules: Suppress lower-priority alerts when critical ones fire
  5. Monitor Alertmanager: Set up alerts for Alertmanager itself
  6. Document Routes: Keep documentation updated when adding new routes
  7. Test Changes: Always test configuration changes in staging first

Troubleshooting

Alerts Not Being Sent

  1. Check Alertmanager logs:
    docker logs alertmanager
    
  2. Verify configuration:
    docker exec alertmanager amtool check-config /etc/alertmanager/alertmanager.yml
    
  3. Check Prometheus is sending alerts:
    # In Prometheus UI: Status -> Targets -> Alertmanagers
    

Too Many Notifications

Missing Notifications

References