Prometheus: Test Your Alerts with promtool
A promtool test is a small file that feeds fake series into your alerting rules and pins the exact alerts you expect. The test catches a broken rule before it reaches production.
Feed fake series into the rule
The shop is up for 20 steps, then down for the rest.
input_series:
- series: 'up{job="shopsentry"}'
values: 1+0x19 0+0x39No alert before the for time ends
At 4 minutes the alert must not exist yet.
- eval_time: 4m
alertname: ShopSentryDown
exp_alerts: []The alert fires after the for time
At 10 minutes the alert must exist with the critical label.
- eval_time: 10m
alertname: ShopSentryDown
exp_alerts:
- exp_labels:
severity: criticalExample
promtool test rules rules/shopsentry_test.ymlKey points
- A test file lists input series with values over time, like up going from 1 to 0.
- Each test case names an eval time and the alerts that must exist at that time.
- The expected alert lists its labels, so a wrong severity fails the test.
- The expected alert can also list its annotations, so a wrong message fails the test.
- A test at 4 minutes expects no alert, because the for clause is 5 minutes.
- A test at 10 minutes expects the firing alert, because the shop has been down for 5 minutes.
- Run the test with promtool test rules and the file path.
In this example, promtool runs the real test file against the real ShopSentryDown rule. The test passes because no alert exists at 4 minutes and the critical alert exists at 10 minutes. If a future edit shortens the for clause, this test fails and the mistake is caught.
Note
A test proves the rule logic, not the live service. The live fire test comes in a later lesson.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Rust Playground
Exercise
Why pin the expected value in an alert test?