Skip to content

Lesson 6 of 9 · 26m

Recording Rules and Alerts: Precompute the Questions That Hurt

Use recording rules to turn expensive raw queries into stable series, then design alerts around user impact rather than metric movement.

A dashboard panel that scans hundreds of thousands of series every refresh is a batch job pretending to be a UI. Recording rules exist because humans ask the same expensive questions repeatedly. They evaluate a PromQL expression on a schedule and write the result as a new time series. Used well, they improve latency, consistency, and alert quality. Used badly, they multiply cardinality and hide broken assumptions.

Recording rules are materialized queries

Example rule file:

groups:
  - name: checkout-service
    interval: 30s
    rules:
      - record: job_route_status:http_requests:rate5m
        expr: |
          sum by (job, route, status) (
            rate(http_requests_total[5m])
          )
      - record: job:http_request_errors:ratio5m
        expr: |
          sum by (job) (rate(http_requests_total{status=~"5.."}[5m]))
          /
          sum by (job) (rate(http_requests_total[5m]))

The resulting series can be graphed cheaply and referenced by alerts. Naming matters. A common convention is level:metric:operations, which tells the reader what dimensions remain and what calculation happened.

The cardinality bill still applies

Recording rules do not make expensive labels free. If the expression keeps route, and route is unbounded, you have scheduled a cardinality explosion every 30 seconds. Validate the output series count before merging the rule:

count(job_route_status:http_requests:rate5m)
count by (job) (job_route_status:http_requests:rate5m)

A rule that reduces query cost but increases stored series without operational value is a bad trade.

Alert on symptoms users can feel

A CPU alert says the machine is busy. Sometimes that matters. Often the real user-facing symptom is elevated latency, error ratio, queue age, or failed probes. A useful alert has four parts:

- alert: CheckoutHighErrorRatio
  expr: job:http_request_errors:ratio5m{job="checkout"} > 0.02
  for: 10m
  labels:
    severity: page
    team: payments
  annotations:
    summary: Checkout error ratio above 2 percent for 10 minutes
    runbook: https://runbooks.internal/checkout/error-ratio

The for clause filters transient noise. It is not a moral failing to page quickly, but paging on a single bad minute trains everyone to ignore the pager.

Multi-window alerts reduce false confidence

For burn-rate alerting, compare fast and slow windows:

expr: |
  (
    sum(rate(http_requests_total{status=~"5.."}[1h]))
    /
    sum(rate(http_requests_total[1h]))
  ) > 0.05
  and
  (
    sum(rate(http_requests_total{status=~"5.."}[5m]))
    /
    sum(rate(http_requests_total[5m]))
  ) > 0.05

The long window proves sustained budget burn; the short window proves it is happening now. This pattern is especially useful for error budgets because pure long-window alerts page after the incident is already old.

Rules need ownership and tests

Rules are code. Review them, test them, and version them. promtool catches syntax and can run rule unit tests:

promtool check rules rules/checkout.yml
promtool test rules tests/checkout.test.yml

A unit test can define input series and assert alert firing at evaluation times. It is tedious once and then saves you during every refactor.

Field notes

  • Do not record a series merely because a query is long; record because it is reused or operationally important.
  • Alert labels determine routing. Keep them bounded and deliberate, especially severity and team.
  • for starts only while the expression continuously matches; disappearing series reset pending alerts.
  • A runbook link that says 'investigate' is not a runbook. Write the first three diagnostic queries down.
  • Recording rules evaluate at the Prometheus performing the rule evaluation; think carefully before assuming global aggregation semantics.

Was this lesson helpful?

Sign in to rate