fastapi-profiler is a FastAPI Middleware that integrates pyinstrument to provide request profiling and performance analysis for FastAPI applications. It helps developers identify bottlenecks by generating detailed reports (HTML, speedscope, JSON, .prof files). As of version 1.5.0, it supports sampling, error auto-capture, and structured JSON logging. It generally follows a regular release cadence, with several minor releases a year.
pip install fastapi-profilerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate `FastAPIProfilerMiddleware` into a FastAPI application, configure basic profiling options, and simulate a slow endpoint. It highlights saving profiles to disk, printing summaries to console, and using new features like sampling and error auto-capture. Run the app with Uvicorn and access endpoints to generate profiles.
In production, always set `profiler_sample_rate` to a low value (e.g., 0.01-0.1) or use environment variables like `PYINSTRUMENT_PROFILING_ENABLED=false` for a no-op. Consider `always_profile_errors=True` to catch performance issues only on failing requests. For long-term monitoring, integrate with structured logging.
Ensure `Jinja2` is installed explicitly if you intend to use `is_browser_enable=True` by running `pip install Jinja2` or `pip install fastapi-profiler[browser]`.
When upgrading to v1.5.0 or later, review your logging configuration. If you prefer the old text format, explicitly set `log_format='text'`. If migrating to JSON, update your log aggregation tools to parse the new structured output.