Skip to main content

promcraft

pypi python Tests docs

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:

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.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for promcraft 0.7.2
File Size Uploaded Provenance Actions
promcraft-0.7.2.tar.gz 29.9 kB With provenance Details for promcraft-0.7.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for promcraft 0.7.2
File Interpreter ABI Platform Actions
promcraft-0.7.2-py3-none-any.whl Python 3 none any Details for promcraft-0.7.2-py3-none-any.whl

Total release size: 63.1 kB

Release files / promcraft-0.7.2.tar.gz

Download URL promcraft-0.7.2.tar.gz
Size 29.9 kB
Tags Source
SHA-256 checksum
How to use checksums
89b4ee1e0ebc8fea8d1a77243ea637073b3038ebe9f957929a5213bb9a332bca
BLAKE2b-256 checksum
How to use checksums
dfc82b73b9a19d7cb3fd63a6191006318b40c019a55d9211f16cd10086842efc
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 28, 2026.

Transparency log

Release files / promcraft-0.7.2-py3-none-any.whl

Download URL promcraft-0.7.2-py3-none-any.whl
Size 33.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
002b0f2243ec52558199d750f32afdd52b3453248f549cddb1f5265889cb4243
BLAKE2b-256 checksum
How to use checksums
4fad4b65374bf564a6987efc83d435af9bfebca5ad58e4e586dda7417c35455e
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 28, 2026.

Transparency log

Security for promcraft 0.7.2

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.

Select from 2 files

Attestation for promcraft-0.7.2.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 28, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page