PromQL: Recording Rules
A recording rule precomputes a query on a schedule and stores the answer as a new series. Dashboards then read the answer instead of redoing the work.
One recording rule
This rule stores the shop request speed under a short name.
groups:
- name: shopsentry
interval: 30s
rules:
- record: job:http_requests_per_second:rate5m
expr: sum(rate(http_requests_total{job="shopsentry"}[5m]))Check the rule file
Promtool checks the file before Prometheus loads it.
promtool check rules shopsentry-rules.ymlRead the new series
The recorded series answers at once, with no work to redo.
job:http_requests_per_second:rate5mExample
job:http_requests_per_second:rate5mKey points
- A recording rule runs a query again and again on a fixed schedule.
- The answer is stored as a new series with the name you choose.
- Use recording rules for queries that dashboards and alerts run often.
- Write rules in a YAML file and point Prometheus at it.
- Check the file with promtool check rules before you load it.
- A rule group needs a name and an interval between runs.
- The new series appears in the expression browser like any other.
In this example, the recorded series holds the shop request speed that the rule precomputed. The value matches the live rate query, and it is the real value from the live ShopSentry lab.
Note
A recording rule costs storage, so record only queries you run often.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
What does a recording rule store?