Prometheus Async
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
-
AttributeError: module 'prometheus_async.aio' has no attribute 'start_http_server'
cause Incorrect import path for `start_http_server` in the `aio` module.fixChange the import to `from prometheus_async.aio.web import start_http_server`. -
TypeError: start_http_server() got an unexpected keyword argument 'loop'
cause Attempting to pass the `loop` argument to `start_http_server()` on a version where it has been removed.fixRemove the `loop=...` argument from your call to `start_http_server()`. The library handles the event loop automatically. -
ModuleNotFoundError: No module named 'prometheus_async.aio'
cause `prometheus-async` library is not installed or there's a problem with your Python environment's path.fixRun `pip install prometheus-async` to install the library. If already installed, check your virtual environment activation or `PYTHONPATH`.
Warnings
- breaking Dropped support for Python 2.7, 3.5, and 3.6. `prometheus-async` now requires Python 3.9 or newer.
- breaking The `loop` argument has been removed from `prometheus_async.aio.web.start_http_server()`.
- gotcha Ensure you import from the correct asynchronous backend (`aio` for asyncio/aiohttp or `twisted` for Twisted). Mixing them or using the wrong one will lead to `AttributeError` or unexpected behavior.
Install
-
pip install prometheus-async
Imports
- start_http_server
from prometheus_async.aio import start_http_server
from prometheus_async.aio.web import start_http_server
- time
from prometheus_async.aio import time
- track_inprogress
from prometheus_async.aio import track_inprogress
- monitor
from prometheus_async.aio import monitor
Quickstart
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.")