Skip to main content

Plug-and-play request/response flow tracer for FastAPI — shows async/sync execution context, blocking call detection, and full call trees.

Project description

🔍 FastAPI FlowTracer

Plug-and-play request/response flow tracer for FastAPI — automatically shows async/sync execution context, blocking call detection, and full call trees for every request.

✨ Features

  • Zero config — just FlowTracer(app) and you're done
  • Full call trees — traces every function your request touches
  • Execution context — shows [event_loop] vs [threadpool:worker-N]
  • Blocking detection — flags sync functions running on the event loop with ⚠️ BLOCKING
  • Await tracking — marks async I/O calls with ⏳ await
  • Smart filtering — traces your code + I/O libraries, skips framework internals
  • Colorized output — green, yellow, red, cyan color-coding in terminal

📦 Installation

pip install fastapi-flowtracer

Or with uv:

uv add fastapi-flowtracer

🚀 Quick Start

from fastapi import FastAPI
from fastapi_flowtracer import FlowTracer

app = FastAPI()
FlowTracer(app)  # That's it!

@app.get("/users/{user_id}")
async def get_user(user_id: int):
    user = await fetch_user_from_db(user_id)
    total = calculate_total(user.orders)  # sync on event loop!
    report = await asyncio.to_thread(generate_report, user)
    return {"user": user, "total": total}

📊 Output

Every request prints a trace tree to stderr:

[REQ-a1b2c3d4] → GET /users/42
  ├─ [event_loop] get_user (routes/users.py:15) ⏱ 120ms
  │  ├─ [event_loop] fetch_user_from_db (db/queries.py:42) ⏱ 45ms  ⏳ await
  │  ├─ [event_loop] ⚠️ BLOCKING calculate_total (utils/pricing.py:8) ⏱ 15ms
  │  │  └─ [event_loop] ⚠️ BLOCKING apply_tax (utils/pricing.py:22) ⏱ 3ms
  │  └─ [threadpool:worker-2] generate_report (services/pdf.py:31) ⏱ 55ms
  │     └─ [threadpool:worker-2] format_report_text (services/pdf.py:58) ⏱ 40ms
  └─ Response: 200 OK ⏱ 122ms total

Reading the output

Marker Meaning
[event_loop] Function running on the asyncio event loop thread
[threadpool:worker-N] Function running in a threadpool worker
⚠️ BLOCKING Sync function on the event loop — blocks all other requests!
⏳ await Async I/O call (non-blocking)
⏱ Nms Function execution time

🔧 How It Works

  1. FlowTracer(app) installs an ASGI middleware on your FastAPI app
  2. For each request, it activates Python's sys.setprofile to capture every function call
  3. It auto-detects your project root and only traces your code + known I/O libraries
  4. Framework internals (Starlette, Pydantic, uvicorn, etc.) are filtered out
  5. After the request completes, it prints the colorized call tree

🧪 Development

# Clone and install
git clone https://github.com/himanshuyadav/fastapi-flowtracer.git
cd fastapi-flowtracer
uv sync

# Run tests
uv run pytest tests/ -v

# Run example app
uv run uvicorn examples.basic.main:app --reload

📄 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

fastapi_flowtracer-0.1.0.tar.gz (43.3 kB view details)

Uploaded Source

Built Distribution

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

fastapi_flowtracer-0.1.0-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_flowtracer-0.1.0.tar.gz
  • Upload date:
  • Size: 43.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_flowtracer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 32acee4fc689f0f49712fbff2da4ab088f29853168c04bc37456ce89604664ae
MD5 1ca0709bbea0cc414fb55f108ed83f75
BLAKE2b-256 c2ca339c9fe84a91db6bc6cf85bba0e6f4e0ae23ca15a34a2eb1f79dd13f1aa3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_flowtracer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_flowtracer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 21e2f9cceaaf99e31512f5f2d1d90d7d93a7f4d5062a8555672459493cf3a86a
MD5 c7b571d4a9216d7556a58935cfa39b07
BLAKE2b-256 a7c101bf51a16deb734641f6a7bbda5764715803987c613962071a2ddc348db8

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