Recording Rules
A recording rule evaluates a PromQL expression on a schedule and stores the result as a new series. Dashboards and alerts then read the cheap precomputed series instead of recomputing an expensive expression over thousands of raw series every time someone opens a page.
When to record
- An expression is used by several dashboards or alerts (the service's error ratio, its p99).
- A query is slow: many series, long ranges,
histogram_quantileover big histograms. - You want a stable, documented name for a concept (
job:http_requests:rate5m) instead of copy-pasted PromQL. - Federation or remote write should only ship aggregates, not raw series.
- SLO burn-rate alerts need the same ratio over several windows (SRE: SLO recording rules).
Rule files
groups:
- name: linkly-red # rules in a group run in order, sequentially
interval: 30s # optional; defaults to global.evaluation_interval
rules:
- record: job_route:http_requests:rate5m
expr: sum by (job, route) (rate(http_requests_total[5m]))
- record: job:http_requests:rate5m
expr: sum by (job) (job_route:http_requests:rate5m) # builds on the rule above
- record: job:http_request_errors:ratio_rate5m
expr: |
sum by (job) (rate(http_requests_total{code=~"5.."}[5m]))
/ sum by (job) (rate(http_requests_total[5m]))The naming convention from the Prometheus docs is level:metric:operations:
level: the labels the result keeps (job,job_route,instance), i.e. the aggregation level;metric: the original metric name, with_totaldropped once rate() has been applied;operations: what was done, newest last (rate5m,ratio_rate5m,p99).
Everybody reading job_route:http_requests:rate5m knows what it is and how it was computed. Rules in one group run in the listed order in each evaluation, so a later rule can use an earlier one's output from the same cycle; across groups there is no order guarantee.
Testing them
Recording rules are code, so they get tests. promql_expr_test checks what an expression (here, the recorded series) returns at a given virtual time:
cat > linkly-red.yml <<'YML'
groups:
- name: linkly-red
rules:
- record: job_route:http_requests:rate5m
expr: sum by (job, route) (rate(http_requests_total[5m]))
- record: job:http_requests:rate5m
expr: sum by (job) (job_route:http_requests:rate5m)
- record: job:http_request_errors:ratio_rate5m
expr: |
sum by (job) (rate(http_requests_total{code=~"5.."}[5m]))
/ sum by (job) (rate(http_requests_total[5m]))
YML
promtool check rules linkly-red.yml
cat > linkly-red.test.yml <<'YML'
rule_files: [linkly-red.yml]
evaluation_interval: 1m
tests:
- interval: 1m
input_series: # per minute: 540 redirects, 60 creations, 6 errors on api-1
- series: 'http_requests_total{job="linkly", instance="api-1", route="/{code}", code="302"}'
values: '0+540x15'
- series: 'http_requests_total{job="linkly", instance="api-1", route="/links", code="201"}'
values: '0+60x15'
- series: 'http_requests_total{job="linkly", instance="api-1", route="/{code}", code="500"}'
values: '0+6x15'
promql_expr_test:
- expr: job_route:http_requests:rate5m
eval_time: 10m
exp_samples:
- labels: 'job_route:http_requests:rate5m{job="linkly", route="/{code}"}'
value: 9.1
- labels: 'job_route:http_requests:rate5m{job="linkly", route="/links"}'
value: 1
- expr: job:http_requests:rate5m
eval_time: 10m
exp_samples:
- labels: 'job:http_requests:rate5m{job="linkly"}'
value: 10.1
- expr: job:http_request_errors:ratio_rate5m
eval_time: 10m
exp_samples:
- labels: 'job:http_request_errors:ratio_rate5m{job="linkly"}'
value: 0.009900990099009901
YML
promtool test rules linkly-red.test.ymlChecking linkly-red.yml
SUCCESS: 3 rules found
SUCCESS
The test passes, so the rules compute what their names promise. Notice the expected labels: the recorded series is named after record, and only the labels kept by sum by remain. When a test fails, promtool prints the expected and actual samples side by side, as the next-but-one lesson shows.
Pitfalls
- Don't rate a recorded rate.
rate(job:http_requests:rate5m[5m])is meaningless; aggregate counters withratefirst, and record the result. - Mind the evaluation interval. A rule recorded every 1 m can't feed a
[30s]window; use windows of at least 2–4 evaluation intervals. - Labels change identity. Keeping
instancein a "fleet" rule makes it as expensive as the raw data. Removing a label in a later version breaks every dashboard that selects on it. - Staleness. If the source series disappear, the recorded series disappear too (after the lookback); alerts relying on them need an
absent()companion. - Missed evaluations are counted in
prometheus_rule_group_iterations_missed_total; a group that takes longer than its interval needs splitting or a longer interval. Watchprometheus_rule_group_last_duration_seconds.
Test yourself
What does the name job_route:http_requests:rate5m tell you?
- It's a raw counter
- Aggregated to the job and route labels, from http_requests_total, using a 5-minute rate
- It's an alert
- It's a histogram bucket
Can a recording rule use the output of a rule listed earlier in the same group?
- Never
- Yes: rules in a group are evaluated in order, each cycle
- Only across groups
- Only with
for
Why is rate(job:http_requests:rate5m[5m]) wrong?
- Recording rules can't be queried
- The recorded series is already a rate (a gauge-like value); rate() on it is meaningless
- The name is too long
- rate needs a histogram