All guides

Prometheus alerting rules: alert on symptoms

An alerting rule is a PromQL expression with a waiting time, labels and annotations. The Prometheus documentation also says what to point it at: symptoms that users feel, with as few alerts as possible.

Lines of code on a monitor, seen at an angle

What a rule file holds

Rules live in YAML files that Prometheus loads through rule_files in its configuration. A file holds groups. Each group has a name and a list of rules, which run in sequence at a regular interval. This is the example from the Prometheus documentation:

YAML

groups:
- name: example
  labels:
    team: myteam
  rules:
  - alert: HighRequestLatency
    expr: job:request_latency_seconds:mean5m{job="myjob"} > 0.5
    for: 10m
    keep_firing_for: 5m
    labels:
      severity: page
    annotations:
      summary: High request latency

The threshold and the two durations are the documentation's example values. Choose your own.

What each field does

  • alert is the name. The Prometheus best practices note that the community writes alert names in camel case.
  • expr is the PromQL expression. Every series it returns becomes one active alert with that series' labels.
  • for is how long the expression must keep returning a series before the alert fires. Until then the alert is pending. The default is 0s, and a rule with no for becomes active on the first evaluation.
  • keep_firing_for keeps the alert firing for a set time after the condition was last met. The default is 0s. The documentation names flapping alerts and false resolutions as what it prevents.
  • labels are added to the alert and overwrite conflicting labels. The example sets severity: page on the rule and team: myteam on the whole group.
  • annotations hold longer information, such as alert descriptions or runbook links. Label and annotation values accept templates such as {{ $labels.instance }} and {{ $value }}.

Symptoms before causes

The Prometheus alerting practices fit in one sentence: keep alerting simple, alert on symptoms, have good consoles for pinpointing causes, and avoid pages where there is nothing to do. The page asks for as few alerts as possible, tied to end-user pain.

For online serving systems its advice is to alert on high latency and on error rates at the highest point of the stack you can, and to page on latency at a single point in the stack. Errors further down the stack that cause a user-visible failure need no separate page.

The SRE workbook writes the error-rate symptom as two rules, for a service with a 99.9% SLO. A recording rule computes the ratio of errors to requests:

YAML

record: job:slo_errors_per_request:ratio_rate10m
expr:
  sum(rate(slo_errors[10m])) by (job)
    /
  sum(rate(slo_requests[10m])) by (job)

An alerting rule fires when that ratio reaches the 0.1% the SLO allows:

YAML

- alert: HighErrorRate
  expr: job:slo_errors_per_request:ratio_rate10m{job="myjob"} >= 0.001

The workbook presents this as its simplest version and names the weakness: low precision. A 0.1% error rate for 10 minutes fires the alert while consuming only 0.02% of the monthly error budget. Its later versions alert on burn rate.

Rule checks with promtool

Shell

promtool check rules /path/to/example.rules.yml
promtool test rules test.yml

The first command checks syntax with no running server. It prints the parsed rules and exits with 0, or prints the error and exits with 1. The second runs unit tests: you supply input series and the alerts you expect at a given evaluation time. promtool is part of the Prometheus archive.

Run both in CI. Prometheus applies a reload only if all rule files are well formatted.

Routing, grouping and inhibition

The Prometheus documentation says that alerting rules work out what is broken right now, and that another layer adds summarization, notification rate limiting, silencing and alert dependencies. That layer is Alertmanager. This excerpt comes from the example configuration in the Alertmanager repository, with the comments, the other routes and the other sections left out:

YAML

route:
  group_by: ['alertname', 'cluster', 'service']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 3h
  receiver: team-X-mails
  routes:
    - matchers:
        - service=~"foo1|foo2|baz"
      receiver: team-X-mails
      routes:
        - matchers:
            - severity="critical"
          receiver: team-X-pager

inhibit_rules:
  - source_matchers: [severity="critical"]
    target_matchers: [severity="warning"]
    equal: [alertname, cluster, service]
  • Routing. Every alert enters at the top-level route, which must match all alerts. It then goes down the child routes and stops at the first match, unless that route sets continue. Here alerts for three services go to team-X-mails, and the critical ones among them go to team-X-pager.
  • Grouping. group_by lists the labels by which alerts are batched into one notification. group_wait is the wait before the first notification for a new group, 30s by default. group_interval is the wait before further notifications for that group, 5m by default. repeat_interval is the wait before a notification is repeated, 4h by default.
  • Inhibition. A rule mutes target alerts while a source alert exists, when both carry the same values for the labels in equal. The example mutes warning-level notifications if the same alert is already critical.

The label values, receiver names and timings are the example file's. Set your own.

The default rules of the kube-prometheus-stack chart carry their runbook link in an annotation called runbook_url. This is one of them, as helm template renders it:

YAML

alert: KubeSchedulerDown
annotations:
  description: KubeScheduler has disappeared from Prometheus target discovery.
  runbook_url: https://runbooks.prometheus-operator.dev/runbooks/kubernetes/kubeschedulerdown
  summary: Target disappeared from Prometheus target discovery.
expr: absent(up{job="kube-scheduler"})
for: 15m
labels:
  severity: critical

Use the same annotation name in your own rules. The Prometheus documentation lists runbook links among the things annotations are for.

What to check in review

  1. The expression describes a symptom. The Prometheus practices ask for alerts on end-user pain.
  2. for leaves slack. The same page says to allow for it, to accommodate small blips.
  3. The labels are the ones your Alertmanager routes match on.
  4. The annotations include a summary and a runbook_url.
  5. promtool check rules and the unit tests pass.
  6. After deploy, the rule shows on the Alerts tab of the Prometheus UI. The ALERTS series records when it is pending or firing.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides