FastAPI middleware for request tracing and logging
Project description
FastAPI Trace Middleware
A simple and effective middleware for FastAPI applications that automatically adds trace IDs to requests and integrates with Python logging.
Features
- 🔍 Automatic trace ID generation for each request
- 📝 Seamless integration with Python logging
- 🎛️ Configurable trace ID headers and generators
- 🔄 Context-aware trace ID access throughout your application
- 📦 Easy setup with sensible defaults
Installation
pip install fastapi-trace-middleware
Quick Start
from fastapi import FastAPI
from fastapi_trace import create_trace_middleware, setup_simple_trace_logging
import logging.config
# Setup logging with trace support
logging.config.dictConfig(setup_simple_trace_logging())
app = FastAPI()
# Add trace middleware
trace_middleware = create_trace_middleware()
app.middleware("http")(trace_middleware)
@app.get("/")
async def root():
from fastapi_trace import get_trace_id
logging.info("Processing request") # Will include trace_id automatically
return {"message": "Hello World", "trace_id": get_trace_id()}
Documentation
Basic Usage
The middleware automatically:
- Extracts trace ID from request headers (default:
X-Trace-ID) - Generates a new trace ID if none exists
- Sets the trace ID in context for the entire request lifecycle
- Adds trace ID to response headers
- Makes trace ID available in logs
Configuration
from fastapi_trace import create_trace_middleware
# Custom configuration
trace_middleware = create_trace_middleware(
trace_header="X-Custom-Trace-ID", # Custom header name
include_response_header=True, # Include in response
trace_id_generator=lambda: "custom-" + generate(size=10) # Custom generator
)
Accessing Trace ID
from fastapi_trace import get_trace_id
async def my_function():
trace_id = get_trace_id()
print(f"Current trace ID: {trace_id}")
Custom Logging Setup
from fastapi_trace import setup_trace_logging
# Custom formatters and handlers
config = setup_trace_logging(
formatters={
"custom": {
"format": "%(asctime)s [%(trace_id)s] %(levelname)s: %(message)s"
}
}
)
import logging.config
logging.config.dictConfig(config)
Requirements
- Python 3.8+
- FastAPI 0.68.0+
- nanoid 2.0.0+
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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
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_trace_middleware-1.0.1.tar.gz.
File metadata
- Download URL: fastapi_trace_middleware-1.0.1.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a76c630e9cbcab409580ea3d8401c49626d2b608cf1eae3f3d1caf362e827dd8
|
|
| MD5 |
0515bbf54b7c4d20ba52da55f2953a68
|
|
| BLAKE2b-256 |
ef7f2e05d72629181f97cece29876c9965cfc31cd92de6a3c8e963f57e85f748
|
File details
Details for the file fastapi_trace_middleware-1.0.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_trace_middleware-1.0.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
782ef4502a92da6f9d35c29ce9d479494bfb912d9a5521a9b34d3936b81fa8cc
|
|
| MD5 |
e323da706d570f364cb69f95394eab7c
|
|
| BLAKE2b-256 |
c4fdc03894ace52779fd454d9bc6e6f499df973cda63f961216e2e7a671da931
|