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
histogram_quantile(0.99, sum by (route, le) (rate(..._bucket[5m])))
over 50 instances touches hundreds of series and thousands of samples
per evaluation. On a dashboard with a 7-day range, that runs at every
step, for every viewer, every refresh. As a recording rule, it runs once
per interval and the dashboard reads one precomputed series per route.
Use recording rules for:
- Expensive queries on dashboards people open often.
- Building blocks for alerts. Alerts become short, readable, and consistent with the dashboards.
- Long-range queries. A 30-day SLO over raw counters is slow; over a 5-minute recorded ratio it's cheap.
- 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:
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
withoutoverby, 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 asrate(x[1h]), though it's close for steady traffic.
A recording rule creates a series from now on. It has no history.
After you add job:http_requests:rate5m, dashboards using it show
nothing before the moment you reloaded. If you need history, keep the
raw query on the dashboard for a while, or backfill with
promtool tsdb create-blocks-from rules.
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/rulesWatch 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_totalIf a group's duration approaches its interval, evaluations start being skipped, and recorded series get gaps. Split the group or simplify the expressions.
Lab
- Query
job_route:http_request_duration_seconds:p99_rate5mand the rawhistogram_quantileexpression it replaces. Compare the values and, usingstats=allon the API, the samples each one loads. - Add a rule
job_route:http_requests_errors:ratio_rate5m(per route). Validate, reload, and find it on/rules. - Change the Error ratio panel on the RED dashboard to use it. Note that its history starts at your reload.