Prometheus Async

26.1.0 · active · verified Thu Apr 16

prometheus-async provides asynchronous helpers for the `prometheus_client` library, making it easier to expose metrics from `asyncio` and `Twisted` applications. Maintained by Hynek Schlawack, the library is currently at version 26.1.0 and receives regular updates, often focusing on type hinting and internal improvements, with occasional breaking changes for major Python versions.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to start a Prometheus HTTP server using `prometheus_async.aio.web.start_http_server` and how to apply the `prometheus_async.aio.time` decorator to an asynchronous function. The metrics server runs in the background, making metrics available at `/metrics` on the specified port.

import asyncio
from prometheus_client import Gauge
from prometheus_async.aio.web import start_http_server
from prometheus_async.aio import time

# Define a metric
MY_GAUGE = Gauge('my_async_gauge', 'Description of my async gauge')

@time(MY_GAUGE)
async def some_async_function():
    """An example async function that does some work."""
    print("Running some_async_function...")
    MY_GAUGE.set(10)
    await asyncio.sleep(0.5)
    MY_GAUGE.set(20)
    print("some_async_function finished.")

async def main():
    # Start the Prometheus HTTP server in the background
    # It will expose metrics on http://localhost:8000/metrics by default
    server = await start_http_server(port=8000)
    print("Prometheus metrics server started on port 8000")

    # Run our async function
    await some_async_function()

    print("Application finished. Metrics server still running. Press Ctrl+C to exit.")
    # Keep the event loop running to allow the metrics server to serve requests
    await asyncio.Future() # This will run forever until cancelled

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\nExiting.")

view raw JSON →