Inject extra info into flask headers and logs.
Project description
Flask-Xtra-Info
Simple Flask Extension to manage several tasks I was repeadidly doing in every project.
- Request ID generation - Track your requests across multiple APIs
- Request ID parsing - Use request ids from other services [coming soon - not complete]
- Response timing - Log the total time in ms spent processing the request
- Instance ID generation - Identify different flask instances
- Automatic logging - Automatically generate access-log style log entries
- Log filter - Use our log filter to insert the ids into your logs
Use
After installing the extension you can get started right away
from flask import Flask
from flask_xtra_info import XtraInfoExtension
app = Flask(__name__)
# setting the config to false in the line below is simply an example
app.config['XTRA_INFO_GEN_INSTANCE_ID'] = False
# finally, instantiate the extension
xtra = XtraInfoExtension(app)
We also suppoort the app factory pattern
from flask import Flask
from flask_xtra_info import XtraInfoExtension
xtra = XtraInfoExtension()
create_app():
app = Flask(__name__)
xtra.init_app(app)
Once the extension has been instantiated you can find the data in your response headers as well as access it in your logs.
Configuration
The extension is configured via Flask's built-in config object, app.config. If unfamiliar with Flask's app.config, you can read more at: https://flask.palletsprojects.com/
Variable | Default | Type | Description |
---|---|---|---|
XTRA_GEN_REQUEST_ID | True |
Boolean | Should the extension generate request ids |
XTRA_GEN_INSTANCE_ID | True |
Boolean | Should the extension generate an instance id |
XTRA_TIME_RESPONSE | True |
Boolean | Should the extension time the response |
XTRA_CREATE_ACCESSLOG | True |
Boolean | Should the extension automatically generate access-log style logs |
XTRA_INSERT_VERSION | True |
Boolean | Should the extension insert the app version in the header |
XTRA_ACCESSLOG_FMT | [^1] | Header | |
XTRA_REQUEST_ID_HEADER | X-Request-Id |
String | Header to use for request-id |
XTRA_INSTANCE_ID_HEADER | X-Instance-Id |
String | Header to use for instance-id |
XTRA_RESPONSE_TIME_HEADER | X-Response-Time |
String | Header to use for the response time |
XTRA_VERSION_HEADER | X-App-Version |
String | Header to use for the app version |
Log Filter
Finally, an example of using the log filter with dictConfig
import logging
from logging.config import dictConfig
from xtra_info import XtraInfoLogFilter
LOG_CFG = {
"version": 1,
"formatters": {
"xtra_info_format": {
"format": "%(message)s requestId=%(request_id)s via: %(instance_id)s took %(response_time)s ms"
}
},
"filters": {
'xtra_info_filter': {
'()': XtraInfoLogFilter
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "xtra_info_format",
"level": "DEBUG",
"filters: ["xtra_info_filter"]
}
}
}
dictConfig(LOG_CFG)
The 3 variables available to you in your log formatting will be:
- request_id
- instance_id
- response_time
[^1]: '{now} - {ip} - - "{method} {path}" {status_code}'
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-xtra-info-0.3.15.tar.gz
.
File metadata
- Download URL: flask-xtra-info-0.3.15.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddf353ab8733994b3d4dc6ec27c20559bfbf7b150b3ce1b2dd7a2f7f960bf5a1 |
|
MD5 | 90a971febfdf1d7fef4ff5c816b60460 |
|
BLAKE2b-256 | 8b7bb72331897c5997aefc4e3ad7b61a0076c8dfe99f9f582a1dd15813534541 |
File details
Details for the file flask_xtra_info-0.3.15-py3-none-any.whl
.
File metadata
- Download URL: flask_xtra_info-0.3.15-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | afb3fc8293e3b4fd421fa99a564fa76f5119ba3c73b629eb4bac66a4cd7ef02f |
|
MD5 | 7294c371a2427346a6b62a81ec76b3ca |
|
BLAKE2b-256 | e824017128859acd166d7cc2bf2bda48db87b849afbeb952506a9e3d335f7b7f |