ASGI middleware for Prometheus metrics collection
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asgi_prometheus_exporter-0.1.0.tar.gz.
File metadata
- Download URL: asgi_prometheus_exporter-0.1.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97e3f9efad8c9be64645160593bab1934c33b2eb6d79fe9bfce5cd1ff8e6e68e
|
|
| MD5 |
337deafb3978bd6a915bdbacc733a92b
|
|
| BLAKE2b-256 |
15b091f36062a94ca9f478be0741776fc2e845654655a31e8cb26c9cb1bb4548
|
File details
Details for the file asgi_prometheus_exporter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: asgi_prometheus_exporter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5909e575ea6bfa3c4fceffe1bbfc84cb0af609dc2aa258aa4ac8fcabb1c6ef95
|
|
| MD5 |
ae4ea7af06c5c25d0907fe976fbe44ec
|
|
| BLAKE2b-256 |
91d8d52cbe693998ef2cab6c369302defd3763abba790833b2c9625663f3b0e5
|