Skip to main content

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 requests
  • asgi_http_request_duration_seconds: HTTP request duration in seconds
  • asgi_http_requests_in_progress: Number of HTTP requests in progress
  • asgi_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asgi_prometheus_exporter-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asgi_prometheus_exporter-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file asgi_prometheus_exporter-0.1.0.tar.gz.

File metadata

File hashes

Hashes for asgi_prometheus_exporter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 97e3f9efad8c9be64645160593bab1934c33b2eb6d79fe9bfce5cd1ff8e6e68e
MD5 337deafb3978bd6a915bdbacc733a92b
BLAKE2b-256 15b091f36062a94ca9f478be0741776fc2e845654655a31e8cb26c9cb1bb4548

See more details on using hashes here.

File details

Details for the file asgi_prometheus_exporter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for asgi_prometheus_exporter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5909e575ea6bfa3c4fceffe1bbfc84cb0af609dc2aa258aa4ac8fcabb1c6ef95
MD5 ae4ea7af06c5c25d0907fe976fbe44ec
BLAKE2b-256 91d8d52cbe693998ef2cab6c369302defd3763abba790833b2c9625663f3b0e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page