Sub topic 14
Precomputing Queries with Recording Rules
This chapter assumes you understand Prometheus scrape jobs, metric labels, PromQL selectors, rate(), and basic YAML. You should also know how to reload or restart a Prometheus service.
A recording rule evaluates a PromQL expression on a schedule and stores the result as a new time series. Dashboards and alerts can then query the stored series instead of recalculating an expensive expression each time.
Why use recording rules
Recording rules help when:
- A query appears in many dashboards or alerts.
- A query uses expensive functions over many time series.
- A dashboard must load quickly over a long time range.
- You want a stable, descriptive name for an important measurement.
For example, CPU utilisation requires calculating a rate and aggregating every CPU core. Store that result once per evaluation interval:
groups:
- name: node-recording
interval: 30s
rules:
- record: instance:node_cpu_utilisation:rate5m
expr: 1 - avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m]))The record field is the name of the new metric. The colons are a naming convention that separates the source level, subject, and calculation window. Prometheus does not require this exact format, but consistent names make rules easier to find.
The expression calculates the average non-idle CPU fraction for each instance. The rate() function uses five minutes of counter data. The rule stores one result for each distinct instance label.
| Part | Meaning |
|---|---|
| `groups` | Collection of rules evaluated together |
| `interval` | How often this group is evaluated |
| `record` | Name of the generated time series |
| `expr` | PromQL expression to evaluate |
| `by (instance)` | Keep one result for each server |
How evaluation works
Prometheus evaluates the group every 30 seconds in this example. Each successful evaluation appends a sample to the recorded series. A dashboard can query the result directly:
100 * instance:node_cpu_utilisation:rate5mThe recorded series keeps the labels produced by the expression. If you aggregate with by (instance), labels such as cpu and mode disappear. This is useful when the intended result is one value per server.
Validate and avoid common errors
Place the rule file under the directory referenced by rule_files, then validate it before reloading Prometheus:
promtool check rules rules/node-recording.ymlCommon errors include:
- Writing a recording rule under
alertinstead ofrecord. - Choosing a name that collides with an existing metric.
- Aggregating away labels that dashboards need.
- Using a very short range, such as
[30s], when scrapes are infrequent. - Creating a rule for a query used only once. The extra stored series may not be worthwhile.
A recording rule does not repair missing input data. If node_cpu_seconds_total is absent or a target is down, the recorded series may also be absent.
Exercise
Why might instance:node_cpu_utilisation:rate5m have no data even though the rule file has valid YAML?
Show answer
Prometheus may not have loaded the rule, the node target may be down, or the input metric may be missing. Check the Prometheus rules page, target health, and the input query separately.