Alertmanager Routing, Grouping and Silences, Hands-On
In the last lesson, ShopDemoDown reached firing (1) on the Prometheus Alerts page, and nothing else happened. A real alert has to reach a person, such as the on-call engineer: once rather than every minute, and not during planned maintenance. That is the job of Alertmanager. This lesson adds it to the lab, shows how routes, groups, silences and inhibit rules decide who is told, and shows what a receiver gets.
Prometheus detects, Alertmanager decides
Prometheus evaluates alerting rules and decides that something is wrong. Alertmanager, a separate server from the Prometheus project on port 9093 in the lab, receives those firing alerts and decides who is told, how often, and what is held back. Several Prometheus servers can share one, keeping notification settings in one place.
What it sends is a notification: one message about one or more alerts, delivered to a receiver, a named destination such as a pager service, an email address, a chat channel or a webhook. A webhook is an HTTP POST of JSON to a URL you choose.
Alertmanager identifies each alert by its labels, so the once-a-minute resends from lesson 7 update the same alert instead of causing new messages.
The config: route tree, receivers and inhibit rules
Alertmanager reads one YAML file, in the lab alertmanager/alertmanager.yml. The route tree picks a receiver for each alert, receivers say how to deliver, and inhibit_rules mute some alerts while others fire. Here it is without comments and with one of three receivers, which differ only in name:
route:
receiver: default
group_by: [alertname, job]
group_wait: 30s
group_interval: 1m
repeat_interval: 10m
routes:
- matchers:
- severity="page"
receiver: pager
- matchers:
- severity="ticket"
receiver: email
receivers:
- name: pager
webhook_configs:
- url: http://webhook:5001/
inhibit_rules:
- source_matchers:
- severity="page"
target_matchers:
- severity="ticket"
equal: [job]
A route picks alerts with matchers, label tests written like the label matchers from lesson 5. severity="page" matches alerts whose severity label is exactly page, and =~ takes a regular expression. ShopDemoDown carries severity: page from its rule, so it goes to pager.
Every alert enters at the top-level root route, which must match all alerts and so has no matchers. Alertmanager tries the child routes in order and stops at the first match, unless that child sets continue: true. An alert that no child matches uses the root's receiver, default. Children inherit settings they leave out, such as group_by.
In the lab, all three receivers post to the webhook container. In production, pager would use pagerduty_configs and email email_configs (see the configuration reference); the routing stays the same.
Grouping: ten alerts, one message
Ten servers failing at once should not mean ten pages. Grouping puts alerts that share the values of the labels in group_by into one group, and each notification covers the whole group. With group_by: [alertname, job], ShopDemoDown on ten shop-demo instances is one message listing ten alerts.
Three timers, set on the route, decide when a group's messages leave:
group_wait: after a new group's first alert, how long to wait for others before the first message. Default 30s.group_interval: after a message, how long to wait before the next one about changes, such as a new or resolved alert. Default 5m.repeat_interval: how long before resending a message for a group still firing with no change. Default 4h.
The lab shortens the last two to 1m and 10m so you see results quickly; real systems keep the defaults or longer (see Alerting Best Practices). The timers add to the rule's for: ShopDemoDown fires about 90 seconds after the app stops, and its first message leaves 30 seconds later.
Remember the loop: silences and inhibit rules are checked before every message, so muting an alert that already paged you still stops the repeats.
Silences and inhibition: holding alerts back
A silence mutes, for a set time, every alert whose labels match all of its matchers. It has a start, an end, a creator and a comment saying why. You create silences while Alertmanager runs, not in its config file, and they expire on their own. Prometheus keeps firing the alert; Alertmanager only stops the messages and marks it suppressed.
In the web UI at http://localhost:9093, click Silence on an alert: the New Silence form opens with the alert's labels as matchers. Set the Duration, fill in Comment, click Preview Alerts, then Create. amtool, Alertmanager's command-line tool, does the same with amtool silence add.
An inhibit rule mutes alerts automatically while another alert fires. The lab's rule says: while an alert matching source_matchers (severity="page") fires, mute alerts matching target_matchers (severity="ticket") whose labels listed in equal, here job, have the same values. While shop-demo is down, a ticket about its slow checkouts adds nothing, so it stays muted until ShopDemoDown resolves. Without equal, any page would mute every ticket, from any service. The timeline shows grouping and a silence together.
Connect the lab: Alertmanager, webhook and Prometheus
The lab in course-labs/open-source-observability-stack/02-prometheus-alertmanager already runs two more services. alertmanager runs prom/alertmanager:v0.35.1 on port 9093, with its config from the alertmanager folder and silences in the alertmanager-data volume.
webhook runs webhook/webhook.py, a short Python program on port 5001 inside the lab network. It prints the full JSON of every notification: the data Alertmanager would turn into a Slack message or a page. If docker compose ps lists neither service, run docker compose up -d.
Step 1: check the Alertmanager config
amtool, included in the Alertmanager image, has a check-config command that parses the file and lists what it found, without touching the running server:
docker compose exec alertmanager amtool check-config /etc/alertmanager/alertmanager.yml
A valid file prints SUCCESS, then a summary matching the file: the route tree, one inhibit rule and three receivers.
Checking '/etc/alertmanager/alertmanager.yml' SUCCESS
Found:
- global config
- route
- 1 inhibit rules
- 3 receivers
- 0 templates
A mistake prints FAILED: and the reason, such as an unknown field. After later edits, curl -X POST localhost:9093/-/reload applies the file without a restart.
Step 2: send Prometheus's alerts to Alertmanager
Prometheus sends alerts only to the Alertmanagers listed under alerting. Add this block to prometheus/prometheus.yml, between global and scrape_configs like rule_files in lesson 7; the service name works as a host name:
alerting:
alertmanagers:
- static_configs:
- targets: ["alertmanager:9093"]
Run the promtool check and reload from lesson 4. Then Status, then Alertmanager discovery (http://localhost:9090/alertmanager-discovery) lists http://alertmanager:9093/api/v2/alerts under Active Alertmanagers. No active alertmanagers means the reload did not happen or the block is mis-indented.
Check yourself
A second shop-demo instance goes down 10 seconds after the first. How many pager messages arrive at first?
One. Both alerts share alertname and job, so they join one group, and the second arrives inside the 30-second group_wait, so the first message lists both.
During planned maintenance, why silence ShopDemoDown instead of deleting its rule from Prometheus?
A silence ends on its own and its comment says why, so alerting returns without anyone restoring a rule. Prometheus also keeps evaluating the rule, so the alert history stays complete.
While ShopDemoDown fires, a ticket-level alert fires with job="db". Is it inhibited?
No. The rule only mutes targets whose job equals the source alert's, and db is not shop-demo, so the ticket goes to email as usual.