Skip to main content

Drop-in ASGI/WSGI middleware for endpoint timing

Project description

philiprehberger-api-timer

Tests PyPI version Last updated

Drop-in ASGI/WSGI middleware for endpoint timing with Server-Timing headers.

Installation

pip install philiprehberger-api-timer

Usage

ASGI (FastAPI, Starlette)

from fastapi import FastAPI
from philiprehberger_api_timer import ASGITimerMiddleware

app = FastAPI()
app.add_middleware(ASGITimerMiddleware, slow_threshold_ms=500)

WSGI (Flask, Django)

from flask import Flask
from philiprehberger_api_timer import WSGITimerMiddleware

app = Flask(__name__)
app.wsgi_app = WSGITimerMiddleware(app.wsgi_app, slow_threshold_ms=500)

Custom Logger

import logging
from philiprehberger_api_timer import ASGITimerMiddleware

logger = logging.getLogger("my_api")
app.add_middleware(ASGITimerMiddleware, logger=logger, include_header=False)

Exclude Paths

from philiprehberger_api_timer import ASGITimerMiddleware

app.add_middleware(
    ASGITimerMiddleware,
    exclude_paths=["/health", "/metrics"],  # bypass timing entirely
)

Custom Header Name

from philiprehberger_api_timer import WSGITimerMiddleware

app.wsgi_app = WSGITimerMiddleware(app.wsgi_app, header_name="X-Request-Time")

What It Does

  • Adds Server-Timing header to every response (e.g., Server-Timing: total;dur=42.5)
  • Logs a WARNING for requests exceeding the slow threshold
  • Zero configuration required — just add the middleware

API

Function / Class Description
ASGITimerMiddleware(app, logger=None, slow_threshold_ms=500, include_header=True, header_name="Server-Timing", exclude_paths=None) ASGI middleware
WSGITimerMiddleware(app, logger=None, slow_threshold_ms=500, include_header=True, header_name="Server-Timing", exclude_paths=None) WSGI middleware

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_api_timer-0.2.0.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_api_timer-0.2.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_api_timer-0.2.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_api_timer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0e5494ed9ed280a7835ce66c17f2823f67ea180114f30ff59fab2f5c8ec52b94
MD5 5889ec7155521512bbf09087ebe51831
BLAKE2b-256 1121462b5e064f235240dace829b237e37a95e7cf185430a5ddbabf1bb082e58

See more details on using hashes here.

File details

Details for the file philiprehberger_api_timer-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_api_timer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8ed52340a3cd36640482cf1c27d589ce99650391cf9f6f7bab09e49e37cd3034
MD5 714993ced6222bf49f8de4cb2abae8db
BLAKE2b-256 baef07f90c94aba56509689e0f4c27dac83f58023ddec6de07659405479ed1ef

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