A correlation ID is the cheapest piece of observability you can add, and one of the highest-leverage. It's a string you generate once per request and attach to everything that request touches. That's the whole idea. The payoff shows up the first time you have to answer "what happened to this one user at 14:23" and you can pull the entire story with a single filter instead of stitching it together from timestamps.
The 3am incident use case
A customer reports their checkout failed at 14:23. Without a correlation ID, you grep logs by timestamp + user ID, miss half the story because cross-service logs don't share keys, and reconstruct the flow by guessing. With a correlation ID: one grep '5c1d9e2a-...' across every log source and you have the full timeline.
Implementing in Laravel
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Str;
class AssignRequestId
{
public function handle(Request $request, Closure $next)
{
$id = $request->header('X-Request-Id') ?: Str::uuid()->toString();
Context::add([
'request_id' => $id,
'user_id' => auth()->id(),
'route' => $request->route()?->uri(),
]);
$response = $next($request);
$response->headers->set('X-Request-Id', $id);
return $response;
}
}
Append it to the web and api groups in bootstrap/app.php with $middleware->web(append: [AssignRequestId::class]) and the same for api. Global middleware runs before routing and before the session starts, so there the route and the user aren't known yet. Laravel adds Context values to every log line written after this point, so each one carries request_id. The response header lets clients report the ID when filing bug reports.
Propagating to outbound HTTP
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Facades\Http;
use Psr\Http\Message\RequestInterface;
public function boot(): void
{
Http::globalRequestMiddleware(function (RequestInterface $request) {
$id = Context::get('request_id');
return $id ? $request->withHeader('X-Request-Id', $id) : $request;
});
}
This adds the header to every call made through the Http facade. Read the ID from Context, not from the incoming request: if the client didn't send X-Request-Id, the incoming header is empty and only Context holds the ID you generated. If you use OpenTelemetry, don't copy the incoming traceparent onto outbound calls either. Let the OTel propagator inject it, so the header names your outbound span as the parent rather than the upstream caller's.
Correlation ID vs trace ID
Pre-OpenTelemetry era: X-Request-Id was the common header, implementations varied. Post-OTel: use W3C traceparent. It encodes trace_id + span_id + sampling flag in one header, and every OTel-instrumented service understands it natively.
For interoperability, send both. X-Request-Id for simple log correlation, traceparent for tracing tooling. They can carry the same value if you want: use the 32-character trace ID as your request ID.
What to include in log context
Beyond the correlation ID itself:
user_id(hashed or numeric, not PII)routeorcontrollerdeploy_sha(git SHA of the running build)env(production, staging)
Don't lose it at the queue boundary
The place correlation IDs quietly break is the queue. A request dispatches a
job and returns; the job runs seconds later in a different process, and unless
you carried the ID across, its logs have no link to the request that caused
them. Laravel's Context handles this for you: values added with
Context::add() are stored with every job the request
dispatches and restored when the job runs, so the job's logs carry the same
request_id. Scheduled commands don't start from a request, so
add your own ID with Context::add() at the start of the
command. The ID is only as useful as the boundaries it
survives.