Skip to content
Testing a Prometheus Exporter Without Prometheus

4 November 2026 · 3 min read

Testing a Prometheus Exporter Without Prometheus

observabilitytesting

The Checkmk exporter in my monitoring stack is about 400 lines of Python. It polls the Checkmk REST API, turns hosts and services into Prometheus gauges and serves them on a port. It also had no tests and no linter in CI, which is a poor combination for something an alert rule depends on.

What makes it testable#

Almost everything funnels through one function, api_get, which calls requests.get and returns JSON. Patch that and the rest is pure: parsing, normalising states, counting, setting gauges. And the prometheus_client library lets you read a gauge's current value from its in-process registry with REGISTRY.get_sample_value, so a test can assert on real metric output without starting a server or scraping anything.

What the 26 tests cover#

  • Parsing helpers. Performance data like user=12.18;;;; util=26.87;;;0;100 keeps only the value before the first semicolon, and malformed tokens are skipped instead of raising.
  • State mapping. Strings like UP, crit and unreachable, plus numbers, map to the right state, and unknown values fall back to a default.
  • The API layer. The auth header is built from credentials, a missing secret raises, an HTTP error raises with a truncated body so a huge error page cannot flood the logs.
  • Fetching. Hosts and services missing a name are skipped, and fallback fields (title, id) are used when the usual ones are absent.
  • The exported metrics. Counts by state, per-host problem totals, and a service metric parsed out of performance data.

The two tests I care most about#

Exporters have two classic failure modes, and each got a test:

  1. Stale series. If a host is deleted in Checkmk, its gauge must disappear from the next scrape instead of reporting its last value forever. The test runs one scrape with host old, another with host new, and asserts old is gone.
  2. Failed scrape. If the API call raises, the exporter must not wipe or corrupt what it already exported. The fetch happens before the gauges are cleared, and a test proves previous values survive.

CI without extra moving parts#

A new job creates a virtual environment, installs pinned dev dependencies (pytest, ruff), then runs ruff check and pytest. It needs no third-party action, since the runner already has Python, so there is nothing more to pin and keep updated. The runtime image is unchanged: the Dockerfile still copies only app.py.

I deliberately did not run ruff format in CI, because it would rewrite the existing file and bury the change in noise. Lint and tests first; style churn can be its own commit.