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)

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) ASGI middleware
WSGITimerMiddleware(app, logger=None, slow_threshold_ms=500, include_header=True) 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.1.12.tar.gz (5.9 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.1.12-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_api_timer-0.1.12.tar.gz
Algorithm Hash digest
SHA256 c776036c6bac0e68289863b0cc4ef9afd18ef9eec2ca4fffdbc4f394f4846d59
MD5 fad67dfd103bb884f6aff7b0b8bfa982
BLAKE2b-256 fca94c6515e8db21ff944426bc39a83f21bcabb27ea6e41493310fb604f8ef3b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_api_timer-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 d62b317a76995345cfb65c83e9add1a6f24d1d777a65c3e25037704f5cbea51b
MD5 5030d4394ed7eb5f291588fbdd17ce34
BLAKE2b-256 dfec8218c61edad3cecc21baecefa6694084ff1f2eb46d456ccbacfc336482dc

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