Flask extension that can parse and handle multiple types of request-id sent by request processors like Amazon ELB, Heroku or any multi-tier infrastructure as the one used for microservices.
Project description
Flask-Log-Request-Id is an extension for Flask that can parse and handle the request-id sent by request processors like Amazon ELB, Heroku Request-ID or any multi-tier infrastructure as the one used at microservices. A common usage scenario is to inject the request_id in the logging system so that all log records, even those emitted by third party libraries, have attached the request_id that initiated their call. This can greatly improve tracing and debugging of problems.
Features
Flask-Log-Request-Id provides the current_request_id() function which can be used at any time to get the request id of the initiated execution chain. It also comes with log filter to inject this information on log events as also an extension to forward the current request id into Celery’s workers.
Example: Parse request id and send it to to logging
In the following example, we will use the RequestIDLogFilter to inject the request id on all log events, and a custom formatter to print this information. If all these sounds unfamiliar please take a look at python logging system
import logging
import logging.config
from random import randint
from flask import Flask
from flask_log_request_id import RequestID, RequestIDLogFilter
def generic_add(a, b):
"""Simple function to add two numbers that is not aware of the request id"""
logging.debug('Called generic_add({}, {})'.format(a, b))
return a + b
app = Flask(__name__)
RequestID(app)
# Setup logging
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - level=%(levelname)s - request_id=%(request_id)s - %(message)s"))
handler.addFilter(RequestIDLogFilter()) # << Add request id contextual filter
logging.getLogger().addHandler(handler)
@app.route('/')
def index():
a, b = randint(1, 15), randint(1, 15)
logging.info('Adding two random numbers {} {}'.format(a, b))
return str(generic_add(a, b))
Installation
The easiest way to install it is using pip from PyPI
pip install flask-log-request-id
License
See the LICENSE file for license rights and limitations (MIT).
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
Built Distribution
File details
Details for the file Flask-Log-Request-ID-0.9.3.tar.gz
.
File metadata
- Download URL: Flask-Log-Request-ID-0.9.3.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38ba219f893f3cf3439bf6e31f808a6fca902ae68cb8b05871548de109ea5dc1 |
|
MD5 | 87dd21c61dde073e8fc4f9efc90c3844 |
|
BLAKE2b-256 | 3a6eef8b2fbf353385ad3dedd68d9b0fc628f883fc6d1c9b9c2208ada06227e7 |
File details
Details for the file Flask_Log_Request_ID-0.9.3-py3-none-any.whl
.
File metadata
- Download URL: Flask_Log_Request_ID-0.9.3-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f485d26d51933677c31430cbd637feae7b92bf6c1cf832fd5ef17b5c56afee48 |
|
MD5 | 7d5eba1da902b3734816e6d8cce82897 |
|
BLAKE2b-256 | 7324ce76a9c8473d27547abe55cfc185b59c5daa5b043982df5ae8d35e08ba15 |