Home / Market / prometheus alert rule testing

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.

skill★ 508960 dlgithubCC-BY-4.0 content; MIT code; upstream exceptions applyInstall in CosmosUpstream Source

Documentation & Preview

--- name: prometheus-alert-rule-testing description: "Unit-test Prometheus alerting rules offline with pinned promtool test rules across pending/firing states, for-duration clearing, labels, annotations, and missing series." category: devops risk: safe source: self source_type: self date_added: "2026-12-01" author: Koushik-Katkam tags: [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:

When to Use

Prerequisites

How It Works

1. Define deterministic rule and test YAML fixtures

  1. Pin the test group interval (for example 1m) and rule for duration (for example 2m) explicitly so step boundaries in input_series map unambiguously to eval_time.
  2. Express input_series step counts cleanly (promtool 1m step intervals: 0 6 6 6 6 evaluates at 0m, 1m, 2m, 3m, 4m). Notice a for: 2m rule first true at t = 1m begins pending at 1m, remains non-firing at 2m, and fires at t = 3m (3m - 1m = 2m).
  3. Represent scrape dropouts or missing samples explicitly with _ rather than 0, accounting for Prometheus's half-open (t - 5m, t] lookback delta (a sample at 0m expires at 5m) and the fact that absent(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:

  1. Evaluation boundaries (exp_alerts: [] vs firing): Assert exp_alerts: [] at eval_time: 1m and 2m (demonstrating absence of firing alerts before for: 2m elapses, which together with firing at eval_time: 3m evidences pending-to-firing progression rather than proving internal pending state directly) and exact firing alert at eval_time: 3m.
  2. Clears before firing: Feed 0 6 6 0 0 and assert zero firing alerts at eval_time: 2m, 3m, and 4m.
  3. Exact labels and annotations: Assert exp_labels (rule labels + metric labels) and rendered exp_annotations ({{ $labels.service }}, {{ $value }}).
  4. Missing time-series behavior: Verify threshold alerts clear after missing _ gaps exceed the 5m lookback delta and absent(up{job="payments"}) == 1 fires once for elapses both after a series disappears and when no samples exist at all.
  5. Failing fixtures (exit code != 0): Execute intentionally broken mutations (wrong for duration or mismatched expected annotation) via real promtool test rules and 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

Safety and Side Effects

Sources and related skills