Alerting Rules
An alerting rule is a PromQL expression that Prometheus evaluates on a schedule. Every series the expression returns is an alert. Prometheus doesn't send emails or pages itself; it sends active alerts to Alertmanager, which groups, deduplicates, silences and routes them (next section).
Anatomy
groups:
- name: linkly-alerts
rules:
- alert: LinklyHighErrorRate
expr: job:http_request_errors:ratio_rate5m{job="linkly"} > 0.05
for: 5m # must stay true this long before firing
keep_firing_for: 10m # keep firing this long after it stops being true
labels:
severity: page # used for routing in Alertmanager
team: links
annotations:
summary: "linkly 5xx ratio is {{ $value | humanizePercentage }}"
description: "{{ $labels.job }} has failed more than 5% of requests for 5 minutes."
runbook_url: https://runbooks.example.com/linkly/high-error-rate
dashboard: https://grafana.example.com/d/linkly-red| Field | Meaning |
|---|---|
expr |
any PromQL; each returned series is one alert, carrying its labels |
for |
how long the condition must hold: the alert is pending until then, firing after |
keep_firing_for |
keep firing after the condition clears, so a flapping signal doesn't resolve and refire every few minutes |
labels |
added to the alert; identity and routing (severity, team) |
annotations |
human text, not identity: summary, description, runbook_url; templated with Go templates |
Labels vs annotations: two alerts with the same labels are the same alert. Never put a value that changes (like $value) into a label, or every evaluation creates a "new" alert. Values go into annotations.
Templates
Annotations (and labels) are Go templates with:
$labels.<name>(or{{ $labels.instance }}),$value(the expression's value),$externalLabels;- functions such as
humanize(1.2k),humanize1024(1.2Ki),humanizePercentage(0.053 → 5.3%),humanizeDuration(93784 → 1d 2h 3m 4s),printf,toUpper,reReplaceAll,title; queryto run another PromQL query inside the template (use sparingly).
The life of an alert
condition false ─▶ inactive
condition true ─▶ pending (for: not yet elapsed) series ALERTS{alertstate="pending"}
held for `for` ─▶ firing (sent to Alertmanager, re-sent every evaluation)
condition false ─▶ resolved (after keep_firing_for, if set; Alertmanager gets an end time)Prometheus also writes the state as series you can graph: ALERTS{alertname, alertstate, …} and ALERTS_FOR_STATE. The test below walks through those states on a virtual clock. The error ratio is 2% for 10 minutes, then 10% (an incident starts at 10 m), then back to 2% at 25 m:
cat > alerts.yml <<'YML'
groups:
- name: linkly-alerts
rules:
- alert: LinklyHighErrorRate
expr: |
sum(rate(http_requests_total{code="500"}[2m])) / sum(rate(http_requests_total[2m])) > 0.05
for: 5m
keep_firing_for: 5m
labels:
severity: page
annotations:
summary: "linkly 5xx ratio is {{ $value | humanizePercentage }}"
YML
cat > alerts.test.yml <<'YML'
rule_files: [alerts.yml]
evaluation_interval: 1m
tests:
- interval: 1m
input_series:
# 600 requests per minute; 500s: 12/min, then 60/min from 10m to 25m, then 12/min again
# (each segment continues where the previous one ended)
- series: 'http_requests_total{code="200"}'
values: '0+588x10 6420+540x14 14568+588x20'
- series: 'http_requests_total{code="500"}'
values: '0+12x10 180+60x14 1032+12x20'
alert_rule_test:
- {eval_time: 9m, alertname: LinklyHighErrorRate, exp_alerts: []}
# 10m-15m: the condition is true but `for: 5m` has not elapsed: pending, nothing is sent
- {eval_time: 13m, alertname: LinklyHighErrorRate, exp_alerts: []}
- eval_time: 17m
alertname: LinklyHighErrorRate
exp_alerts:
- exp_labels: {severity: page}
exp_annotations: {summary: "linkly 5xx ratio is 10%"}
# 27m: the ratio is back to 2%, but keep_firing_for holds the alert
- eval_time: 29m
alertname: LinklyHighErrorRate
exp_alerts:
- exp_labels: {severity: page}
exp_annotations: {summary: "linkly 5xx ratio is 10%"}
- {eval_time: 33m, alertname: LinklyHighErrorRate, exp_alerts: []}
promql_expr_test:
- expr: ALERTS{alertname="LinklyHighErrorRate"}
eval_time: 13m
exp_samples:
- labels: 'ALERTS{alertname="LinklyHighErrorRate", alertstate="pending", severity="page"}'
value: 1
YML
promtool test rules alerts.test.yml SUCCESS
Things to notice:
alert_rule_testchecks only firing alerts. Pending ones are visible through theALERTSseries, which the last check reads.- While
keep_firing_forholds the alert, the annotation keeps its last value (10%): the alert is still the one that fired. - The condition flips at 10 m in the data, but the
[2m]rate needs a minute or two to cross the threshold, andfor: 5madds five more. Detection delay = rate window lag +for. Pick both consciously: long enough to skip blips, short enough to page before users complain.
Choosing for
| Alert | Typical for |
|---|---|
target down (up == 0) |
2–5 m (one missed scrape is not an incident) |
| error ratio, latency | 5–10 m, or use SLO burn-rate alerts with no for and multiple windows (SRE: multi-window alerts) |
disk will be full (predict_linear) |
30 m–1 h |
| certificate expiry | 1 h |
for resets to zero whenever the expression stops returning the series even for one evaluation, including when Prometheus restarts (it restores for state from ALERTS_FOR_STATE on restart, within a grace period).
Test yourself
Why must $value never be used in an alert's labels?
- Labels can't be templated
- Labels define the alert's identity; a changing value creates a new alert each evaluation
- It's too long
- Alertmanager drops it
An alert has for: 5m. Its condition is true for 4 minutes, false once, then true again. When does it fire?
- After 1 more minute
- After 5 more minutes: the pending timer restarted
- Immediately
- Never
What does keep_firing_for: 10m prevent?
- Alerts firing too early
- An alert resolving and re-firing repeatedly when the signal briefly dips below the threshold
- Duplicate notifications from two Prometheus servers
- Alerts without runbooks