sag-py-fastapi-request-id


Namesag-py-fastapi-request-id JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/SamhammerAG/sag_py_fastapi_request_id
SummaryAdds an unique identifiert to fastapi requests
upload_time2024-09-18 11:38:24
maintainerNone
docs_urlNone
authorSamhammer AG
requires_python>=3.12
licenseMIT
keywords auth fastapi keycloak
VCS
bugtrack_url
requirements contextvars starlette
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # sag_py_fastapi_request_id

[![Maintainability][codeclimate-image]][codeclimate-url]
[![Coverage Status][coveralls-image]][coveralls-url]
[![Known Vulnerabilities](https://snyk.io/test/github/SamhammerAG/sag_py_fastapi_request_id/badge.svg)](https://snyk.io/test/github/SamhammerAG/sag_py_fastapi_request_id)

[coveralls-image]:https://coveralls.io/repos/github/SamhammerAG/sag_py_fastapi_request_id/badge.svg?branch=master
[coveralls-url]:https://coveralls.io/github/SamhammerAG/sag_py_fastapi_request_id?branch=master
[codeclimate-image]:https://api.codeclimate.com/v1/badges/1d0606922774a8ac4a7d/maintainability
[codeclimate-url]:https://codeclimate.com/github/SamhammerAG/sag_py_fastapi_request_id/maintainability

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:
```python
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
```python
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.

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

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/SamhammerAG/sag_py_fastapi_request_id",
    "name": "sag-py-fastapi-request-id",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.12",
    "maintainer_email": null,
    "keywords": "auth, fastapi, keycloak",
    "author": "Samhammer AG",
    "author_email": "support@samhammer.de",
    "download_url": "https://files.pythonhosted.org/packages/e1/73/9234a7aecae1449bc7ac79d1b94444219b0d0a9c02f3b84760ed5f3a0990/sag_py_fastapi_request_id-1.0.0.tar.gz",
    "platform": null,
    "description": "# sag_py_fastapi_request_id\n\n[![Maintainability][codeclimate-image]][codeclimate-url]\n[![Coverage Status][coveralls-image]][coveralls-url]\n[![Known Vulnerabilities](https://snyk.io/test/github/SamhammerAG/sag_py_fastapi_request_id/badge.svg)](https://snyk.io/test/github/SamhammerAG/sag_py_fastapi_request_id)\n\n[coveralls-image]:https://coveralls.io/repos/github/SamhammerAG/sag_py_fastapi_request_id/badge.svg?branch=master\n[coveralls-url]:https://coveralls.io/github/SamhammerAG/sag_py_fastapi_request_id?branch=master\n[codeclimate-image]:https://api.codeclimate.com/v1/badges/1d0606922774a8ac4a7d/maintainability\n[codeclimate-url]:https://codeclimate.com/github/SamhammerAG/sag_py_fastapi_request_id/maintainability\n\nThis library provides a way to identify all log entries that belong to a single request.\n\n## What it does\n* Provides a middleware to generate a random request id for every request\n* Contains a logging filter that adds the request id as field to every log entry\n\n## How to use\n\n### Installation\n\npip install sag-py-fastapi-request-id\n\n### Add the middleware\n\nAdd this middleware so that the request ids are generated:\n```python\nfrom sag_py_fastapi_request_id.request_context_middleware import RequestContextMiddleware\nfrom fastapi import FastAPI\n\napp = FastAPI(...)\napp.add_middleware(RequestContextMiddleware)\n```\n\n### Get the request id\n\nThe request id can be accessed over the context\n```python\nfrom sag_py_fastapi_request_id.request_context import get_request_id as get_request_id_from_context\nrequest_id = get_request_id_from_context()\n```\n\nThis works in async calls but not in sub threads (without additional changes).\n\nSee:\n* https://docs.python.org/3/library/contextvars.html\n* https://kobybass.medium.com/python-contextvars-and-multithreading-faa33dbe953d\n\n### Add request id field to logging\n\nIt is possible to log the request id by adding a filter.\n\n```python\nimport logging\nfrom sag_py_fastapi_request_id.request_context_logging_filter import RequestContextLoggingFilter\n\nconsole_handler = logging.StreamHandler(sys.stdout)\nconsole_handler.addFilter(RequestContextLoggingFilter())\n\n```\n\nThe filter adds the field request_id if it has a value in the context.\n\n## How to start developing\n\n### With vscode\n\nJust install vscode with dev containers extension. All required extensions and configurations are prepared automatically.\n\n### With pycharm\n\n* Install latest pycharm\n* Install pycharm plugin BlackConnect\n* Install pycharm plugin Mypy\n* Configure the python interpreter/venv\n* pip install requirements-dev.txt\n* pip install black[d]\n* Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed files\n* Ctl+Alt+S => Check Tools => BlackConnect => Trigger on code reformat\n* Ctl+Alt+S => Click Tools => BlackConnect => \"Load from pyproject.yaml\" (ensure line length is 120)\n* Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the \"local instance\" config (e.g. C:\\Python310\\Scripts\\blackd.exe)\n* Ctl+Alt+S => Click Tools => Actions on save => Reformat code\n* Restart pycharm\n\n## How to publish\n* Update the version in setup.py and commit your change\n* Create a tag with the same version number\n* Let github do the rest\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Adds an unique identifiert to fastapi requests",
    "version": "1.0.0",
    "project_urls": {
        "Bug Reports": "https://github.com/SamhammerAG/sag_py_fastapi_request_id/issues",
        "Documentation": "https://github.com/SamhammerAG/sag_py_fastapi_request_id",
        "Homepage": "https://github.com/SamhammerAG/sag_py_fastapi_request_id",
        "Source": "https://github.com/SamhammerAG/sag_py_fastapi_request_id"
    },
    "split_keywords": [
        "auth",
        " fastapi",
        " keycloak"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "de86662dad15abe93ae658076b6fa38262bd1799ded4ef56cd5868c2fa7b4010",
                "md5": "be807904bd614fa6c91c8cab95f13e06",
                "sha256": "e7c3f1e552aa23f4acc015b998873907d732d93bb4b7bc8ac8206e1be083d815"
            },
            "downloads": -1,
            "filename": "sag_py_fastapi_request_id-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "be807904bd614fa6c91c8cab95f13e06",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.12",
            "size": 5775,
            "upload_time": "2024-09-18T11:38:23",
            "upload_time_iso_8601": "2024-09-18T11:38:23.747172Z",
            "url": "https://files.pythonhosted.org/packages/de/86/662dad15abe93ae658076b6fa38262bd1799ded4ef56cd5868c2fa7b4010/sag_py_fastapi_request_id-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e1739234a7aecae1449bc7ac79d1b94444219b0d0a9c02f3b84760ed5f3a0990",
                "md5": "ada2c5754345e41e45c7cd9193d71313",
                "sha256": "ba9f59bbb01c3b72f11e9bbd9f33997d4cd1361a42d870ad0d9d0aab4b63886d"
            },
            "downloads": -1,
            "filename": "sag_py_fastapi_request_id-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "ada2c5754345e41e45c7cd9193d71313",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.12",
            "size": 5496,
            "upload_time": "2024-09-18T11:38:24",
            "upload_time_iso_8601": "2024-09-18T11:38:24.881400Z",
            "url": "https://files.pythonhosted.org/packages/e1/73/9234a7aecae1449bc7ac79d1b94444219b0d0a9c02f3b84760ed5f3a0990/sag_py_fastapi_request_id-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-18 11:38:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "SamhammerAG",
    "github_project": "sag_py_fastapi_request_id",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "contextvars",
            "specs": [
                [
                    ">=",
                    "2.4"
                ]
            ]
        },
        {
            "name": "starlette",
            "specs": [
                [
                    ">=",
                    "0.37.2"
                ]
            ]
        }
    ],
    "lcname": "sag-py-fastapi-request-id"
}
        
Elapsed time: 0.75330s