Skip to main content

Adds an unique identifiert to fastapi requests

Project description

sag_py_fastapi_request_id

Maintainability Coverage Status Known Vulnerabilities

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:

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

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

sag_py_fastapi_request_id-1.0.4.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sag_py_fastapi_request_id-1.0.4-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file sag_py_fastapi_request_id-1.0.4.tar.gz.

File metadata

File hashes

Hashes for sag_py_fastapi_request_id-1.0.4.tar.gz
Algorithm Hash digest
SHA256 4865a64be88ea5bfa2e2918f7406c8efb330d379ee3cd6d2666991bb490a5bdc
MD5 4300d1e5a6211e9b130d183090f87d99
BLAKE2b-256 358bc08de22b25f41007ed739186bbf308cdbba85afe84c03f00140c019e6287

See more details on using hashes here.

File details

Details for the file sag_py_fastapi_request_id-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for sag_py_fastapi_request_id-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ef32ec1c1bd8e8d9ec2ca79433fc4390dbf1f249276387d3fc6f6e4467d55888
MD5 584491319723a8cfa36963c2478d13fc
BLAKE2b-256 568b6490b49d47dfc115dad23a7da9a71f999c92234f941c80bba2c74da0dbbd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page