Flask-Log-Request-ID

raw JSON →
0.10.1 verified Fri May 01 auth: no python

Flask extension that parses and handles request IDs from headers like X-Request-ID, X-Correlation-ID, and Amazon TraceId. Supports Flask and Celery workers. Current version: 0.10.1. Release cadence is low (last release 2021).

pip install flask-log-request-id
error AttributeError: module 'flask_log_request_id' has no attribute 'current_request_id'
cause Typo or wrong import path. Common mistake: using 'current_request_id' instead of 'current_request_id' (underscore). Also check if you imported the correct symbol.
fix
Use: from flask_log_request_id import current_request_id
error flask.exceptions.ImportError: cannot import name 'RequestID' from 'flask.ext.log_request_id'
cause Using the deprecated import pattern flask.ext.log_request_id which is not supported in newer Flask versions.
fix
Replace with: from flask_log_request_id import RequestID
error RuntimeError: Working outside of request context.
cause Calling current_request_id() outside of a Flask request context (e.g., in a background thread).
fix
Ensure you are inside a request context, or pass the request ID explicitly to the background task.
deprecated The import pattern flask.ext.log_request_id is deprecated since Flask 0.12. Use the modern import flask_log_request_id.
fix Use from flask_log_request_id import RequestID instead.
gotcha The extension does not work out-of-the-box with debug=True in Flask development server (Werkzeug) because the server does not send request IDs by default.
fix Either set an environment variable or use a middleware to inject a test request ID header.
gotcha When using Celery, the request ID is only propagated if you explicitly initialize the extension with the Celery app or use the provided helper.
fix Call init_celery(app, celery) or use the Celery integration documented in README.

Initialize the extension with your Flask app and use current_request_id() to get the current request ID.

from flask import Flask
from flask_log_request_id import RequestID, current_request_id

app = Flask(__name__)
RequestID(app)

@app.route('/')
def index():
    return f"Request ID: {current_request_id()}"

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