ASGI Prometheus Exporter
A middleware for ASGI applications that exports Prometheus metrics. This middleware is compatible with any ASGI framework (Django, FastAPI, Starlette, etc.) and ASGI Servers.
- Django (with Django ASGI)
- FastAPI
- Starlette
- Quart
- Sanic
- Any other ASGI-compatible framework
Tested with FastAPI
Features
- Request duration metrics
- Request count metrics
- Response status code metrics
- Custom metrics support (TODO)
- Compatible with any ASGI framework (UNDER EVALUATION)
- Easy to integrate
Installation
pip install asgi-prometheus-exporter
Usage
Basic Usage with FastAPI
from fastapi import FastAPI
from asgi_prometheus_exporter import PrometheusMiddleware
app = FastAPI()
app.add_middleware(PrometheusMiddleware)
@app.get("/")
async def root():
return {"message": "Hello World"}
Usage with Django
In your Django project's asgi.py:
import os
from django.core.asgi import get_asgi_application
from asgi_prometheus_exporter import PrometheusMiddleware
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
# Get the Django ASGI application
django_asgi_app = get_asgi_application()
# Wrap it with the Prometheus middleware
application = PrometheusMiddleware(
django_asgi_app,
metrics_path="/metrics", # Optional: customize metrics endpoint
custom_labels={
"app": "django_app",
"environment": "production"
}
)
Then in your settings.py:
ASGI_APPLICATION = 'myproject.asgi.application'
Usage with Starlette
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from asgi_prometheus_exporter import PrometheusMiddleware
app = Starlette()
app.add_middleware(PrometheusMiddleware)
@app.route("/")
async def homepage(request):
return JSONResponse({"message": "Hello World"})
Custom Metrics Endpoint
By default, metrics are exposed at /metrics. You can customize this by passing the metrics_path parameter:
app.add_middleware(
PrometheusMiddleware,
metrics_path="/custom/metrics/path"
)
Custom Labels
You can add custom labels to your metrics:
app.add_middleware(
PrometheusMiddleware,
custom_labels={
"app": "myapp",
"environment": "production"
}
)
Available Metrics
The middleware exposes the following metrics:
asgi_http_requests_total: Total number of HTTP requestsasgi_http_request_duration_seconds: HTTP request duration in secondsasgi_http_requests_in_progress: Number of HTTP requests in progressasgi_http_responses_total: Total number of HTTP responses by status code
Running with Different ASGI Servers
Uvicorn
uvicorn myproject.asgi:application
Daphne (Django)
daphne myproject.asgi:application
Hypercorn
hypercorn myproject.asgi:application
License
MIT License
Metadata
Release files for asgi-prometheus-exporter 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| asgi_prometheus_exporter-0.1.0.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asgi_prometheus_exporter-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.2 kB
Release files / asgi_prometheus_exporter-0.1.0.tar.gz
| Download URL | asgi_prometheus_exporter-0.1.0.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
97e3f9efad8c9be64645160593bab1934c33b2eb6d79fe9bfce5cd1ff8e6e68e
|
|
BLAKE2b-256 checksum How to use checksums |
15b091f36062a94ca9f478be0741776fc2e845654655a31e8cb26c9cb1bb4548
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.22
|
Release files / asgi_prometheus_exporter-0.1.0-py3-none-any.whl
| Download URL | asgi_prometheus_exporter-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5909e575ea6bfa3c4fceffe1bbfc84cb0af609dc2aa258aa4ac8fcabb1c6ef95
|
|
BLAKE2b-256 checksum How to use checksums |
91d8d52cbe693998ef2cab6c369302defd3763abba790833b2c9625663f3b0e5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.22
|