aioprometheus
raw JSON → 23.12.0 verified Mon Apr 27 auth: no python
A Prometheus Python client library for asyncio-based applications. Version 23.12.0. Supports creating metrics (Counter, Gauge, Histogram, Summary) and exposing them via an async HTTP server or pushing to Pushgateway. Compatible with Python 3.8+. Released regularly on PyPI.
pip install aioprometheus Common errors
error ModuleNotFoundError: No module named 'aioprometheus.metrics' ↓
cause Importing from old submodules removed in version 21.
fix
Use
from aioprometheus import Counter instead. error ValueError: The 'labels' argument must be a dict ↓
cause Passing labels as keyword arguments or non-dict.
fix
Pass labels as a dictionary:
counter.inc({"endpoint": "/"}). Warnings
breaking Version 21.x removed the old submodule import paths. Use top-level imports: `from aioprometheus import Counter`, not `from aioprometheus.metrics import Counter`. ↓
fix Update all imports to `from aioprometheus import ...`.
deprecated The `render` function signature changed in 22.x. Previously `render(metrics, accept_header)` now `render(metrics, accept=None)`. Defaults to text format. ↓
fix Pass `accept` as keyword argument if needed.
gotcha Metrics must be registered with a collector registry before they are exposed. Not doing so results in empty /metrics output. ↓
fix Always add the metric to the service's collector: `service.prometheus_metrics.add_collector(my_metric)`.
Imports
- Counter
from aioprometheus import Counter - Gauge
from aioprometheus import Gauge - Histogram
from aioprometheus import Histogram - Summary
from aioprometheus import Summary - MetricsMiddleware wrong
from aioprometheus.middleware import MetricsMiddlewarecorrectfrom aioprometheus import MetricsMiddleware - Service
from aioprometheus import Service - render wrong
from aioprometheus.renderer import rendercorrectfrom aioprometheus import render
Quickstart
from aioprometheus import Counter, Service
import asyncio
async def main():
counter = Counter("requests_total", "Total requests")
counter.inc({"endpoint": "/"})
service = Service()
service.prometheus_metrics.add_collector(counter)
await service.start(addr="0.0.0.0", port=8080)
try:
await asyncio.Event().wait()
finally:
await service.stop()
asyncio.run(main())