1. Why recording rules? #
Compute in advance rather than on every read.
The problem #
A PromQL query is computed when it runs. On a twenty-panel dashboard, refreshed every thirty seconds and opened by ten people, Prometheus keeps repeating the same computations on the same samples. The dashboard gets slow, and every extra user makes it worse.
A recording rule solves this: Prometheus evaluates the expression at a regular interval and writes the result to a new series. Reads become simple accesses to that series.
Real measurement: on a test Prometheus 3.15, withstats=all,sum by (job) (rate(prometheus_http_requests_total[5m]))reads 448 samples, and the equivalent recorded series just one. The gap is far larger on a production server.
When to use them, and when not to #
| Good idea | Bad idea |
|---|---|
| Expensive, frequent query (heavily used dashboard) | Query run rarely, by hand |
| Alert with a heavy expression | Query already fast on few series |
| Shared indicator (SLI, error rate) defined only once | Keeping all original labels: nothing is reduced |
| Aggregates to keep for a long time or to federate | Replacing exploration: the window (5m) is fixed |
A rule is not free: each recorded series uses storage and memory, like a scraped series. The gain comes from aggregation: replacing thousands of series with a few dozen.
2. How Prometheus evaluates them #
Groups, interval, order and no retroactive effect.
How it works #
- Rules live in groups, declared in files referenced by
rule_files. - Within a group they run in order, at the same timestamp. A rule can therefore use the result of the previous one.
- The interval is the group's (
interval), otherwise the globalevaluation_interval. - The result keeps the labels of the expression; the metric name is replaced by
record. - A rule only computes the present: a new rule has no history, unless recomputed manually (see below).
Mind the interval: a series with no point for more than five minutes (the default lookback) no longer appears in instant queries. A group evaluated every ten minutes therefore produces gaps.
My progress
Sign in to track your progress and take notes. Sign in
💬 Recent discussions about this course
- Prometheus consomme 12 Go de RAM : comment trouver les métriques coupables ?✓ solved · 2 reply(ies)
- PromQL : quelle différence entre rate() et irate() ?✓ solved · 1 reply(ies)
Ask a question about this lesson →See all discussions about this course
Course Glossary
Alerting
The process of notifying teams (via email, Slack, Teams) when a specific event, threshold breach, or anomaly is detected.
Architecture
The overall structural design of an IT system, defining its various components, their relationships, and the principles guiding its evolution.
OpenTelemetry
An open source framework providing standards, APIs, and SDKs to collect and export observability data (traces, metrics, logs) in an agnostic way.
Recommended Articles
Prometheus best practices: metrics, cardinality, rules and operations
Prometheus best practices: metric naming, labels and cardinality, scrape intervals, recording rul...
Thanos Ruler: rules and alerts on the global view
Evaluate recording and alerting rules across all clusters with Thanos Ruler: configuration, Alert...
Measuring SLIs with Prometheus
Implement availability and latency SLIs with Prometheus: required metrics, ratio queries, histogr...
Prometheus: alerting with Alertmanager (rules, routing, silences)
Set up Prometheus alerting: alert rules, Alertmanager, routing, grouping, inhibition, silences, S...
Alerting on logs with the Loki Ruler
Set up LogQL alerts and recording rules with the Loki Ruler: configuration, rule files, Alertmana...
Mimir: ruler, Alertmanager and mimirtool
Evaluate recording and alerting rules in Mimir, configure the multi-tenant Alertmanager and autom...
Writing good Prometheus alerting rules (and testing them)
Design useful alerts: alert on symptoms, choose for and thresholds, severity and team labels, ann...
Time-based vs event-based SLO: understanding, computing and choosing (with the Qonto example)
Differences between time-based SLOs (time slices, uptime) and event-based SLOs (good events over ...
Prometheus: visualizing your metrics (UI, Grafana and dashboards)
Visualize Prometheus data: the web interface, PromQL graphs, connecting to Grafana, RED and USE d...