Skip to main content

Python Flask Middleware to log and set Request ID in the HTTP header

Project description

Flask RequestID Middleware

A Flask middleware to log and set Request ID in the HTTP header.

Overview

This project provides a Flask middleware that ensures a unique Request ID is generated and logged for each incoming request. The Request ID is also included in the response back to the client.

Features

Generates a unique Request ID for each incoming request Logs the Request ID using a log filter Includes the Request ID in the response back to the client Installation To install the middleware, run the following command:

pip install flask-request-id-header-middleware

Usage

To use the middleware in your Flask application, simply import and initialize it:

from flask import Flask
from flask_request_id_header_middleware import RequestID

app = Flask(__name__)
RequestID(app)

Configuration

The middleware can be configured using the following settings:

  • REQUEST_ID_UNIQUE_VALUE_PREFIX: a prefix that indicates a request ID should be considered unique
app.config['REQUEST_ID_UNIQUE_VALUE_PREFIX'] = 'MY-APP-'

In this example, any request ID that starts with MY-APP- will be considered unique, and will not be modified by the middleware.

For instance, if the client sends a request with the header X-Request-ID: MY-APP-12345, the middleware will not append a new request ID, and will instead return the original value.

Logging

The middleware uses a log filter to inject the current request ID into log records. To use the log filter, add it to your logging configuration:

from flask_request_id_header_middleware.log_filter import RequestIDLogFilter

logging.basicConfig()
logger = logging.getLogger()
logger.addFilter(RequestIDLogFilter())

Example

Here is an example of how to use the middleware in a Flask application:

from flask import Flask
from flask_request_id_header_middleware import RequestID

app = Flask(__name__)
RequestID(app)

@app.route("/")
def index():
    return "Hello, World!"

if __name__ == "__main__":
    app.run()

In this example, the middleware will generate a unique Request ID for each incoming request and log it using the log filter. The Request ID will also be included in the response back to the client.

Tests

To run the tests, run the following command:

flask --app manage.py test

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

flask_request_id_header_middleware-0.0.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file flask_request_id_header_middleware-0.0.2.tar.gz.

File metadata

File hashes

Hashes for flask_request_id_header_middleware-0.0.2.tar.gz
Algorithm Hash digest
SHA256 73bbd983d162a22592e8dc2ff996efddcb3f08f1c4a15b75e5ba55f22b5bd7bb
MD5 a5ee72c931e041ac9b24097e3ec83a56
BLAKE2b-256 5f63209195b3fb0a7ec30900fca96988cd6feefb5e76eae568069f40f671013f

See more details on using hashes here.

File details

Details for the file flask_request_id_header_middleware-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_request_id_header_middleware-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a8b0f9b26ef39a885e11d1ad5876fe14c9ec401da11d38ca27e6c109842c71fd
MD5 f28d018c570633fd14ddc24bab792f48
BLAKE2b-256 e76d9234df4841ca4fcd8a1ef87c84be44a559e78b1f1682cd9d188814dee98e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page