Skip to main content

Request waterfall tracing for Starlette-compatible ASGI applications

Project description

waterfall-log

waterfall-log is a small Python library for Starlette-compatible applications that prints a request waterfall to the console after every HTTP request.

It is designed for FastAPI and other ASGI apps built on Starlette, and it focuses on two things:

  • capturing the Python call tree for one request
  • making the slowest parts obvious in the console output

What it does

For each HTTP request, the middleware:

  • profiles Python function calls in the active request task
  • builds a nested call tree with timestamps
  • prints a waterfall-style timeline to the configured output stream
  • reports the hottest frames by inclusive and self time

Install

poetry install --with dev,demo

Install the library into another project from a built artifact or directly from PyPI later with:

pip install waterfall-log

Quick start

from fastapi import FastAPI

from waterfall_log import WaterfallMiddleware

app = FastAPI()
app.add_middleware(WaterfallMiddleware)


@app.get("/hello")
async def hello() -> dict[str, str]:
    return {"message": "hello"}

Run the sample app:

poetry run uvicorn sample_app:app --reload

Then call:

curl http://127.0.0.1:8000/report/42

Example output:

Request 200 GET /report/42 took 86.54 ms
Hotspots
  38.12 ms total | 36.89 ms self  sample_app.py:24 load_line_items
  21.07 ms total | 20.81 ms self  sample_app.py:36 render_summary
Waterfall
    0.00 ms |############################################################|   86.54 ms 100.0% GET /report/42
    1.14 ms | ###                                                        |    4.93 ms   5.7% sample_app.py:51 compute_discount
    7.03 ms |     ##########################                             |   38.12 ms  44.0% sample_app.py:24 load_line_items  <<< hottest
   49.82 ms |                                  ###############          |   21.07 ms  24.3% sample_app.py:36 render_summary

Notes

  • The profiler automatically isolates the active asyncio task, so overlapping requests handled on the same event loop do not share one trace.
  • Work executed in background threads or native extensions is not profiled directly. Time spent there is still visible in the waiting parent frame.
  • The middleware only traces HTTP requests. WebSocket and lifespan scopes pass through unchanged.

Poetry workflow

Install dependencies for local work:

poetry install --with dev,demo

Run tests:

poetry run pytest

Build publishable artifacts:

poetry build

Check package metadata:

poetry check

Publish to PyPI:

poetry config pypi-token.pypi <token>
poetry publish --build

If you want to publish to TestPyPI first:

poetry config repositories.testpypi https://test.pypi.org/legacy/
poetry publish --build --repository testpypi

Files

  • src/waterfall_log: library package
  • sample_app.py: runnable FastAPI demo
  • tests/test_middleware.py: smoke test for middleware output

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

waterfall_log-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

waterfall_log-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file waterfall_log-0.1.0.tar.gz.

File metadata

  • Download URL: waterfall_log-0.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for waterfall_log-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fca51d0945938918ee05b284901df555159eb949433fd93788b183c78b8889ad
MD5 833117dae132e0f778c2684d2f1767c9
BLAKE2b-256 c4989e40cbe555889bd0dd30c95e882f94c0699648831538cf431993a9adb4f8

See more details on using hashes here.

File details

Details for the file waterfall_log-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: waterfall_log-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for waterfall_log-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c0889452ae297324c05d868af080401d33d11931c0e496aa9b2e529237eeb41
MD5 eff43d9086ca11a109279a3dae7e475f
BLAKE2b-256 30a639b27216ddd3596747b6308aaf40e2f6844a36f61ba1c49075cf230be882

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