DEV Community

Amaresh Pelleti
Amaresh Pelleti

Posted on Originally published at devtoolhub.com

Test Prometheus Alert Rules in GitHub Actions: promtool

Originally published on DevToolHub.

Flow diagram to test Prometheus alert rules in GitHub Actions: a pull request runs promtool check rules, then promtool test rules

To test Prometheus alert rules in GitHub Actions, run promtool check rules and promtool test rules on every pull request that touches the rule files. The first command validates the rule files but says nothing about what an alert does. However, the second feeds fake metrics to your rules and fails the job when an alert fires, or stays quiet, when it should not.

I tested this on 30 November 2026 with promtool 3.16.0, locally in Docker and on a GitHub-hosted runner. A threshold typo, a wrong for: duration and a wrong label all passed check rules and failed test rules. One edit passed both until I added a test with a step change.

In this guide:

  • Write an alert rule with a unit test that covers firing, waiting and quiet cases
  • Run both promtool commands in a pull request workflow
  • See which rule edits check rules misses and which a test catches
  • Avoid two traps: tests that pass without testing, and flat test data

What do promtool check rules and test rules each catch?

promtool check rules parses the rule files and reports syntax errors. promtool test rules loads a test file, replays the input series you wrote, and compares the alerts or query results at chosen times against what you expect. The first command checks that a rule is valid. The second checks that it behaves the way you intended.

Here is the alert I used. It fires an alert, labeled severity: page, when more than 5% of requests return a 5xx status for ten minutes. Whether that alert pages anyone depends on Alertmanager routing, which I did not test:

groups:
  - name: http
    rules:
      - alert: HighErrorRate
        expr: |
          sum by (job) (rate(http_requests_total{code=~"5.."}[5m]))
            / sum by (job) (rate(http_requests_total[5m])) > 0.05
        for: 10m
        labels:
          severity: page
        annotations:
          summary: "{{ $labels.job }} error rate is {{ $value | humanizePercentage }}"
Enter fullscreen mode Exit fullscreen mode

How do you unit test Prometheus alert rules with promtool?

First describe the metric series with the expanding notation. Then list the alerts you expect at specific evaluation times. A value like 0+90x30 starts at 0 and adds 90 for each of 30 further samples. The Prometheus docs define the notation and the fields.

This test checks silence at 5 minutes and firing at 20 minutes. It does not pin the exact minute the alert switches, so add more eval_time points around the boundary if the for: duration matters to you:

rule_files:
  - ../rules/alerts.yml
evaluation_interval: 1m
tests:
  - name: error rate above 5 percent pages after 10 minutes
    interval: 1m
    input_series:
      - series: 'http_requests_total{job="api", code="200"}'
        values: '0+90x30'
      - series: 'http_requests_total{job="api", code="500"}'
        values: '0+10x30'
    alert_rule_test:
      - eval_time: 5m
        alertname: HighErrorRate
        exp_alerts: []
      - eval_time: 20m
        alertname: HighErrorRate
        exp_alerts:
          - exp_labels:
              severity: page
              job: api
            exp_annotations:
              summary: "api error rate is 10%"
Enter fullscreen mode Exit fullscreen mode

At 5 minutes the alert is still pending because of for: 10m. So no alert is expected. At 20 minutes it fires with the labels and annotation you wrote. I added three more tests to the same file. The last one, where errors start late, is the one that catches a changed rate() window:

  - name: errors that start at minute 10 page by minute 26
    interval: 1m
    input_series:
      - series: 'http_requests_total{job="api", code="200"}'
        values: '0+90x40'
      - series: 'http_requests_total{job="api", code="500"}'
        values: '0x10 0+10x30'
    alert_rule_test:
      - eval_time: 26m
        alertname: HighErrorRate
        exp_alerts:
          - exp_labels:
              severity: page
              job: api
            exp_annotations:
              summary: "api error rate is 10%"
Enter fullscreen mode Exit fullscreen mode

The whole file is five tests:

Test name Series Expectation
one percent errors stay quiet 99 ok, 1 error per minute no alert at 25m
errors that start at minute 10 steady traffic, errors begin late alert at 26m
no 5xx series at all only code="200" exists no alert at 25m
traffic that stops samples end with _x20 (missing) no alert at 25m

The _ value in the input stands for a missing sample. The last two rows check that a missing numerator or denominator does not produce a false page.

How do you test Prometheus alert rules in a GitHub Actions workflow?

Trigger the workflow on pull requests that change the monitoring files, download a pinned promtool from the release page, verify its checksum, then run both commands. The check step comes first so a syntax error fails fast with a clear message. This is the workflow that passed on a runner:

name: Alert rule tests
on:
  pull_request:
    paths:
      - "monitoring/**"
permissions:
  contents: read
jobs:
  promtool:
    runs-on: ubuntu-latest
    env:
      PROM_VERSION: "3.16.0"
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
      - name: Install promtool
        run: |
          base="https://github.com/prometheus/prometheus/releases"
          base="${base}/download/v${PROM_VERSION}"
          tar_name="prometheus-${PROM_VERSION}.linux-amd64.tar.gz"
          curl -fsSL -O "${base}/${tar_name}"
          curl -fsSL "${base}/sha256sums.txt" | grep "${tar_name}" | sha256sum -c -
          tar -xzf "${tar_name}" --strip-components=1 \
            "prometheus-${PROM_VERSION}.linux-amd64/promtool"
          sudo install promtool /usr/local/bin/promtool
      - name: Check and test the rules
        working-directory: monitoring
        run: |
          promtool check rules rules/alerts.yml
          promtool test rules tests/alerts_test.yml
Enter fullscreen mode Exit fullscreen mode

Workflow-only edits do not match the monitoring/** filter. Add the workflow file's own path to paths if you want those validated too; I did not test that variant. My repository keeps rules/ and tests/ under monitoring/, which is why the last step sets working-directory. The test file's rule_files path is relative to the test file.

The alternative is the Docker image, which needs no install step. Call docker run --rm -v "$PWD/monitoring":/w -w /w --entrypoint promtool prom/prometheus:v3.16.0 test rules tests/alerts_test.yml. On the runner the Docker job took 13 seconds and the tarball job 7 seconds, The Docker job also pulls the image. I did not time the steps separately to confirm that is the whole difference. Either is fast. Plus, the tarball path lets you verify a checksum, which the security best practices guide recommends for downloaded tools.

For other testing patterns in a workflow, see the GitHub Actions testing guide.

Which rule edits does check rules miss and test rules catch?

I made one edit at a time to the rule file and ran both commands locally with promtool 3.16.0. check rules exited 0 for every edit except the syntax error.

Edit to the rule check rules test rules
Threshold 0.05 to 0.5 passes fails: expected an alert at 20m, got none
for: 10m to for: 0m passes fails: expected none at 5m, got an alert
severity: page to warning passes fails: label mismatch
Alert renamed HighErrorRat passes fails: expected an alert, got none
Template $labels.job to $labels.jobb passes fails: annotation mismatch
Window [5m] to [30m] passes passes with two flat tests, fails with the step-change test
Missing parenthesis in the expression fails, exit 1 fails, exit 1

The window row is the one to remember. My first two tests used constant error rates. As a result, a 5-minute and a 30-minute window gave the same answer. Only the test where errors start late told them apart. A test with flat input only proves the rule works on flat input.

On the runner I first opened three pull requests against a three-test version of the file. A no-op comment passed. The threshold edit failed both the Docker and the tarball job. The syntax slip failed both jobs. In the tarball job the log showed the parse error at check rules, the same one I saw locally. After that, the workflow below and the full five-test file passed on a runner in a fourth pull request.

Why can a promtool test pass when the alert is broken?

Because an empty expectation is satisfied by an alert name that does not exist, so a typo goes unnoticed. I misspelled alertname in the "stay quiet" test, then changed the rule so it fires on any error. promtool test rules printed SUCCESS and exited 0. With the correct name the same rule failed the test.

Warning: A test with exp_alerts: [] can pass while the alert is wrong if its alertname matches no rule. A separate "fires" test with the correct name does not catch a typo in the quiet test. Check that every alertname in your tests exists in the rule files, and break each rule on purpose once to confirm its quiet test goes red.

Two flags helped me read failures. --diff is experimental and prints a colored comparison of expected and received alerts, for example "page" => "warning". --junit=out.xml wrote a JUnit file with tests="3" failures="0" for the passing run, which most CI systems can display.

What I did not test

I tested one alerting rule, promtool 3.16.0 and five unit tests. Untested: promql_expr_test blocks, recording rules, Alertmanager routing, promtool check config, other Prometheus versions, pinning the Docker image by digest, and publishing JUnit results in the workflow. The two regression pull requests on the runner used the three-test version of the file. The five-test version passed on the runner but I did not plant regressions against it there. I did not check which ubuntu-latest image version the runner resolved to.

The Grafana Tempo 3.1 article covers another part of the observability stack, and the GitHub Actions monitoring guide covers watching the pipeline itself.

Frequently Asked Questions

Q: How do I test Prometheus alert rules in CI?
A: Run promtool check rules and promtool test rules on pull requests that change the rule files. The test command replays input series you define and fails when the alerts at your chosen times differ from what you expect. Both commands exit 1 on failure.

Q: What is the difference between promtool check rules and test rules?
A: check rules validates the rule files but does not test behavior. In my test it passed after a threshold typo, a wrong for: duration and a wrong label. test rules ran the alert against fake data and failed on all three.

Q: Why does my promtool test pass when the alert is wrong?
A: Check the alertname and the test data. A test that expects no alerts passes even when the alert name is mistyped. Flat input data can also hide a wrong rate() window. Add a test where the error rate changes over time.

Q: Should I install promtool or use the Docker image in GitHub Actions?
A: Either works. On my runner the Docker job took 13 seconds and the tarball job 7 seconds. The tarball lets you check a SHA-256 checksum. The image is simpler because it needs no install step.

Quick Summary:

  • promtool check rules validates the rule files, and it passed after every behavior change I made except a syntax error
  • promtool test rules caught a wrong threshold, for: duration, label, alert name and template field
  • A window change from [5m] to [30m] passed two flat tests and failed only a step-change test
  • A "stays quiet" test with a mistyped alertname can pass while the rule is wrong
  • On the runner, the Docker job took 13 seconds and the tarball job 7 seconds
  • Pin the promtool version and verify the checksum of the download

Copy the rule and the test file into a scratch branch and break the threshold on purpose. Then confirm that the pull request goes red and that the test, not the file validation, is what failed. That is the fastest way to learn whether you can really test Prometheus alert rules in GitHub Actions.

Top comments (0)