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 test-flask-request-id-header-middleware 0.0.4

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

Source distribution (sdist)

Source distribution for test-flask-request-id-header-middleware 0.0.4
File Size Uploaded Provenance Actions
test_flask_request_id_header_middleware-0.0.4.tar.gz 7.0 kB Without provenance Details for test_flask_request_id_header_middleware-0.0.4.tar.gz

Built distribution (wheel)

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

Total release size: 14.1 kB

Release files / test_flask_request_id_header_middleware-0.0.4.tar.gz

Download URL test_flask_request_id_header_middleware-0.0.4.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
eb47d9156cd5ae611b75505a675479ef5ffa934ca7b38fe4de459f0b1d4c8258
BLAKE2b-256 checksum
How to use checksums
d4439eeb6c44c982b8f7e442446103bb38eb1aa0ae3790af4457982d1daad497
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release files / test_flask_request_id_header_middleware-0.0.4-py3-none-any.whl

Download URL test_flask_request_id_header_middleware-0.0.4-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
decf12fa8ad2d840b7078ab1932a0d40320838d1872fc9ce565365d739947237
BLAKE2b-256 checksum
How to use checksums
c93a94a5f2f0f689b13de9413e11e42b4bd1144a482b8b25b400a7a2e6c864da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Security for test-flask-request-id-header-middleware 0.0.4

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.

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