Skip to main content

A helper package for collecting default Prometheus metrics in ASGI applications.

Project description

default_metrics_fastapi test PyPI version

A helper package for collecting default Prometheus metrics in ASGI applications (such as FastAPI, Starlette, etc.).

Features

  • Automatic request/response metrics: Tracks request counts, response codes, exceptions, durations, and in-progress requests.
  • Prometheus-compatible: Exposes metrics in Prometheus text format.
  • Configurable exception logging: Optionally logs unhandled exceptions at the middleware level.
  • ASGI compatible: Works with FastAPI, Starlette, and other ASGI frameworks.

Usage

from fastapi import FastAPI
from default_metrics_asgi import PrometheusMiddleware, metrics

app = FastAPI()
app.add_middleware(PrometheusMiddleware)  # log_exceptions=True by default
app.add_route("/metrics", metrics)

@app.get("/")
async def root():
    return {"message": "Hello, world!"}

Optional: Disable Middleware Exception Logging

app.add_middleware(PrometheusMiddleware, log_exceptions=False)

Metrics Collected

  • api_app_info: Application info (gauge)
  • api_requests_total: Total number of requests received by method and path (counter)
  • api_responses_total: Total number of responses sent by method, path, and status code (counter)
  • api_requests_duration_seconds: Histogram of request processing time by method and path (histogram)
  • api_exceptions_total: Total number of exceptions raised by method, path, and exception type (counter)
  • api_requests_in_progress: Number of requests currently being processed by method and path (gauge)

Exporting Metrics

Visit /metrics endpoint to see the Prometheus metrics.

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

default_metrics_asgi-0.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

default_metrics_asgi-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: default_metrics_asgi-0.1.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1018-azure

File hashes

Hashes for default_metrics_asgi-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6ec7b7612e14febc4cea3344bfc6f413450f0e4984ec56590189d5438b102b13
MD5 59d1f5bffe71912425aeed634747664e
BLAKE2b-256 25bf1f21d0c18a7ccc16c0fce178a944eb091d875916d49d376f3b011b6c2a1a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: default_metrics_asgi-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.11 Linux/6.11.0-1018-azure

File hashes

Hashes for default_metrics_asgi-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 71e2eb68853073b257164ce39ed341bdec0ae29641fc129ad3d78e546c72dcbf
MD5 01d3369e2299a43e8eb8bfc059107568
BLAKE2b-256 720646e4d405223bd52163fa41d2bbd1959b08afced882d217b2d3b8b005a992

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