Skip to main content

Middleware for tracking request processing time in Swarmauri applications

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_time


Swarmauri Middleware Time

Middleware for tracking request processing time in Swarmauri applications.

Features

  • Logs when each request starts and when it completes using Python's logging module.
  • Measures the total time spent handling a request and attaches X-Request-Duration and X-Request-Start-Time headers to the response.
  • Raises an HTTPException with status code 500 if an unexpected error occurs while the downstream handler is executing.

Installation

pip

pip install swarmauri_middleware_time

Poetry

poetry add swarmauri_middleware_time

uv

# Install uv if it is not already available
curl -LsSf https://astral.sh/uv/install.sh | sh

# Add the middleware to your project
uv add swarmauri_middleware_time

Usage

The middleware exposes a standard dispatch method that matches FastAPI's middleware interface. Instantiate it once and forward requests through dispatch inside an @app.middleware("http") hook:

from fastapi import FastAPI, Request
from starlette.testclient import TestClient

from swarmauri_middleware_time import TimerMiddleware

app = FastAPI()
timer_middleware = TimerMiddleware()


@app.middleware("http")
async def add_timer(request: Request, call_next):
    return await timer_middleware.dispatch(request, call_next)


@app.get("/ping")
async def ping():
    return {"message": "pong"}


if __name__ == "__main__":
    client = TestClient(app)
    response = client.get("/ping")

    assert response.json() == {"message": "pong"}
    assert "X-Request-Duration" in response.headers
    assert float(response.headers["X-Request-Duration"]) >= 0.0
    assert "X-Request-Start-Time" in response.headers

    print("Duration header:", response.headers["X-Request-Duration"])
    print("Start time header:", response.headers["X-Request-Start-Time"])

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

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

swarmauri_middleware_time-0.8.0.dev32.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file swarmauri_middleware_time-0.8.0.dev32.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_time-0.8.0.dev32.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_middleware_time-0.8.0.dev32.tar.gz
Algorithm Hash digest
SHA256 a92b03873dd467cdc9a88a453235dba792e438aba71af52ff43c32bf6ddf30ae
MD5 1905f804a37aac96c61e9d34bb326ce7
BLAKE2b-256 6077859b3c2f4cbfd76b529cad30a37a0a98dae252ed1750bf4345042acd3bcc

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_time-0.8.0.dev32-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_time-0.8.0.dev32-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_middleware_time-0.8.0.dev32-py3-none-any.whl
Algorithm Hash digest
SHA256 ae761dc3c880058aa572304a1b8f2822972df2c5d3c15a7c3f17ff807450f420
MD5 dc3a27b4bbabc428b99ac9e97a13a415
BLAKE2b-256 2f5b6b122ed91703a4c40506672afead465f408ad8876b3c52d235e65a65e42e

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