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 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

sag_py_fastapi_request_id-1.0.3.tar.gz (5.7 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.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for sag_py_fastapi_request_id-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ddc996dada5ff3b662341e514becf00cb788a5aa6cdd7cb59acc16705083c0e4
MD5 a03ead28f6b0c4ef44746235465ec0e0
BLAKE2b-256 b609925d3000d247c464ed6b2e574684a5a271ddbb99120ea62cf73a84bd69b7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sag_py_fastapi_request_id-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 47211191c1f5a1c8b9c604076036a65c7f66b8f1767eaf4b7519c8f11894104d
MD5 d3864c298ba3968082ffa18cf86f36b8
BLAKE2b-256 a57da74b793d0976496bc4d9c301ff81fd6ed74682d07c9cc8fe659cac30279c

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