Learn Labs
Rules, Alerting & SLOs

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."
alertname; becomes the alertname labelexprreturns one series per thing that is wrongforhow long it must keep returning before firingkeep_firing_forhow long to keep firing after it stops returning (anti-flap)labelsadded to the alert; used by Alertmanager for routingannotationshuman-readable text; templated, not used for routing

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

StateMeaning
InactiveThe expression returns nothing for this series
PendingIt returned something, but for less than for
FiringIt 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 active
up drops to 0+2 mintarget backup == 0expression returns a seriesstatependingfiringnotifiedtime →

Choosing 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: for of 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:

{{ $labels.instance }}a label of the alerting series{{ $value }}the expression's value{{ $value | humanize }}1234567 -> 1.235M{{ $value | humanizePercentage }}0.0732 -> 7.32%{{ $value | humanizeDuration }}0.42 -> 420ms{{ $externalLabels.cluster }}from global.external_labels

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. critical pages a human now. warning creates a ticket for working hours. Nothing in between.
  • Every alert links to a runbook. Add a runbook_url annotation 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.yml

Run 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

  1. Inject errors: curl -X POST 'localhost:8080/admin/chaos?error_rate=0.3'.
  2. Open http://localhost:9090/alerts. Watch ErrorBudgetBurnFast go 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.
  3. docker logs -f demo-app | grep ALERT shows the webhook notification arriving at the pager receiver.
  4. Restore the error rate. Watch it resolve. How long did it take, and why? (Hint: the 5m window.)
  5. Write a test case for HighLatencyP99 in alerts.test.yml, run it, and make it pass.

On this page