TExcelTAD
Home › Site Reliability Engineering: SLOs, Incidents & Chaos › Burn-Rate Alert Rules & Their Tests

Burn-Rate Alert Rules & Their Tests

This lesson turns the multi-window, multi-burn-rate table into Prometheus alerting rules on top of the recording rules, then proves with promtool test rules that they page when they should, stay quiet when they should, and resolve on time. An alert that has never fired in a test is an alert you hope works.

The rules

Both severities use one alert name, CheckoutErrorBudgetBurn, with a severity label. That is the convention SLO generators follow too: Alertmanager routes on the label (next lessons), and the page can inhibit the ticket for the same problem. The thresholds are written as burn rate × (1 − SLO), exactly as in the workbook's examples, so the SLO is visible in the expression:

bash · try it yourself
{
  echo "groups:"
  echo "  - name: slo-checkout"
  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
# the alerts go into the same group, after the recording rules they read
cat >> slo-rules.yml <<'YML'
      - alert: CheckoutErrorBudgetBurn
        expr: |
          (
            job:slo_errors_per_request:ratio_rate1h{job="checkout"} > (14.4 * 0.001)
            and
            job:slo_errors_per_request:ratio_rate5m{job="checkout"} > (14.4 * 0.001)
          )
          or
          (
            job:slo_errors_per_request:ratio_rate6h{job="checkout"} > (6 * 0.001)
            and
            job:slo_errors_per_request:ratio_rate30m{job="checkout"} > (6 * 0.001)
          )
        labels:
          severity: page
        annotations:
          summary: "checkout is burning its 30-day error budget fast"
      - alert: CheckoutErrorBudgetBurn
        expr: |
          (
            job:slo_errors_per_request:ratio_rate1d{job="checkout"} > (3 * 0.001)
            and
            job:slo_errors_per_request:ratio_rate2h{job="checkout"} > (3 * 0.001)
          )
          or
          (
            job:slo_errors_per_request:ratio_rate3d{job="checkout"} > 0.001
            and
            job:slo_errors_per_request:ratio_rate6h{job="checkout"} > 0.001
          )
        labels:
          severity: ticket
        annotations:
          summary: "checkout is burning its 30-day error budget slowly"
YML
promtool check rules slo-rules.yml
Checking slo-rules.yml
  SUCCESS: 9 rules found

Notes on the expressions:

  • and between the long and the short window keeps only series present in both, i.e. both above the threshold. or joins the two page conditions.
  • There is no for: clause. The short window already filters out blips; a for would only delay real pages.
  • The ticket rules repeat the 6-hour ratio used by the page; recording rules make that free.
  • The alerts live in the same rule group as the recording rules, after them. Rules inside a group run in order on every evaluation, so the alerts always read ratios recorded in that same evaluation. Groups run independently of each other: with the alerts in a separate group they would sometimes read the previous minute's ratios, and a test that checks the exact minute an alert starts would pass on one run and fail on the next.

Testing the alerts

alert_rule_test evaluates an alert at a given time and compares the firing alerts (labels and annotations) with exp_alerts; an empty list asserts silence. The session tests two scenarios on three days of normal traffic (burn rate 0.5):

  • a fast burn: about 9% errors for 30 minutes starting at 3d0m. Expected: nothing at 8 minutes, a page at 9 minutes, page and ticket at 30 minutes, the page resolved 28 minutes after the end (the 30-minute short window has to clear) while the ticket stays, and silence at 3d7h once the 6-hour short window has forgotten the incident;
  • a slow burn: 0.5% errors (burn rate 5) from 3d0m. It never pages; the 3-day/6-hour ticket rule opens a ticket after about eight hours.

Then the session makes a change a reviewer might suggest, for: 15m on the page, and runs the same tests:

bash · try it yourself
{
  echo "groups:"
  echo "  - name: slo-checkout"
  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
# the alerts go into the same group, after the recording rules they read
cat >> slo-rules.yml <<'YML'
      - alert: CheckoutErrorBudgetBurn
        expr: |
          (
            job:slo_errors_per_request:ratio_rate1h{job="checkout"} > (14.4 * 0.001)
            and
            job:slo_errors_per_request:ratio_rate5m{job="checkout"} > (14.4 * 0.001)
          )
          or
          (
            job:slo_errors_per_request:ratio_rate6h{job="checkout"} > (6 * 0.001)
            and
            job:slo_errors_per_request:ratio_rate30m{job="checkout"} > (6 * 0.001)
          )
        labels:
          severity: page
        annotations:
          summary: "checkout is burning its 30-day error budget fast"
      - alert: CheckoutErrorBudgetBurn
        expr: |
          (
            job:slo_errors_per_request:ratio_rate1d{job="checkout"} > (3 * 0.001)
            and
            job:slo_errors_per_request:ratio_rate2h{job="checkout"} > (3 * 0.001)
          )
          or
          (
            job:slo_errors_per_request:ratio_rate3d{job="checkout"} > 0.001
            and
            job:slo_errors_per_request:ratio_rate6h{job="checkout"} > 0.001
          )
        labels:
          severity: ticket
        annotations:
          summary: "checkout is burning its 30-day error budget slowly"
YML
cat > slo-test.yml <<'YML'
rule_files: [slo-rules.yml]
evaluation_interval: 1m
tests:
  - name: fast burn (about 9% errors for 30 minutes)
    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'
    alert_rule_test:
      - eval_time: 3d8m
        alertname: CheckoutErrorBudgetBurn
        exp_alerts: []
      - eval_time: 3d9m
        alertname: CheckoutErrorBudgetBurn
        exp_alerts:
          - exp_labels: {job: checkout, severity: page}
            exp_annotations: {summary: "checkout is burning its 30-day error budget fast"}
      - eval_time: 3d30m
        alertname: CheckoutErrorBudgetBurn
        exp_alerts:
          - exp_labels: {job: checkout, severity: page}
            exp_annotations: {summary: "checkout is burning its 30-day error budget fast"}
          - exp_labels: {job: checkout, severity: ticket}
            exp_annotations: {summary: "checkout is burning its 30-day error budget slowly"}
      - eval_time: 3d58m
        alertname: CheckoutErrorBudgetBurn
        exp_alerts:
          - exp_labels: {job: checkout, severity: ticket}
            exp_annotations: {summary: "checkout is burning its 30-day error budget slowly"}
      - eval_time: 3d7h
        alertname: CheckoutErrorBudgetBurn
        exp_alerts: []
  - name: slow burn (0.5% errors, burn rate 5)
    interval: 1m
    input_series:
      - series: 'http_requests_total{job="checkout",code="200"}'
        values: '0+1999x5000'
      - series: 'http_requests_total{job="checkout",code="500"}'
        values: '0+1x4320 4330+10x679'
    alert_rule_test:
      - eval_time: 3d7h50m
        alertname: CheckoutErrorBudgetBurn
        exp_alerts: []
      - eval_time: 3d8h10m
        alertname: CheckoutErrorBudgetBurn
        exp_alerts:
          - exp_labels: {job: checkout, severity: ticket}
            exp_annotations: {summary: "checkout is burning its 30-day error budget slowly"}
YML
promtool test rules slo-test.yml
# a reviewer asks for "for: 15m" on the page, to be safe from flapping
yq -i '(.groups[0].rules[] | select(.labels.severity == "page")).for = "15m"' slo-rules.yml
promtool test rules slo-test.yml 2>&1 | grep -v '^
  

 | head -n 9
echo "exit ${PIPESTATUS[0]}"
  SUCCESS

  FAILED:
    name: fast burn (about 9% errors for 30 minutes),
    alertname: CheckoutErrorBudgetBurn, time: 3d9m,
        exp:[
            0:
              Labels:{alertname="CheckoutErrorBudgetBurn", job="checkout", severity="page"}
              Annotations:{summary="checkout is burning its 30-day error budget fast"}
            ],
        got:[]
exit 1

The tests pass as written. With for: 15m the first assertion that changes is the page at 3d9m: the alert is only pending there, and pending alerts don't count as firing, so got is empty. The test turned a vague argument ("for makes it safer") into a measurable cost: 15 more minutes before anyone is woken, while 9% of checkouts fail.

Tip: Finding the exact minute when an alert starts or stops is easiest by writing a test that expects exp_alerts: [] at several times and reading the failures, then turning what you learned into real assertions.

What to test for every SLO alert

Case Assertion
normal traffic no alert at all
short blip (a few bad minutes) no page
fast burn page within the expected minutes
slow burn ticket, no page
recovery page resolves within the short window
missing data (no requests at all) decide: no alert, or a separate "no traffic" alert

The last row is a real trap: with zero requests, the ratio is 0/0, PromQL returns nothing, and the burn-rate alert is silent while the service may be completely unreachable. Pair SLO alerts with an absent() or traffic-drop alert, or a synthetic probe.

Running the tests in CI

Keep rules and tests together in the repository and run both commands in the pipeline:

yaml
# .github/workflows/rules.yml (excerpt)
- name: Check and test Prometheus rules
  run: |
    promtool check rules slo/*-rules.yml slo/*-alerts.yml
    promtool test rules slo/*-test.yml

Test yourself

Why does the page rule have no for: clause?

  1. Prometheus forbids it
  2. The short window already suppresses blips; for would only delay real pages
  3. It is set in Alertmanager
  4. for only works for tickets

In alert_rule_test, what does exp_alerts: [] assert?

  1. The test is skipped
  2. No alert of that name is firing at that time
  3. Any alert is fine
  4. The alert is pending

A service receives no requests at all because DNS is broken. What do the burn-rate alerts do?

  1. Page immediately
  2. Stay silent, because the error ratio 0/0 has no value
  3. Open a ticket
  4. Fire the ticket and the page