March Docs

CorrelationId

CorrelationId module: request correlation ID middleware for Bastion.

Assigns a unique correlation ID to every incoming request. The ID is either taken from the incoming X-Request-ID header (if present and at most 200 bytes) or generated fresh as a UUID v4.

The ID is stored in conn.assigns["request_id"] and echoed back in the X-Request-ID response header. It is also injected into the Logger context so that every subsequent log call in the request lifecycle includes the ID automatically — no manual threading required.

Typical pipeline:

conn |> CorrelationId.assign() |> Session.load(:cookie) |> MyApp.Router.route()

After assign(), all Logger calls in this request process include: request_id=<uuid>

Structured logging with metadata:

Logger.log_with(Logger.Info, "user signed in", [("user_id", uid)]) -- emits: [INFO] user signed in {request_id=..., user_id=42}

Clearing the correlation ID context (e.g., at request end):

CorrelationId.clear()