Skip to main content

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

Metadata

Release files for flask-request-id-header-middleware 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-request-id-header-middleware 0.0.2
File Size Uploaded Provenance Actions
flask_request_id_header_middleware-0.0.2.tar.gz 5.2 kB Without provenance Details for flask_request_id_header_middleware-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-request-id-header-middleware 0.0.2
File Interpreter ABI Platform Actions
flask_request_id_header_middleware-0.0.2-py3-none-any.whl Python 3 none any Details for flask_request_id_header_middleware-0.0.2-py3-none-any.whl

Total release size: 11.5 kB

Release files / flask_request_id_header_middleware-0.0.2.tar.gz

Download URL flask_request_id_header_middleware-0.0.2.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
73bbd983d162a22592e8dc2ff996efddcb3f08f1c4a15b75e5ba55f22b5bd7bb
BLAKE2b-256 checksum
How to use checksums
5f63209195b3fb0a7ec30900fca96988cd6feefb5e76eae568069f40f671013f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Release files / flask_request_id_header_middleware-0.0.2-py3-none-any.whl

Download URL flask_request_id_header_middleware-0.0.2-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8b0f9b26ef39a885e11d1ad5876fe14c9ec401da11d38ca27e6c109842c71fd
BLAKE2b-256 checksum
How to use checksums
e76d9234df4841ca4fcd8a1ef87c84be44a559e78b1f1682cd9d188814dee98e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.12

Security for flask-request-id-header-middleware 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page