Skip to main content

SQL profiler middleware for FastAPI

Project description

FastAPI-Silk

Lightweight SQL profiling middleware for FastAPI + SQLAlchemy
Track query count, database time, and total request time per request.

PyPI CI Python License

Installation | Quick Start | How It Works | Development

Why FastAPI-Silk

Capability Details
SQL instrumentation setup_sql_profiler(engine) hooks into SQLAlchemy engine events (before_cursor_execute / after_cursor_execute) so SQL executed through that engine is captured per request.
Request-level metrics Adds X-DB-Queries, X-DB-Time, and X-Total-Time response headers.
Slow query visibility Logs queries slower than 0.1s to stdout for quick diagnostics.
Context isolation Uses contextvars for per-request query storage.
Minimal setup One profiler setup call + one middleware registration.

Installation

pip install fastapi-silk

PyPI: https://pypi.org/project/fastapi-silk/

Quick Start

from fastapi import FastAPI
from sqlalchemy import create_engine, text

from fastapi_silk import SQLDebugMiddleware, setup_sql_profiler

app = FastAPI()
engine = create_engine("sqlite:///./app.db")

# Profiles SQL that goes through this engine
setup_sql_profiler(engine)
app.add_middleware(SQLDebugMiddleware)


@app.get("/health")
def health() -> dict[str, bool]:
    with engine.connect() as conn:
        conn.execute(text("SELECT 1"))
    return {"ok": True}

Example response headers:

X-DB-Queries: 1
X-DB-Time: 0.0012s
X-Total-Time: 0.0049s

How It Works

flowchart LR
  A[Incoming request] --> B[SQLDebugMiddleware starts request timer]
  B --> C[Endpoint runs SQL through profiled SQLAlchemy Engine]
  C --> D[setup_sql_profiler listeners capture query start/end]
  D --> E[Query data stored in request-local context]
  E --> F[Middleware sets X-DB-Queries, X-DB-Time, X-Total-Time]

Requirements

Item Requirement
Python >=3.8 (CI runs 3.10 through 3.14)
Framework FastAPI
Database layer SQLAlchemy Engine

Code Convention / Style

  • Use Ruff for linting and formatting.
  • Use MyPy (strict mode) for type checks.
  • Keep changes small and typed where possible.

Repository Layout

fastapi-silk/
|- src/fastapi_silk/
|  |- middleware.py
|  |- profiler.py
|  `- storage.py
|- tests/
|  `- profiler/test_profiler.py
|- .github/workflows/
|  |- ci.yml
|  `- publish.yml
|- pyproject.toml
`- Makefile

Development

Install dev dependencies and run checks:

uv sync --locked --all-extras --dev
make ci
python -m pytest

make ci runs:

  • Ruff lint/format checks
  • MyPy strict type checks

Contributing

  1. Create a branch from development (for example, feature/<name> or fix/<name>).
  2. Keep the pull request focused on a single change.
  3. Add or update tests when behavior changes.
  4. Run checks locally (make ci and python -m pytest).
  5. Open a PR with a clear summary of what changed, why, and how it was tested.

Scope

FastAPI-Silk focuses on SQL profiling and request timing headers. It does not provide a built-in dashboard UI.

License

GNU General Public License v3.0 (GPL-3.0). See LICENSE.

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_silk-1.0.4.tar.gz (98.6 kB view details)

Uploaded Source

Built Distribution

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

fastapi_silk-1.0.4-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_silk-1.0.4.tar.gz.

File metadata

  • Download URL: fastapi_silk-1.0.4.tar.gz
  • Upload date:
  • Size: 98.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastapi_silk-1.0.4.tar.gz
Algorithm Hash digest
SHA256 49294077ef0427fc4d274851bb792d353f6950d82673be961d831f6b2e84277b
MD5 30a662c49b1ca393d78418951f2ca31d
BLAKE2b-256 9b82f9411eb41b27d0e0a7bbbb42fa484b599c701714e12d3ac6848251d537f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_silk-1.0.4.tar.gz:

Publisher: publish.yml on Nikolaev3Artem/fastapi-silk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_silk-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: fastapi_silk-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastapi_silk-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1e01c904927c106cc3e7b512ab0f77fda8bd289eea17519a13827a5e021a49cb
MD5 0fec445169eeee38ade83803ec8606c4
BLAKE2b-256 0dc1b9cf8216378d090204c7874fd7abf03693ecfcf59abe65ca3295d6e7cdbd

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_silk-1.0.4-py3-none-any.whl:

Publisher: publish.yml on Nikolaev3Artem/fastapi-silk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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