Skip to content

philiprehberger/py-api-timer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

philiprehberger-api-timer

Tests PyPI version License Sponsor

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

About

Drop-in ASGI/WSGI middleware for endpoint timing

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages