sag-py-fastapi-request-id


Namesag-py-fastapi-request-id JSON
Version 0.1.2 PyPI version JSON
download
home_pagehttps://github.com/SamhammerAG/sag_py_fastapi_request_id
SummaryAdds an unique identifiert to fastapi requests
upload_time2024-04-30 16:42:27
maintainerNone
docs_urlNone
authorSamhammer AG
requires_python>=3.8
licenseMIT
keywords auth fastapi keycloak
VCS
bugtrack_url
requirements No requirements were recorded.
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.8",
    "maintainer_email": null,
    "keywords": "auth, fastapi, keycloak",
    "author": "Samhammer AG",
    "author_email": "support@samhammer.de",
    "download_url": "https://files.pythonhosted.org/packages/66/7e/90615a11f80f7ae653582ec6e5db60bdcd265fbd88a3a712a61946cc6576/sag_py_fastapi_request_id-0.1.2.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": "0.1.2",
    "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": "588da9044855e3ec5cf65622b94b761f2ed2e87574d46e6389c00f66d5eb65b4",
                "md5": "41f3c89115f0c47064721dc4003f1180",
                "sha256": "d09bf025518c8f1607a5dedc164520923ccf44b69af67124c2648082902cc499"
            },
            "downloads": -1,
            "filename": "sag_py_fastapi_request_id-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "41f3c89115f0c47064721dc4003f1180",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 5792,
            "upload_time": "2024-04-30T16:42:25",
            "upload_time_iso_8601": "2024-04-30T16:42:25.534813Z",
            "url": "https://files.pythonhosted.org/packages/58/8d/a9044855e3ec5cf65622b94b761f2ed2e87574d46e6389c00f66d5eb65b4/sag_py_fastapi_request_id-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "667e90615a11f80f7ae653582ec6e5db60bdcd265fbd88a3a712a61946cc6576",
                "md5": "a594764fc3d9e085c0d65a1a36ff84d5",
                "sha256": "cce09f8161cf23624cc37e57917b1d542b7d7b3158a8f715453c3abe60764035"
            },
            "downloads": -1,
            "filename": "sag_py_fastapi_request_id-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "a594764fc3d9e085c0d65a1a36ff84d5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 5244,
            "upload_time": "2024-04-30T16:42:27",
            "upload_time_iso_8601": "2024-04-30T16:42:27.200720Z",
            "url": "https://files.pythonhosted.org/packages/66/7e/90615a11f80f7ae653582ec6e5db60bdcd265fbd88a3a712a61946cc6576/sag_py_fastapi_request_id-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-30 16:42:27",
    "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": [],
    "lcname": "sag-py-fastapi-request-id"
}
        
Elapsed time: 0.30899s