Alertmanager: Routing & Grouping
Prometheus decides that something is wrong; Alertmanager decides who hears about it, how, and how often. It receives alerts from one or more Prometheus servers (or Loki, or Grafana), then:
- groups related alerts into one notification (50 pods failing = one message, not 50);
- routes each group through a tree of matchers to a receiver (Slack, PagerDuty, email, webhook);
- deduplicates (two HA Prometheus servers send the same alerts) and throttles repeats;
- applies inhibitions and silences (next lesson).
The routing tree
route: # the root route: every alert enters here
receiver: team-links-slack # fallback receiver
group_by: [alertname, service]
group_wait: 30s # wait for more alerts before the first notification of a new group
group_interval: 5m # wait before notifying about changes to an existing group
repeat_interval: 4h # re-send an unchanged, still-firing group
routes:
- matchers: [severity="page"]
receiver: oncall-pagerduty
continue: true # keep matching siblings after this route
- matchers: [team="db"]
receiver: db-slack
group_by: [alertname, cluster]
- matchers: [alertname="Watchdog"]
receiver: deadmanssnitch
repeat_interval: 1m- An alert walks down from the root and takes the first child route whose
matchersall match (=,!=,=~,!~, Prometheus syntax), recursing into that child's ownroutes. If no child matches, the current node handles it. continue: truemeans "match here and keep checking the next siblings": the usual way to page and post to a channel.group_by, timers andreceiverare inherited by children unless overridden.group_by: ['...']disables grouping (each alert alone): avoid it.mute_time_intervals/active_time_intervals(with namedtime_intervals) silence a route outside business hours: use them for ticket-level routes, never for pages.
Checking and testing a config offline
amtool (shipped with Alertmanager) validates the file, draws the tree and, most usefully, tells you which receivers an alert with given labels would reach:
cat > alertmanager.yml <<'YML'
route:
receiver: team-links-slack
group_by: [alertname, service]
routes:
- matchers: [severity="page"]
receiver: oncall-pagerduty
continue: true
- matchers: [team="db"]
receiver: db-slack
- matchers: [severity="ticket"]
receiver: team-links-tickets
receivers:
- name: team-links-slack
slack_configs:
- api_url_file: /etc/alertmanager/secrets/slack-links
channel: '#links-alerts'
send_resolved: true
- name: db-slack
slack_configs:
- api_url_file: /etc/alertmanager/secrets/slack-db
channel: '#db-alerts'
- name: oncall-pagerduty
pagerduty_configs:
- routing_key_file: /etc/alertmanager/secrets/pagerduty-links
- name: team-links-tickets
webhook_configs:
- url: http://ticket-bridge.internal:8080/alerts
YML
amtool check-config alertmanager.yml
amtool config routes show --config.file=alertmanager.yml
echo "--- where do these alerts go?"
amtool config routes test --config.file=alertmanager.yml severity=page service=linkly
amtool config routes test --config.file=alertmanager.yml severity=page team=db
amtool config routes test --config.file=alertmanager.yml severity=ticket team=db
amtool config routes test --config.file=alertmanager.yml alertname=UnknownChecking 'alertmanager.yml' SUCCESS
Found:
- global config
- route
- 0 inhibit rules
- 4 receivers
- 0 templates
Routing tree:
.
└── default-route receiver: team-links-slack
├── {severity="page"} continue: true receiver: oncall-pagerduty
├── {team="db"} receiver: db-slack
└── {severity="ticket"} receiver: team-links-tickets
--- where do these alerts go?
oncall-pagerduty
oncall-pagerduty,db-slack
db-slack
team-links-slack
Reading the answers:
- A
pagefor linkly goes to PagerDuty only.continue: truelet it try the siblings, but none matched, so the root's Slack fallback did not apply: the fallback is only used when no child route matches at all. If you want every page in Slack too, add a route for it after the PagerDuty one. - A
pagefor the database team reaches PagerDuty and the DB channel, thanks tocontinue. - A database
ticketstops at the first match (team="db"), so it never reaches the ticket webhook: order matters. - Anything unmatched falls through to the root receiver.
--verify.receivers turns a routing test into an assertion with an exit code, so you can keep a table of "these labels must reach these receivers" in CI:
cat > alertmanager.yml <<'YML'
route:
receiver: team-links-slack
routes:
- matchers: [severity="page"]
receiver: oncall-pagerduty
continue: true
- matchers: [team="db"]
receiver: db-slack
- matchers: [severity="ticket"]
receiver: team-links-tickets
receivers:
- name: team-links-slack
- name: oncall-pagerduty
- name: db-slack
- name: team-links-tickets
YML
cat > routing-cases.txt <<'TXT'
oncall-pagerduty severity=page service=linkly
oncall-pagerduty,db-slack severity=page team=db
team-links-slack severity=warning service=linkly
team-links-tickets severity=ticket service=linkly
team-links-tickets severity=ticket team=db
TXT
fail=0
while read -r expected labels; do
if amtool config routes test --config.file=alertmanager.yml --verify.receivers="$expected" $labels >/dev/null; then
echo "ok $labels -> $expected"
else
echo "FAIL $labels -> expected $expected"; fail=1
fi
done < routing-cases.txt
exit $failok severity=page service=linkly -> oncall-pagerduty
ok severity=page team=db -> oncall-pagerduty,db-slack
ok severity=warning service=linkly -> team-links-slack
ok severity=ticket service=linkly -> team-links-tickets
FAIL severity=ticket team=db -> expected team-links-tickets
The last case is the ordering bug from above, now caught automatically: the team wants database tickets in the ticket system, but the team="db" route catches them first. Moving the ticket route above it makes the table pass. (Receivers without any *_configs are valid; they simply drop notifications, which is handy for tests and for a "blackhole" route.)
Grouping and timers in practice
| Setting | Effect | Typical |
|---|---|---|
group_by |
alerts with equal values for these labels share one notification | [alertname, service] or [alertname, cluster, namespace] |
group_wait |
delay before the first notification of a new group, to collect its siblings | 30 s (10 s for pages) |
group_interval |
delay before sending an updated group (new alert joined, one resolved) | 5 m |
repeat_interval |
re-send an unchanged firing group | 4 h for pages, 12–24 h for tickets |
So a new incident takes group_wait plus Prometheus's for plus the rule evaluation interval to reach a phone. Put that number in your runbook so nobody wonders why the page came "late".
Running it
Alertmanager is one binary; in Kubernetes it comes with kube-prometheus-stack. Run at least two replicas with clustering (--cluster.peer): they gossip silences and notification state so that each notification is sent once. Point every Prometheus at all Alertmanager replicas; never put a load balancer between them.
Test yourself
An alert matches the first child route, which has no continue. What happens with the later sibling routes?
- They are evaluated too
- They are skipped: routing stops at the first matching child
- The root receiver also gets it
- It depends on group_by
Which setting decides how long Alertmanager waits to collect alerts for a brand-new group?
- repeat_interval
- group_interval
- group_wait
- evaluation_interval
How do you check in CI that severity=ticket alerts never reach PagerDuty?
- Send a test page
amtool config routes test --verify.receivers=… severity=ticket …and check the exit codepromtool test rules- Grep the YAML