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 Mypy
- Configure the python interpreter/venv
- pip install requirements-dev.txt
- 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
Metadata
Release files for sag-py-fastapi-request-id 1.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sag_py_fastapi_request_id-1.0.5.tar.gz | 5.5 kB | Without provenance | Details for sag_py_fastapi_request_id-1.0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sag_py_fastapi_request_id-1.0.5-py3-none-any.whl | Python 3 | none | any | Details for sag_py_fastapi_request_id-1.0.5-py3-none-any.whl |
Total release size: 11.3 kB
Release files / sag_py_fastapi_request_id-1.0.5.tar.gz
| Download URL | sag_py_fastapi_request_id-1.0.5.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
771b13e577202f0c8ac16115e32d40bd65d37f7e226a0094074deac283b7563c
|
|
BLAKE2b-256 checksum How to use checksums |
4ca35f1cdf4744e0f124d6926317c39aad80518d3ac09ce4fcf2b03f0149c6bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / sag_py_fastapi_request_id-1.0.5-py3-none-any.whl
| Download URL | sag_py_fastapi_request_id-1.0.5-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a7bb25e68f696e986f6cda7b1969decad99f629347f899c1f33103950be2d5dd
|
|
BLAKE2b-256 checksum How to use checksums |
e6b5e902860e6c2f86b054531440d6f2dd44317528c9091896583dda8b519854
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for sag-py-fastapi-request-id 1.0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.