RousselTMLEARNING

Observability

Writing good Prometheus alerting rules (and testing them)

Design useful alerts: alert on symptoms, choose for and thresholds, severity and team labels, annotations and runbooks, and unit tests with promtool.

1. Alerting on symptoms #

What bothers the user, not every possible cause.

Symptoms and causes #

Alert on a causeAlert on a symptom
CPU at 90%p99 latency above target
A pod restarts5xx error rate above 2%
Disk at 80%Disk full in less than 4h (prediction)

An alert must be actionable: if nobody has anything to do when receiving it, it is a dashboard indicator, not an alert. Every useless alert leads to alert fatigue: teams end up ignoring the real ones too.

- alert: DiskWillFillIn4Hours
  expr: predict_linear(node_filesystem_avail_bytes{fstype!="tmpfs"}[6h], 4*3600) < 0
  for: 30m
  labels: { severity: warning }
  annotations:
    summary: "Le disque {{ $labels.mountpoint }} de {{ $labels.instance }} sera plein dans 4 h" 

2. Labels and annotations #

What routing and humans need.

Conventions #

ElementUsageExample
severityUrgency: critical (wakes people up), warning (business hours), infocritical
teamRecipient team, used by routingpayments
summaryOne sentence readable on a phonePayment API: 8% errors
descriptionDetails, values{{ $value | humanizePercentage }}
runbook_urlDiagnosis and resolution procedurewiki link
dashboard_urlContext dashboardGrafana link
Common mistake: Never put a changing value ({{ $value }}) in labels: each new value would create a new alert. Values go in annotations.

3. Testing your rules #

promtool test rules.

Unit tests #

promtool replays simulated series and checks which alerts fire, when and with which labels:

# tests.yml
rule_files: [rules.yml]
evaluation_interval: 1m
tests:
  - interval: 1m
    input_series:
      - series: 'up{job="api", instance="api-1"}'
        values: '1 1 0 0 0 0 0 0'
    alert_rule_test:
      - eval_time: 4m          # 2 min de panne : encore pending
        alertname: InstanceDown
        exp_alerts: []
      - eval_time: 8m          # 6 min de panne : firing
        alertname: InstanceDown
        exp_alerts:
          - exp_labels: { severity: critical, team: plateforme, job: api, instance: api-1 }
            exp_annotations:
              summary: "api-1 ne répond plus"
              runbook_url: https://wiki.example.org/runbooks/instance-down
promtool check rules rules.yml
promtool test rules tests.yml
Good practice: Add these two commands to the CI of the rules repository: a rule that never fires is as dangerous as a broken rule.

Related articles

Alertmanager: role, architecture and lifecycle of an alert

What Alertmanager does between Prometheus and your teams: deduplication, grouping, routing, silences and inhi…

Routing, grouping and receivers in Alertmanager

The Alertmanager routing tree: matchers, group_by, group_wait, group_interval, repeat_interval, continue, rec…

Silences, inhibitions and time intervals in Alertmanager

Reduce alert noise: one-off silences with amtool, inhibition rules (a critical alert mutes secondary alerts) …

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!