Middleware for FastAPI
Project description
FastAPI middleware
Overview
fastapi-middleware provides a collection of middleware for enhancing FastAPI applications.
Installation
Install fastapi-middleware directly from PyPI:
pip install fastapi-middleware
Getting Started
To start using the middleware, import the components you need and add them to your FastAPI app.
Example usage
import logging
from fastapi import FastAPI
from fastapi_middleware import SQLQueriesMiddleware
app = FastAPI()
# Set logging level for middleware
logging.getLogger('fastapi-middleware').setLevel(logging.DEBUG)
# Add the middleware components
app.add_middleware(SQLQueriesMiddleware)
Middleware Components
GlobalContextMiddleware
The GlobalContextMiddleware provides a global context object (global_ctx) that allows you to store data throughout the lifetime of a request. This can be useful for sharing request-specific information across different parts of your application without needing to pass it explicitly.
Example Use Case
Add data to the global_ctx in one part of your application, and access it in other middleware or endpoint functions:
from fastapi import FastAPI
from fastapi_middleware import GlobalContextMiddleware, global_ctx
app = FastAPI()
# Add the middleware components
app.add_middleware(GlobalContextMiddleware)
...
global_ctx.user_id = 12345 # Set user-specific data
global_ctx.foo = 'bar' # Or set request-specific data
# Retrieve in other parts of the app: global_ctx.user_id, global_ctx.foo
SQLQueriesMiddleware
The SQLQueriesMiddleware logs a concise summary of SQL query performance in a single INFO message for each request, making it easy to monitor query performance. Additionally, detailed query insights, such as the actual SQL statements of the fastest and slowest queries, as well as individual query statements, are available at the DEBUG level if configured.
Key metrics include:
- Total SQL Queries: Number of SQL queries executed.
- Total Query Time: Combined execution time for all queries.
- Average Query Time: Average time taken per query.
- Fastest and Slowest Query Times: Execution times of the fastest and slowest queries, along with statements at DEBUG level.
Configuration
You can customize SQLQueriesMiddleware with the following option:
print_each_query(bool): IfTrue, logs each individual SQL query's time and statement at theDEBUGlevel. This setting is useful for in-depth debugging. Default isFalse.
Example Usage
# Add SQLQueriesMiddleware with configuration
app.add_middleware(SQLQueriesMiddleware, print_each_query=True)
Example Log Output
INFO: [SQLQueriesMiddleware] Total Queries: 4, Total Time: 24.04ms, Avg Time: 6.01ms, Fastest: 0.46ms, Slowest: 13.30ms
DEBUG: [SQLQueriesMiddleware] Fastest Query: Fastest Query: SELECT * FROM users WHERE id = 1;
DEBUG: [SQLQueriesMiddleware] Slowest Query: SELECT * FROM orders WHERE status = 'pending';
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_middleware-0.2.1.tar.gz.
File metadata
- Download URL: fastapi_middleware-0.2.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4adfa55f3fb4a063e5854160d60d6aea2b465bb8f3723cefbaf0acb074a36d81
|
|
| MD5 |
285d5f2d0798668d9308074bc47604bc
|
|
| BLAKE2b-256 |
4f6065ef4ff66187762a2bb251b54e2b11ade270dd1828f6d43638aae2a07c66
|
File details
Details for the file fastapi_middleware-0.2.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_middleware-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
070aadcbf2716545db618c90750ddae9975b5ff762fef54ba29cea8b9d8f0faa
|
|
| MD5 |
e5294d9fa06e01de2e9b3ff2cd6392b3
|
|
| BLAKE2b-256 |
b33a7aa49fb407bd9edbeee91b0a4cc6cb9ff0113142853cc0ab484296ce000a
|