Attach a unique identifier to every HTTP request in your WSGI or ASGI application.
request-id-flask
is implemented as a WSGI and ASGI middleware.
The package will do one of two things:
-
Read the
X-Request-ID
HTTP header from a client HTTP request and return the sameX-Request-ID
HTTP header in the server HTTP response and is stored in the WSGIenviron
. -
Or, when no
X-Request-ID
HTTP header is present in the client HTTP request, generate a new and uniquerequest_id
identifier (using uuid 4) which is stored in the WSGIenviron
and set as theX-Request-ID
HTTP header in the server HTTP responser.
- Python 3.6 or above
You can install the request-id-flask package using pip:
pip install request-id-flask
However, recommended to add it to the requirements.txt
file instead, and install using: pip install -r requirements.txt
request-id-flask
The REQUEST_ID
is stored in the request environ
dictionary and may be accessed from anywhere this is available in Flask.
- Version 0.0.4 adds support for Quart.
- Version 0.0.3 adds support for the app factory pattern.
from flask import (
Flask,
request
)
from request_id import RequestId
app = Flask(__name__)
RequestId(app)
@app.route('/')
def index():
request_id = request.environ.get('REQUEST_ID', '')
return str(request_id)
from quart import (
Quart,
request
)
from request_id import RequestId
app = Quart(__name__)
RequestId(app)
@app.route('/')
async def index():
request_id = request.environ.get('REQUEST_ID', '')
return await str(request_id)