[ 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-04-13

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)

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