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:
{
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.ymlChecking slo-rules.yml
SUCCESS: 9 rules found
Notes on the expressions:
andbetween the long and the short window keeps only series present in both, i.e. both above the threshold.orjoins the two page conditions.- There is no
for:clause. The short window already filters out blips; aforwould 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:
{
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 '^