Adds an unique identifiert to fastapi requests
Project description
sag_py_fastapi_request_id
This library provides a way to identify all log entries that belong to a single request.
What it does
- Provides a middleware to generate a random request id for every request
- Contains a logging filter that adds the request id as field to every log entry
How to use
Installation
pip install sag-py-fastapi-request-id
Add the middleware
Add this middleware so that the request ids are generated:
from sag_py_fastapi_request_id.request_context_middleware import RequestContextMiddleware
from fastapi import FastAPI
app = FastAPI(...)
app.add_middleware(RequestContextMiddleware)
Get the request id
The request id can be accessed over the context
from sag_py_fastapi_request_id.request_context import get_request_id as get_request_id_from_context
request_id = get_request_id_from_context()
This works in async calls but not in sub threads (without additional changes).
See:
- https://docs.python.org/3/library/contextvars.html
- https://kobybass.medium.com/python-contextvars-and-multithreading-faa33dbe953d
Add request id field to logging
It is possible to log the request id by adding a filter.
import logging
from sag_py_fastapi_request_id.request_context_logging_filter import RequestContextLoggingFilter
console_handler = logging.StreamHandler(sys.stdout)
console_handler.addFilter(RequestContextLoggingFilter())
The filter adds the field request_id if it has a value in the context.
How to start developing
With vscode
Just install vscode with dev containers extension. All required extensions and configurations are prepared automatically.
With pycharm
- Install latest pycharm
- Install pycharm plugin BlackConnect
- Install pycharm plugin Mypy
- Configure the python interpreter/venv
- pip install requirements-dev.txt
- pip install black[d]
- Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed files
- Ctl+Alt+S => Check Tools => BlackConnect => Trigger on code reformat
- Ctl+Alt+S => Click Tools => BlackConnect => "Load from pyproject.yaml" (ensure line length is 120)
- Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the "local instance" config (e.g. C:\Python310\Scripts\blackd.exe)
- Ctl+Alt+S => Click Tools => Actions on save => Reformat code
- Restart pycharm
How to publish
- Update the version in setup.py and commit your change
- Create a tag with the same version number
- Let github do the rest
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
File details
Details for the file sag_py_fastapi_request_id-1.0.0.tar.gz
.
File metadata
- Download URL: sag_py_fastapi_request_id-1.0.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba9f59bbb01c3b72f11e9bbd9f33997d4cd1361a42d870ad0d9d0aab4b63886d |
|
MD5 | ada2c5754345e41e45c7cd9193d71313 |
|
BLAKE2b-256 | e1739234a7aecae1449bc7ac79d1b94444219b0d0a9c02f3b84760ed5f3a0990 |
File details
Details for the file sag_py_fastapi_request_id-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: sag_py_fastapi_request_id-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7c3f1e552aa23f4acc015b998873907d732d93bb4b7bc8ac8206e1be083d815 |
|
MD5 | be807904bd614fa6c91c8cab95f13e06 |
|
BLAKE2b-256 | de86662dad15abe93ae658076b6fa38262bd1799ded4ef56cd5868c2fa7b4010 |