How do you unit test alerting rules?
AdvancedConceptPromQL & Recording Rules
Answer
With promtool test rules and synthetic series:
rule_files: [api-alerts.yml]
evaluation_interval: 1m
tests:
- interval: 1m
input_series:
- series: 'job:http_errors:ratio_rate5m{job="api"}'
values: '0.01 0.08x15'
alert_rule_test:
- eval_time: 12m
alertname: APIHighErrorRate
exp_alerts:
- exp_labels: { job: api, severity: page, team: payments }
0.08x15 means 0.08 repeated 15 more times. We run this in CI so threshold and label changes are reviewed with proof.
More on PromQL & Recording Rules
What are the PromQL data types?BeginnerWhat label matchers does PromQL support?BeginnerExplain `rate()`.Beginner`rate()` vs `irate()`: what is the difference?IntermediateWhat does `increase()` return and why is it sometimes a non-integer?IntermediateWhy must you `rate()` before `sum()`, never the reverse?Intermediate