prometheus-alert-rule-testing
Unit-test Prometheus alerting rules offline with pinned promtool test rules across pending/firing states, for-duration clearing, labels, annotations, and missing series.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: prometheus-alert-rule-testingdescription: "Unit-test Prometheus alerting rules offline with pinned promtool test rules across pending/firing states, for-duration clearing, labels, annotations, and missing series."category: devopsrisk: safesource: selfsource_type: selfdate_added: "2026-12-01"author: Koushik-Katkamtags: [prometheus, promtool, alerting, promql, unit-testing, reliability]Prometheus Alert Rule Testing
Overview
Verify Prometheus alerting rules deterministically offline using the real promtool test rules command against synthetic input_series time series (promtool==3.16.0), without deploying Prometheus, touching live Alertmanager receivers, or implementing a custom PromQL evaluator. Tests pending vs. firing state transitions across for durations, early threshold clearing before firing, exact emitted exp_labels and templated exp_annotations, missing time-series behavior (_ gaps and absent()), evaluation interval boundaries, and non-zero exit codes on failing rule/test fixtures.
Overlap evaluation against prometheus-configuration and related skills:
prometheus-configuration:risk: critical,source: communityskill focused on cluster Helm installation, Docker Compose setup, scrape configs, recording rules, sample alert definitions, and static syntax checks (promtool check config/promtool check rules/ livepromtool query instant); it contains zeropromtool test rulesfixtures (alert_rule_test,input_series,eval_time,exp_alerts) and carries an external unbundled<support-root>notice. Keepingprometheus-alert-rule-testingseparate preserves a strictlyrisk: safe, self-contained offline unit-testing boundary with cleansource: selfprovenance.prometheus-grafanaandobservability-monitoring-monitor-setup: describe cluster metric dashboards and static monitoring templates rather than unit-testingfor:temporal windows withpromtool test rules.
When to Use
- Writing or reviewing Prometheus alert rules (
expr,for,labels,annotations) before committing them to monitoring repos. - Verifying exact
eval_timefiring boundaries whenevaluation_interval/ ruleintervalandforinteract. - Confirming that transient spikes shorter than
forremain pending and clear without firing, and missing series stop firing or triggerabsent()alerts as intended. - Do not use this skill to test Alertmanager routing trees, inhibition/silences, webhook receiver delivery, or live cluster scrape health.
- Restrict the supplied Python harness to trusted synthetic fixtures unless explicit file-access confinement is enforced for rule and test inputs.
Prerequisites
- Official
promtoolCLI (promtool --version, tested strictly on3.16.0) onPATHor pointed to byPROMTOOL_BIN. - Python
3.10+withpytest(tested with Python3.12.10andpytest==9.1.1) to execute the isolated temporary-directory harness. - No running Prometheus server, Alertmanager instance, or network access required.
How It Works
1. Define deterministic rule and test YAML fixtures
- Pin the test group
interval(for example1m) and ruleforduration (for example2m) explicitly so step boundaries ininput_seriesmap unambiguously toeval_time. - Express
input_seriesstep counts cleanly (promtool1mstep intervals:0 6 6 6 6evaluates at0m,1m,2m,3m,4m). Notice afor: 2mrule first true att = 1mbegins pending at1m, remains non-firing at2m, and fires att = 3m(3m - 1m = 2m). - Represent scrape dropouts or missing samples explicitly with
_rather than0, accounting for Prometheus's half-open(t - 5m, t]lookback delta (a sample at0mexpires at5m) and the fact thatabsent(up{job="payments"})automatically propagates equality matcher labels (job="payments").
2. Run pinned promtool test rules with positive and negative assertions
Run the local verification suite in promtool test rules harness:
- Evaluation boundaries (
exp_alerts: []vs firing): Assertexp_alerts: []ateval_time: 1mand2m(demonstrating absence of firing alerts beforefor: 2melapses, which together with firing ateval_time: 3mevidences pending-to-firing progression rather than proving internal pending state directly) and exact firing alert ateval_time: 3m. - Clears before firing: Feed
0 6 6 0 0and assert zero firing alerts ateval_time: 2m,3m, and4m. - Exact labels and annotations: Assert
exp_labels(rule labels + metric labels) and renderedexp_annotations({{ $labels.service }},{{ $value }}). - Missing time-series behavior: Verify threshold alerts clear after missing
_gaps exceed the5mlookback delta andabsent(up{job="payments"}) == 1fires onceforelapses both after a series disappears and when no samples exist at all. - Failing fixtures (
exit code != 0): Execute intentionally broken mutations (wrongforduration or mismatched expected annotation) via realpromtool test rulesand confirm non-zero exit status +FAILED:diagnostic diff, distinct from malformed YAML syntax (INVALID_INPUT).
Examples
Run the self-contained verification suite in promtool test rules harness:
User request: "Verify with promtool that HighErrorRate waits 2m before firing, clears on transient spikes, and handles missing targets."
Inputs: Synthetic Prometheus alert rule group + promtool test YAML executed via promtool 3.16.0.
Expected result: Passing suite exits 0 across pending (2m), firing (3m), early-clear (0 6 6 0 0), exact labels/annotations,
and missing-series cases; broken rule/expectation mutations exit 1 with promtool FAILED diffs.
Limitations
promtool test rulesevaluates rule YAML and syntheticinput_seriesin isolation; it never tests Alertmanager routing, grouping, inhibition, silences, receiver credentials, notification delivery, or production scrape reliability.- Recording-rule chains across separate rule files require including all referenced
rule_filesin the test YAML. - Subquery timing or
keep_firing_forsemantics depend on the pinnedpromtoolbinary version (3.16.0).
Safety and Side Effects
- Strictly offline and read-only when used with trusted synthetic fixtures: confines
rules_filenameandrule_filesreferences strictly inside a temporary directory cleaned up after each test and invokespromtool test rulesin-process with a15stimeout. Do not feed untrusted YAML files without enforcing process/filesystem sandboxing aroundpromtool. - Never modifies cluster
PrometheusRuleCRDs, Alertmanager configs, or live Prometheus endpoints.