promcraft
A Python library for building Prometheus QL queries programmatically. Instead of constructing raw query strings, use composable Python objects that serialize to valid PromQL syntax.
Documentation: https://xbabka01.github.io/promcraft/
Installation
pip install promcraft
# or with Poetry
poetry add promcraft
Requires Python 3.11+.
Quick Start
>>> from promcraft import InstantVector, RangeVector, Label, Duration
>>> # Simple metric selector
>>> query = InstantVector("up", [])
>>> print(query)
up{}
>>> # With label filters
>>> query = InstantVector(
... "http_requests_total",
... [
... Label.eq("job", "api-server"),
... Label.eq("env", "production"),
... ],
... )
>>> print(query)
http_requests_total{ job = "api-server", env = "production" }
>>> # Range vector with rate()
>>> from promcraft import rate
>>> query = rate(
... RangeVector(
... "http_requests_total",
... [
... Label.eq("job", "api-server"),
... ],
... Duration(m=5),
... )
... )
>>> print(query)
rate(http_requests_total{ job = "api-server" }[5m])
>>> # Binary operation
>>> left = InstantVector("http_requests_total", [Label.eq("status", "500")])
>>> right = InstantVector("http_requests_total", [])
>>> ratio = left / right
>>> print(ratio)
http_requests_total{ status = "500" } / http_requests_total{}
Raw values instead of wrapper types
Anywhere a String is expected (label values, count_values, label-manipulation function
arguments), you can pass a plain str instead — it's converted with String.from_value()
under the hood. Anywhere a Scalar is expected (Float-typed function/aggregation
parameters, at, binary-operator operands), you can pass a plain float/int instead,
converted with Float.from_value(). Range, resolution and offset values are converted with
Duration.from_value() instead, so offset=300 renders as offset 5m. Passing an existing
String/Scalar instance through is a no-op, so both styles compose freely:
>>> from promcraft import InstantVector, Label, topk
>>> v = InstantVector("http_requests_total", [])
>>> print(Label.eq("job", "api-server")) # same as Label.eq("job", String("api-server"))
job = "api-server"
>>> print(topk(5, v)) # same as topk(Float(5), v)
topk(5, http_requests_total{})
Composing and formatting
Every Query composes with every other, and to_string() can pretty-print or match
Prometheus' own formatting:
>>> from promcraft import InstantVector, Duration, Label, Style, rate, sum_
>>> expr = sum_(rate(InstantVector("up", [Label.eq("job", "api")])[Duration(m=5)])).by(["job"])
>>> print(expr)
sum(rate(up{ job = "api" }[5m])) by (job)
>>> print(expr.to_string(style=Style.prometheus()))
sum by (job) (rate(up{job="api"}[5m]))
>>> print(expr.to_string(indent=4))
sum(
rate(
up{
job = "api"
}[5m]
)
) by (job)
Documentation
The user guide covers every building block with examples:
- Literals and variables:
Float,Hex,Duration,String, GrafanaVariable - Selectors and subqueries:
Label,InstantVector,RangeVector,Subquery - Binary operators: arithmetic, comparison, set operators,
bool, vector matching - Aggregations:
sum_,topk,count_values,by/without, ... - Functions: every PromQL function
- Composing and formatting: pretty printing and
Style - Recipes: error ratios, SLO burn rates, latency percentiles, Grafana dashboards, ...
- Errors and validation: the mistakes promcraft catches while building a query
The API reference is generated from the docstrings.
Development
# Install dependencies
poetry install
# Run tests (includes type checking and linting)
poetry run pytest
# Run only unit tests
poetry run pytest -o addopts=""
# Build and preview the docs
poetry install --with docs
poetry run zensical serve
The test suite uses pytest with pytest-mypy for static type checking and pytest-ruff for linting. All checks run together by default.
Metadata
Release files for promcraft 0.7.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| promcraft-0.7.1.tar.gz | 29.9 kB | With provenance | Details for promcraft-0.7.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| promcraft-0.7.1-py3-none-any.whl | Python 3 | none | any | Details for promcraft-0.7.1-py3-none-any.whl |
Total release size: 63.1 kB
Release files / promcraft-0.7.1.tar.gz
| Download URL | promcraft-0.7.1.tar.gz |
|---|---|
| Size | 29.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
07885b5a6bb36b0fca01a6cb172bb4f56cc58c28bca0152f967d949af3026ce8
|
|
BLAKE2b-256 checksum How to use checksums |
e9862793baa422f3d71d6ade515bf99e4f77bcc699220951bc267981a14ca880
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.
Transparency logRelease files / promcraft-0.7.1-py3-none-any.whl
| Download URL | promcraft-0.7.1-py3-none-any.whl |
|---|---|
| Size | 33.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fd62b61e0f5b7e997e1f8301163239be2a81bd5d7008342a54b687344464e77f
|
|
BLAKE2b-256 checksum How to use checksums |
594cc1736b856fb5c038db3aec1e3835c7c3578d63c18510c0b07a81e52b21ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.
Transparency logSecurity for promcraft 0.7.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for promcraft-0.7.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.