Learn Labs
Rules, Alerting & SLOs

Recording Rules

Precomputed series and the naming convention

A recording rule evaluates a PromQL expression on a schedule and stores the result as a new time series. It's the Prometheus equivalent of a materialised view: pay the cost once, every 15 seconds, instead of every time someone opens a dashboard.

Why they exist

Use recording rules for:

  1. Expensive queries on dashboards people open often.
  2. Building blocks for alerts. Alerts become short, readable, and consistent with the dashboards.
  3. Long-range queries. A 30-day SLO over raw counters is slow; over a 5-minute recorded ratio it's cheap.
  4. Federation and remote write. Ship a few aggregated series instead of all raw ones.

The rule file

Rules live in YAML files listed under rule_files in prometheus.yml. This stack loads config/rules/*.yml.

groups:
- name: demo-app-red
  interval: 15s                 # optional; defaults to global evaluation_interval
  rules:
    - record: job:http_requests:rate5m
      expr: sum by (job) (rate(http_requests_total[5m]))

    - record: job:http_requests_errors:ratio_rate5m
      expr: |
        sum by (job) (rate(http_requests_total{status=~"5.."}[5m]))
          /
        sum by (job) (rate(http_requests_total[5m]))

    - record: job_route:http_request_duration_seconds:p99_rate5m
      expr: histogram_quantile(0.99, sum by (job, route, le) (rate(http_request_duration_seconds_bucket[5m])))

Rules in a group run sequentially, in order, at the same timestamp. So a later rule can use the output of an earlier one in the same group. Different groups run concurrently.

The naming convention

Recording rule names follow level:metric:operations:

job_route:http_request_duration_seconds:p99_rate5mjob_routelevel: labels the result keeps (job and route)http_request_duration_secondsmetric: the source metric, minus _total/_bucketp99_rate5moperations: what was applied, most recent first

The colons make recorded series instantly recognisable: raw metrics never contain colons. The convention also makes mistakes visible: instance:... that is actually summed by job is a bug you can spot in review.

Rules of thumb

  • Aggregate in the rule. A rule that records rate(x[5m]) without aggregating just duplicates every series. Little saving, double the storage.
  • Keep the labels alerts and dashboards need. Prefer without over by, so labels added by service discovery (env, team) survive.
  • Rate inside, ratio outside. Record the rates of numerator and denominator, and ratios of them, rather than averaging ratios.
  • Don't record over recorded series with a different window unless you mean it. avg_over_time(job:x:rate5m[1h]) is not the same as rate(x[1h]), though it's close for steady traffic.

Checking and reloading

# syntax and PromQL validity
docker exec prometheus promtool check rules /etc/prometheus/rules/recording.yml

# apply
curl -X POST localhost:9090/-/reload

# did it work? last evaluation, duration, errors per rule
open http://localhost:9090/rules

Watch rule cost with Prometheus's own metrics:

prometheus_rule_group_last_duration_seconds        # how long each group took
prometheus_rule_group_interval_seconds             # how long it's allowed to take
prometheus_rule_group_iterations_missed_total      # evaluations skipped because the last one overran
prometheus_rule_evaluation_failures_total

If a group's duration approaches its interval, evaluations start being skipped, and recorded series get gaps. Split the group or simplify the expressions.

Lab

  1. Query job_route:http_request_duration_seconds:p99_rate5m and the raw histogram_quantile expression it replaces. Compare the values and, using stats=all on the API, the samples each one loads.
  2. Add a rule job_route:http_requests_errors:ratio_rate5m (per route). Validate, reload, and find it on /rules.
  3. Change the Error ratio panel on the RED dashboard to use it. Note that its history starts at your reload.

On this page