← LeaderboardThe Skill Leaderboard

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.

Published by sickn33 ↗sickn33/agentic-awesome-skills

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: community skill 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 / live promtool query instant); it contains zero promtool test rules fixtures (alert_rule_test, input_series, eval_time, exp_alerts) and carries an external unbundled <support-root> notice. Keeping prometheus-alert-rule-testing separate preserves a strictly risk: safe, self-contained offline unit-testing boundary with clean source: self provenance.
  • prometheus-grafana and observability-monitoring-monitor-setup: describe cluster metric dashboards and static monitoring templates rather than unit-testing for: temporal windows with promtool test rules.

When to Use

  • Writing or reviewing Prometheus alert rules (expr, for, labels, annotations) before committing them to monitoring repos.
  • Verifying exact eval_time firing boundaries when evaluation_interval / rule interval and for interact.
  • Confirming that transient spikes shorter than for remain pending and clear without firing, and missing series stop firing or trigger absent() 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 promtool CLI (promtool --version, tested strictly on 3.16.0) on PATH or pointed to by PROMTOOL_BIN.
  • Python 3.10+ with pytest (tested with Python 3.12.10 and pytest==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

  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

  • promtool test rules evaluates rule YAML and synthetic input_series in 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_files in the test YAML.
  • Subquery timing or keep_firing_for semantics depend on the pinned promtool binary version (3.16.0).

Safety and Side Effects

  • Strictly offline and read-only when used with trusted synthetic fixtures: confines rules_filename and rule_files references strictly inside a temporary directory cleaned up after each test and invokes promtool test rules in-process with a 15s timeout. Do not feed untrusted YAML files without enforcing process/filesystem sandboxing around promtool.
  • Never modifies cluster PrometheusRule CRDs, Alertmanager configs, or live Prometheus endpoints.

Sources and related skills