Linux Labs

Observability: metrics, alerts, logs / Stage 4 of 9

An alert rule: threshold and duration

Not running Starting usually ~20 s
Leave the lesson

Step 2 of 6 · on a machine · 7 min

The rule file and rule_files

Prometheus checks only the rules it has loaded from the files listed in its configuration.

An alert rule is an entry in a YAML file: the alert name, a PromQL expression and the conditions under which the alert fires. Rules are collected into groups, and one file can hold several:

yaml
groups:
  - name: machines      # a rule group
    rules:
      - alert: InstanceDown
        expr: up == 0   # which series "fire"
        for: 1m         # how long to hold

Prometheus learns which files to read from the rule_files list in /etc/prometheus/prometheus.yml: a file path or a pattern that picks up the whole directory.

yaml
rule_files:
  - /etc/prometheus/rules/machines.yml

In the package configuration rule_files: is empty — Prometheus has no rules, however many files lie next to it.

A mistake in a file is caught before the reread: promtool check rules <file> parses the YAML and the rule expressions, and promtool check config checks the configuration together with the connected files and prints how many it found. sudo systemctl reload prometheus rereads the configuration — just as after editing the scrape jobs in the lesson on scraping metrics.

Exercise · do it

Connect the rule file

Make Prometheus load the rules from the /etc/prometheus/rules directory: edit /etc/prometheus/prometheus.yml (with sudo), check it with promtool and make Prometheus reread its configuration.

Sign in and start The machine is available after signing in

Goals · 0 of 2

  • Connect the rule directory in prometheus.yml not done yet
  • Load the rule into the running Prometheus not done yet

Step goals

0 of 2
  • Connect the rule directory in prometheus.yml — not done yet
  • Load the rule into the running Prometheus — not done yet
Ubuntu 24.04

Step goals

0 of 2
  • Connect the rule directory in prometheus.yml — not done yet
  • Load the rule into the running Prometheus — not done yet
Loading…