TExcelTAD
Home › Site Reliability Engineering: SLOs, Incidents & Chaos › SLO Recording Rules with promtool

SLO Recording Rules with promtool

Burn-rate alerts need the error ratio over seven windows: 5 minutes, 30 minutes, 1, 2 and 6 hours, 1 and 3 days. Computing rate(...[3d]) across every series at every evaluation would be expensive, so the ratios are precomputed by recording rules, one per window, and the alerts read those. This lesson writes the rules and tests them with promtool, Prometheus's own command-line tool, on synthetic series. Recording rules, PromQL and promtool in general are taught in Observability; here they serve the SLO.

The naming convention

Prometheus recommends level:metric:operations names for recorded series. The workbook's examples use exactly that shape:

job:slo_errors_per_request:ratio_rate1h
└┬┘ └─────────┬──────────┘ └────┬─────┘
 │            │                 └── what was done: a ratio of rates over 1h
 │            └── the metric: errors per request
 └── aggregated by job

Generating the rules

Seven nearly identical rules are a copy-paste accident waiting to happen. The session generates them from the list of windows with a shell loop, then checks the file:

bash · try it yourself
{
  echo "groups:"
  echo "  - name: slo-checkout-sli"
  echo "    rules:"
  for w in 5m 30m 1h 2h 6h 1d 3d; do
    cat <<YML
      - record: job:slo_errors_per_request:ratio_rate$w
        expr: |
          sum by (job) (rate(http_requests_total{job="checkout",code=~"5.."}[$w]))
          /
          sum by (job) (rate(http_requests_total{job="checkout"}[$w]))
YML
  done
} > slo-rules.yml
promtool check rules slo-rules.yml
head -9 slo-rules.yml
grep -c 'record:' slo-rules.yml
Checking slo-rules.yml
  SUCCESS: 7 rules found

groups:
  - name: slo-checkout-sli
    rules:
      - record: job:slo_errors_per_request:ratio_rate5m
        expr: |
          sum by (job) (rate(http_requests_total{job="checkout",code=~"5.."}[5m]))
          /
          sum by (job) (rate(http_requests_total{job="checkout"}[5m]))
      - record: job:slo_errors_per_request:ratio_rate30m
7

A few details matter:

  • Sum before dividing. sum(rate(errors)) / sum(rate(all)) weights each instance by its traffic. Averaging per-instance ratios would let a tiny, broken instance count as much as a busy healthy one.
  • rate() before sum(). rate() handles counter resets per series; summing raw counters first would turn a pod restart into a negative rate.
  • Filter early. The job and code matchers select only the series you need; the denominator has no code matcher, so it counts every request. If some codes are excluded from valid events (health checks, 4xx), exclude them in both selectors.

Testing the rules on a synthetic incident

promtool test rules loads the rule files, feeds them series written in a compact notation, and compares the results at chosen times. '0+1999x4800' means "start at 0 and add 1999 every interval, 4,800 times": with interval: 1m that's a counter growing by 1999 requests a minute for 80 hours. The scenario has 1 error a minute for three days (burn rate 0.5 for a 99.9% SLO), then 200 errors a minute (about 9% of requests) for 30 minutes from 3d0m, then the normal rate again.

The test checks the burn rate (ratio / 0.001, rounded to one decimal so that floating-point noise doesn't fail the comparison) for several windows, 9 minutes into the incident and 90 minutes after it ended:

bash · try it yourself
{
  echo "groups:"
  echo "  - name: slo-checkout-sli"
  echo "    rules:"
  for w in 5m 30m 1h 2h 6h 1d 3d; do
    cat <<YML
      - record: job:slo_errors_per_request:ratio_rate$w
        expr: |
          sum by (job) (rate(http_requests_total{job="checkout",code=~"5.."}[$w]))
          /
          sum by (job) (rate(http_requests_total{job="checkout"}[$w]))
YML
  done
} > slo-rules.yml
cat > slo-rules-test.yml <<'YML'
rule_files: [slo-rules.yml]
evaluation_interval: 1m
tests:
  - name: 30-minute incident at 3d
    interval: 1m
    input_series:
      - series: 'http_requests_total{job="checkout",code="200"}'
        values: '0+1999x4800'
      - series: 'http_requests_total{job="checkout",code="500"}'
        values: '0+1x4320 4520+200x29 10321+1x450'
    promql_expr_test:
      # before the incident: burn rate 0.5 everywhere
      - expr: round(job:slo_errors_per_request:ratio_rate1h / 0.001, 0.1)
        eval_time: 2d
        exp_samples: [{labels: '{job="checkout"}', value: 0.5}]
      # 9 minutes in: short windows react first, long windows barely move
      - expr: round(job:slo_errors_per_request:ratio_rate5m / 0.001, 0.1)
        eval_time: 3d9m
        exp_samples: [{labels: '{job="checkout"}', value: 91}]
      - expr: round(job:slo_errors_per_request:ratio_rate1h / 0.001, 0.1)
        eval_time: 3d9m
        exp_samples: [{labels: '{job="checkout"}', value: 15.4}]
      - expr: round(job:slo_errors_per_request:ratio_rate6h / 0.001, 0.1)
        eval_time: 3d9m
        exp_samples: [{labels: '{job="checkout"}', value: 3}]
      - expr: round(job:slo_errors_per_request:ratio_rate3d / 0.001, 0.1)
        eval_time: 3d9m
        exp_samples: [{labels: '{job="checkout"}', value: 0.7}]
      # 90 minutes after the end: short windows are back to normal, the 6h window remembers
      - expr: round(job:slo_errors_per_request:ratio_rate5m / 0.001, 0.1)
        eval_time: 3d2h
        exp_samples: [{labels: '{job="checkout"}', value: 0.5}]
      - expr: round(job:slo_errors_per_request:ratio_rate6h / 0.001, 0.1)
        eval_time: 3d2h
        exp_samples: [{labels: '{job="checkout"}', value: 8.7}]
YML
promtool test rules slo-rules-test.yml
  SUCCESS

The numbers tell the multi-window story from the previous lesson: 9 minutes in, the 5-minute burn rate is 91 and the 1-hour one has just crossed 14.4, while the 6-hour and 3-day windows have hardly noticed. An hour and a half after the fix, the short windows are back at 0.5, but the 6-hour window still shows 8.7.

Note: Why round(..., 0.1)? The ratio of two rates is a floating-point number such as 0.009999999999999998; promtool compares sample values exactly, so a test expecting 0.01 would fail. Rounding the burn rate to one decimal makes the expected values readable and stable.

Error budget remaining

Dashboards usually show the remaining budget over the whole SLO window. With a 30-day recording rule it is:

yaml
- record: job:slo_error_budget_remaining:ratio
  expr: 1 - (job:slo_errors_per_request:ratio_rate30d / (1 - 0.999))

A rate(...[30d]) over raw series is expensive on a big Prometheus. SLO generators avoid it by averaging an already recorded short-window ratio over 30 days (Sloth, for example, divides a sum_over_time of its 5-minute ratio by a count_over_time), which reads one series instead of every raw counter. See the Toolkit.

Test yourself

Why compute sum(rate(errors)) / sum(rate(all)) rather than averaging per-instance ratios?

  1. It is shorter
  2. So each instance counts in proportion to its traffic
  3. Prometheus can't average
  4. To include health checks

In promtool's series notation with interval: 1m, what does '0+5x60' describe?

  1. 5 samples of 60
  2. A series starting at 0 that grows by 5 every minute for 60 minutes (61 samples)
  3. A gauge of 0.5
  4. 60 random values

Why does the test round the burn rate before comparing it?

  1. promtool can't print decimals
  2. Ratios of rates carry floating-point noise, and promtool compares values exactly
  3. Rounding makes PromQL faster
  4. SLOs must be integers