[ GLOSSARY ]

What is a correlation ID?

QUICK ANSWER

What is a correlation ID and how do I add one to Laravel?

A correlation ID is a unique identifier attached to every log, span, and downstream request for a single user action — so you can reconstruct what happened end-to-end. In Laravel, add a middleware that generates an ID (or reuses the client's X-Request-Id header), attach it to the log context via Log::withContext, and propagate it on outbound HTTP calls. When paired with a trace ID (from nightwatch or OpenTelemetry), you get log-trace correlation in every incident postmortem.

Updated · 2026-06-04

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 'req_5c1d...' across every log source and you have the full timeline.

Implementing in Laravel

app/Http/Middleware/AssignRequestId.php

php
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class AssignRequestId
{
    public function handle(Request $request, Closure $next)
    {
        $id = $request->header('X-Request-Id', Str::uuid()->toString());

        Log::withContext([
            'request_id' => $id,
            'user_id' => auth()->id(),
            'route' => $request->route()?->uri(),
        ]);

        $response = $next($request);
        $response->headers->set('X-Request-Id', $id);

        return $response;
    }
}

Register globally in bootstrap/app.php. Every subsequent log line in that request carries request_id. The response header lets clients report the ID when filing bug reports.

Propagating to outbound HTTP

php
use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
    'X-Request-Id' => request()->header('X-Request-Id'),
    'traceparent' => request()->header('traceparent'), // if using OTel
])->get($url);

Or set it once via a Laravel Http::macro() so it's automatic on every outbound call.

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 share the same underlying UUID if you want.

What to include in log context

Beyond the correlation ID itself:

  • user_id (hashed or numeric, not PII)
  • route or controller
  • deploy_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. Pass the ID into the job's constructor and re-apply it with Log::withContext at the top of handle(). Same idea for scheduled commands and any other async work — the ID is only as useful as the boundaries it survives.

Frequently asked questions

What is a correlation ID?

A unique identifier attached to every log line, span, and downstream request associated with a single user action or request. It lets you reconstruct 'what happened' for that request across logs, traces, and downstream services. Often called a request ID, trace ID, or transaction ID depending on context — the concepts are equivalent.

How do I add a correlation ID in Laravel?

Generate one in a middleware (or reuse the client's X-Request-Id header if they sent one), attach it to the log context via Log::withContext(), and propagate it on outbound HTTP calls. Every log line for that request will carry the ID. Combine with a trace ID from OTel or Nightwatch to correlate logs with traces.

What's the difference between a correlation ID and a trace ID?

Often they're the same thing. OpenTelemetry uses trace_id as the 16-byte correlation ID propagated across services. Older systems invented their own (X-Request-Id, X-Correlation-ID). When in doubt, use OTel's trace_id — it's the de facto standard and tools understand it natively.

Should I log the correlation ID on every log line?

Yes, unconditionally. It's the single most valuable piece of metadata for incident response — you can pull every log line for a broken request in a single query. In Laravel, Log::withContext(['request_id' => $id]) in a middleware handles this for all downstream logs.

How do I propagate a correlation ID to external APIs?

Pass it as a header on every outbound request. The W3C standard is traceparent (which encodes trace_id, span_id, and sampling flag). A simpler per-request header is X-Request-Id — many APIs echo it back in their logs so you can cross-reference. Laravel's Http facade: Http::withHeaders(['X-Request-Id' => request()->header('X-Request-Id')]).

Does Laravel generate a correlation ID automatically?

Not in the framework core. Laravel's nightwatch package generates one per request for internal tracing. Otherwise add a simple middleware: if the client sent X-Request-Id use it, else Str::uuid()->toString(). Attach to log context, propagate on outbound calls.

PRICING

Flat pricing. No event caps. No per-seat fees.

14-day free trial, no credit card. Your PostgreSQL, your data.

HOBBY

$5 /month

1 app · 14 days lookback · all Laravel events

TEAM

$15 /month

Up to 3 connected apps · unlimited environments · all Laravel events

AGENCY

$69 /month

Unlimited apps · unlimited agent instances · same flat rate at any traffic