Skip to main content

philiprehberger-api-timer

Tests PyPI version Last updated

philiprehberger-api-timer

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")

Export to Prometheus / statsd via metric_callback

from philiprehberger_api_timer import ASGITimerMiddleware

def record(method: str, path: str, status: int, elapsed_ms: float) -> None:
    histogram.labels(method=method, path=path, status=status).observe(elapsed_ms)

app.add_middleware(ASGITimerMiddleware, metric_callback=record)

The callback fires once per non-excluded request after the response has been sent. Exceptions raised inside the callback are caught and logged so they cannot break the response.

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, metric_callback=None) ASGI middleware
WSGITimerMiddleware(app, logger=None, slow_threshold_ms=500, include_header=True, header_name="Server-Timing", exclude_paths=None, metric_callback=None) WSGI middleware
MetricCallback Type alias for (method, path, status, elapsed_ms) -> None

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

Release files for philiprehberger-api-timer 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for philiprehberger-api-timer 0.3.0
File Size Uploaded
philiprehberger_api_timer-0.3.0.tar.gz 191.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-api-timer 0.3.0
File Interpreter ABI Platform
philiprehberger_api_timer-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 197.3 kB

Release files / philiprehberger_api_timer-0.3.0.tar.gz

Download URL philiprehberger_api_timer-0.3.0.tar.gz
Size 191.7 kB
Tags Source
SHA-256 checksum
How to use checksums
552b87edc23af9254e7e950445441f5c4a608e99d06e9b2cf44e3e10da09a7fd
BLAKE2b-256 checksum
How to use checksums
055cb400ad8fddb6f5ca16fb8cbacc85f3502ac8fc48e4fe18dee6c6c462c00c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_api_timer-0.3.0-py3-none-any.whl

Download URL philiprehberger_api_timer-0.3.0-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e000e7bedf4c8adba275d7e4d77f56a190504b8416d456dbb51cb1a26cd7a01
BLAKE2b-256 checksum
How to use checksums
d0ef07b72c6cdcf6813618108d8eb13656d9b5871e039e180dd8bd58549d3037
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page