Alerting Rules
for, labels, annotations, templates, and unit tests
An alerting rule is a PromQL expression plus a name. Every series the expression returns is an active alert. If it returns nothing, nothing is wrong. That's the whole model, and it's why comparison operators filter instead of returning true/false.
Anatomy
- alert: TargetDown
expr: up == 0
for: 2m
labels:
severity: warning
annotations:
summary: "{{ $labels.job }} target {{ $labels.instance }} is down"
description: "Prometheus has failed to scrape {{ $labels.instance }} for 2 minutes."The alert's labels are the series' labels plus the ones you add. So
TargetDown for the demo app carries job="demo-app",
instance="demo-app:8080", severity="warning", and
alertname="TargetDown". Alertmanager routes and groups on those.
Alert states
| State | Meaning |
|---|---|
| Inactive | The expression returns nothing for this series |
| Pending | It returned something, but for less than for |
| Firing | It has returned something for at least for; sent to Alertmanager |
Prometheus re-sends firing alerts to Alertmanager on every evaluation (they expire if not refreshed), and sends a resolved notification when the expression stops returning the series.
# every active alert is also a series you can query:
ALERTS{alertstate="firing"}
ALERTS_FOR_STATE # when each pending/firing alert became activeChoosing for
for trades speed for noise. With for: 0 an alert fires on a single
bad evaluation, which can come from one slow scrape. Too long, and you
learn about a real outage late.
- Symptom alerts on rates:
forof a few minutes, or none if the expression already uses a long window, like burn-rate alerts. up == 0: 1 to 5 minutes.- Slow-moving capacity alerts (disk, certs): 15 minutes or more.
A for timer resets if the expression stops returning even once.
An error rate hovering around the threshold may never fire. That's what
keep_firing_for and longer rate() windows are for.
Templates
Annotations (and labels) are Go templates:
Don't put $value in labels. Labels identify the alert; if a label
changes, Alertmanager sees a brand-new alert, resolves the old one, and
re-notifies. Values belong in annotations only.
What makes a good alert
Every page should be urgent, actionable, and real. If the person paged can't or needn't do anything right now, it isn't a page.
- Alert on symptoms, not causes. "Users are getting errors" (error ratio, latency, SLO burn) rather than "CPU is at 90%". High CPU with happy users is not an incident; an outage with idle CPU is.
- Severity drives routing.
criticalpages a human now.warningcreates a ticket for working hours. Nothing in between. - Every alert links to a runbook. Add a
runbook_urlannotation with what to check and what to do. - Include enough context to start. Which service, which instance, how bad, a dashboard link.
Unit testing rules
Alert rules are code, so test them. promtool test rules feeds synthetic
series through your rules and checks which alerts fire, and when.
rule_files:
- ../rules/alerts.yml
- ../rules/recording.yml
evaluation_interval: 15s
tests:
- interval: 15s
input_series:
- series: 'up{job="demo-app", instance="demo-app:8080"}'
values: "1 1 1 0 0 0 0 0 0 0 0 0 0" # down from the 4th sample (45s)
alert_rule_test:
- eval_time: 1m # down for 15s: still pending
alertname: TargetDown
exp_alerts: []
- eval_time: 3m # down for 2m15s: firing
alertname: TargetDown
exp_alerts:
- exp_labels: { severity: warning, job: demo-app, instance: demo-app:8080 }The values notation is compact: "0+80x90" means start at 0 and add 80
each step, 90 times. That's how the second test builds a counter with
20% errors to trigger the burn-rate alert.
docker exec prometheus promtool test rules /etc/prometheus/tests/alerts.test.ymlRun this in CI on every change to rules/. A broken alert is worse than
none, because everyone believes it's covering them.
Meta-alerts
Two alerts every Prometheus should have, both in this stack:
Watchdog:expr: vector(1), always firing. Routed to an external "dead man's switch" service that pages if the heartbeat stops arriving. It's the only way to detect that Prometheus, Alertmanager, or the network between them is down, because a dead alerting pipeline can't send an alert about itself.PrometheusRuleEvaluationFailures: a rule that errors (a bad regex, many-to-many matching) silently evaluates to nothing. This catches it.
Lab
- Inject errors:
curl -X POST 'localhost:8080/admin/chaos?error_rate=0.3'. - Open http://localhost:9090/alerts. Watch
ErrorBudgetBurnFastgo from inactive to firing. It needs the 1h error ratio above 7.2%. Only some routes fail, so 30% chaos means roughly 20% errors overall, and the healthy traffic from earlier in the hour dilutes the window. Expect it to take around 20 minutes. Work out why from the expression. docker logs -f demo-app | grep ALERTshows the webhook notification arriving at thepagerreceiver.- Restore the error rate. Watch it resolve. How long did it take, and why? (Hint: the 5m window.)
- Write a test case for
HighLatencyP99inalerts.test.yml, run it, and make it pass.