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.4.tar.gz
.
File metadata
- Download URL: Flask-Log-Request-ID-0.9.4.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1173dc4cbac373ccc43bc79b33e5ac994f901338cec878f1d92f75a0cbf64b6d |
|
MD5 | 82cf1bae8aba757b6ee70d10ebacc4c4 |
|
BLAKE2b-256 | fd9f07b7e64f2d3cb86c576dc84ae6c5a5cb06d976cd8259ba0ee938e19e0910 |
File details
Details for the file Flask_Log_Request_ID-0.9.4-py3-none-any.whl
.
File metadata
- Download URL: Flask_Log_Request_ID-0.9.4-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf2cd650898b29499783b65cca260d73314c58d0e856eb3bc8309fc1192b6500 |
|
MD5 | 4b1858a45df777787d74eaa5b385b5f2 |
|
BLAKE2b-256 | c3704a0c794352a27edf634e0ab47fe92a07c7f3e4428cb124638e77ecfbe339 |