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.5

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.5
File Size Uploaded Provenance Actions
test_flask_request_id_header_middleware-0.0.5.tar.gz 7.2 kB Without provenance Details for test_flask_request_id_header_middleware-0.0.5.tar.gz

Built distribution (wheel)

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

Total release size: 14.4 kB

Release files / test_flask_request_id_header_middleware-0.0.5.tar.gz

Download URL test_flask_request_id_header_middleware-0.0.5.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
58ffae8ebb59f12544db03fc9f533d2d8e890debd9f434111d0810eae5bcc1bd
BLAKE2b-256 checksum
How to use checksums
f0b778c951d89ed9ed6b21db8ffd869189606c2e523b4031dd8154b814830e2c
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.5-py3-none-any.whl

Download URL test_flask_request_id_header_middleware-0.0.5-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
684233bbeffdf44e18b7757f669901d1f2e684234d86dc2c63601d1bfd62c77f
BLAKE2b-256 checksum
How to use checksums
51520df180928bcad9580be3a4cef7d7ac5c35a21a4f130cd0b6d1ac78ccf4b1
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.5

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