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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_api_timer-0.1.11.tar.gz
Algorithm Hash digest
SHA256 95fed8495acff61e7e4c54da77bc532ba0fa1b3616002e83d05399f3294c143e
MD5 8fddee1ba91fc8f9f7ebd01e13680c11
BLAKE2b-256 6d3021f3085cc3dc7c20a6e1ba18f52c25e809a399c02c565576e7bba429be8f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_api_timer-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 8b7508cdb5d9e6abf31b69b3d3d21d088a7679b84284f27319bba33f72431fa2
MD5 a43d2f2023c07b1ff9b8187ae00803a5
BLAKE2b-256 312ed760c7ba612918e39c1b35381b13e1df9adcac533e8fd5c3678d77b1f42b

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