Skip to main content

Minimalist, Pythonic Prometheus HTTP client that supports both async and sync usage

Project description

aiopromql

codecov

aiopromql is a minimalist Prometheus HTTP client for Python that supports both synchronous and asynchronous querying. It provides a clean, Pythonic model layer for Prometheus query responses and convenient helpers for mapping metrics into structured time series.


🚀 Features

  • Sync and async Prometheus client interfaces via httpx
  • Pydantic models for Prometheus vector and matrix responses
  • Time series utilities and hashable metric keys
  • Zero dependencies outside of httpx and pydantic

📦 Installation

pip install aiopromql

🔧 Basic Usage

Synchronous Query

from aiopromql import PrometheusSync

# Initialize the client
client = PrometheusSync("http://localhost:9090")

# Execute a simple query
resp = client.query('up')
metric_map = resp.to_metric_map()

# Process the results
for labels, series in metric_map.items():
    print(f"Labels: {labels.dict}")
    for point in series:
        print(f"  {point}")

Ranged Query

from datetime import datetime, timedelta, timezone

# Set time range
end = datetime.now(timezone.utc)
start = end - timedelta(hours=1)

# Execute range query
resp = client.query_range('up', start=start, end=end, step='60s')
metric_map = resp.to_metric_map()

# Process results as before
for labels, series in metric_map.items():
    print(f"Labels: {labels.dict}")
    for point in series:
        print(f"  {point}")

Asynchronous Query

import asyncio
from aiopromql import PrometheusAsync

async def main():
    async with PrometheusAsync("http://localhost:9090") as client:
        # Execute multiple queries concurrently
        queries = ['up', 'process_cpu_seconds_total', 'process_resident_memory_bytes']
        tasks = [client.query(q) for q in queries]
        responses = await asyncio.gather(*tasks)

        # Process results
        for query, resp in zip(queries, responses):
            print(f"Results for query: {query}")
            metric_map = resp.to_metric_map()
            for labels, series in metric_map.items():
                print(f"  Labels: {labels.dict}")
                for point in series:
                    print(f"    {point}")

asyncio.run(main())

Asynchronous Ranged Query

from datetime import datetime, timedelta, timezone
from aiopromql import PrometheusAsync

async def get_range_data():
    async with PrometheusAsync("http://localhost:9090") as client:
        end = datetime.now(timezone.utc)
        start = end - timedelta(hours=1)

        resp = await client.query_range('up', start=start, end=end, step='60s')
        return resp.to_metric_map()

# Run the async function
metric_map = asyncio.run(get_range_data())

🚧 Development

For full guidelines on contributing, please see CONTRIBUTING.md.

🤝 Acknowledgments

This project is used in the DECICE — DEVICE-EDGE-CLOUD Intelligent Collaboration framEwork — aiming to bridge HPC cloud and edge orchestration.

Special thanks to the VeNIT Lab and other partners for their support and collaboration.

📄 License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiopromql-0.1.1.tar.gz (8.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiopromql-0.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file aiopromql-0.1.1.tar.gz.

File metadata

  • Download URL: aiopromql-0.1.1.tar.gz
  • Upload date:
  • Size: 8.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for aiopromql-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6ee4ca3481a2cf3002d0c3e314e63553c54d2c0212345009c27d3b03b7bff447
MD5 0559a5095455a9675f2feaeaf00635ce
BLAKE2b-256 aa46918bf7c1dbf09c0ee029b28b04f1f3bc73cf34ec8c7b4fdf23122ebc3158

See more details on using hashes here.

File details

Details for the file aiopromql-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: aiopromql-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for aiopromql-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 330a8d7a140459c2be77f08b26fde638b41866499576dfe4947f6d0aa8d7c175
MD5 44caa12fa3e5c25a1d4206cc943abe7e
BLAKE2b-256 80d0db88b9b5bab4c397d3031b88aad638061d0cab5291b7157eafcdd0233cbf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page