The middleware provides two contextvars.ContextVar instances for
request-scoped storage. These variables make the correlation ID and user ID
available throughout the application — including code that does not have access
to Falcon's req object, such as logging filters, utility functions, and
downstream service clients.
correlation_id_var
A context variable for the current request's correlation ID.
- Type:
contextvars.ContextVar[str | None] - Default:
None
from falcon_correlate import correlation_id_var
# Retrieve the current correlation ID (None if not set)
cid = correlation_id_var.get()
user_id_var
A context variable for the current request's authenticated user ID.
- Type:
contextvars.ContextVar[str | None] - Default:
None
from falcon_correlate import user_id_var
# Retrieve the current user ID (None if not set)
uid = user_id_var.get()
Note: correlation_id_var lifecycle is managed automatically by the
middleware. It is set during process_request and reset during
process_response, including request-failure paths. The user_id_var is
intended for use by authentication middleware or application code that
identifies the current user.