Skip to main content

Drop-in ASGI/WSGI middleware for endpoint timing

Project description

philiprehberger-api-timer

Tests PyPI version License

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

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.10.tar.gz (3.8 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.10-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_api_timer-0.1.10.tar.gz
Algorithm Hash digest
SHA256 557e087d28accf433a567b6964d3ed7a7392c01d79b42daa0e503028209a2413
MD5 8353f1ca2bbfc5ae182ac84ec11a8eac
BLAKE2b-256 7f3b4cf95c29020a5bb62566da2b2a900da08eb0ce6bcda15a9d912567f08ec0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_api_timer-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 89e6047ec0343c9d75a84fbb993affa6968a76075c489566e66faf1781bb0e3b
MD5 752eb7c25c432fe4d0a67e382d01f6ea
BLAKE2b-256 11924c7be2c8aab8ff03d5d1ef337f689640c1d09abb98eb3f8024cae3a509ac

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