TExcelTAD

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_quantile over 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

yaml
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 _total dropped 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:

bash · try it yourself
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.yml
Checking 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 with rate first, 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 instance in 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. Watch prometheus_rule_group_last_duration_seconds.

Test yourself

What does the name job_route:http_requests:rate5m tell you?

  1. It's a raw counter
  2. Aggregated to the job and route labels, from http_requests_total, using a 5-minute rate
  3. It's an alert
  4. It's a histogram bucket

Can a recording rule use the output of a rule listed earlier in the same group?

  1. Never
  2. Yes: rules in a group are evaluated in order, each cycle
  3. Only across groups
  4. Only with for

Why is rate(job:http_requests:rate5m[5m]) wrong?

  1. Recording rules can't be queried
  2. The recorded series is already a rate (a gauge-like value); rate() on it is meaningless
  3. The name is too long
  4. rate needs a histogram