Skip to content

Prometheus Recording Rule Evaluation Order Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Prometheus Recording Rule Evaluation Order Fix. We cover key concepts, practical examples, and best practices.

Your Prometheus recording rule produces stale or missing data — the recorded metric has gaps, depends on another recording rule that has not been evaluated yet, or the evaluation interval is too long for the query's resolution.

The Problem

groups:
  - name: error_rate
    interval: 1m
    rules:
      - record: job:error_rate:rate5m
        expr: rate(errors_total[5m]) / rate(requests_total[5m])

The error_rate:5m recording rule references errors_total and requests_total in separate rate() calls. If either metric has gaps, the recording rule produces no output for that evaluation interval.

Step-by-Step Fix

1. Handle metric availability in recording rules

groups:
  - name: error_rate
    interval: 1m
    rules:
      - record: job:errors:rate5m
        expr: rate(errors_total[5m])
      - record: job:requests:rate5m
        expr: rate(requests_total[5m])
      - record: job:error_rate:rate5m
        expr: |
          (job:errors:rate5m > 0 or vector(0))
          /
          (job:requests:rate5m > 0 or vector(0))

2. Order rule groups by dependency

groups:
  # Evaluated first — base recording rules
  - name: base_metrics
    interval: 30s
    rules:
      - record: instance:cpu:rate5m
        expr: rate(cpu_seconds_total[5m])

  # Evaluated second — aggregated rules
  - name: aggregated_metrics
    interval: 1m
    rules:
      - record: job:cpu:rate5m
        expr: sum by (job) (instance:cpu:rate5m)

  # Evaluated third — alerting rules
  - name: alerts
    interval: 1m
    rules:
      - alert: HighCPUUsage
        expr: job:cpu:rate5m > 0.8

3. Match evaluation intervals to data granularity

groups:
  # Fast evaluation for near-real-time metrics
  - name: realtime
    interval: 15s
    rules:
      - record: job:requests:rate1m
        expr: rate(requests_total[1m])

  # Slower evaluation for long-term trends
  - name: trends
    interval: 5m
    rules:
      - record: job:requests:rate1h
        expr: rate(requests_total[1h])

4. Handle absent data in rules

groups:
  - name: availability
    interval: 1m
    rules:
      - record: job:up:count
        expr: count by (job) (up == 1)
      - record: job:down:count
        expr: count by (job) (up == 0)
      - record: job:uptime:ratio
        expr: |
          (job:up:count > 0 or vector(0))
          /
          ((job:up:count + job:down:count) > 0 or vector(1))

5. Validate recording rules

# Check rule syntax
promtool check rules /etc/prometheus/rules/*.yml

# Test rule evaluation
promtool test rules testdata.yml

# Check rule status in Prometheus UI
# http://prometheus:9090/rules

Expected output:

Rule evaluation: SUCCESS
job:error_rate:rate5m → 0.023 (2.3% error rate)
Evaluation time: 12ms

Prevention Tips

  • Order rule groups from base metrics to aggregated rules to alerts
  • Use or vector(0) to prevent division by zero
  • Set shorter intervals for base rules (30s) and longer for aggregations (1m+)
  • Validate rules with promtool check rules before deploying
  • Monitor rule evaluation lag with <a href="/devops/prometheus-grafana/">Prometheus</a>_rule_evaluation_duration_seconds

Common Mistakes with recording rule

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world PROMETHEUS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the difference between recording and alerting rules?

Recording rules pre-compute frequently-used expressions into new metrics — they reduce query load and improve dashboard performance. Alerting rules evaluate conditions and send alerts. Recording rules start with a level: prefix (e.g., instance:...).

How do I know if my recording rule is too slow?

Check prometheus_rule_evaluation_duration_seconds — if it exceeds the interval for the group, rules are falling behind. Increase the interval or optimize the PromQL expression. Use promtool to benchmark rule evaluation time.

Can recording rules reference other recording rules?

Yes, but ensure the referenced rule is evaluated first by placing it in an earlier group. Prometheus evaluates groups sequentially in file order and groups within a file in definition order.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro