Laravel Utilities

Correlate Requests and Queue Jobs with the Context Facade

Punyapal Shah 1 min read
edit this tip
Use the Context facade to capture request metadata that automatically propagates to logs, exception reports, and queued jobs.

When an HTTP request dispatches asynchronous queue jobs, contextual details (like the requesting user ID, tenant ID, or trace UUID) are traditionally lost inside background workers.

Laravel's Context facade stores cross-cutting metadata that automatically flows into logs and queued jobs.

Capturing Context in Middleware

namespace App\Http\Middleware;

use Closure;
use IlluminateHttpRequest;
use Illuminate\Support\Facades\Context;
use Illuminate\Support\Str;

class CaptureRequestContext
{
    public function handle(Request $request, Closure $next)
    {
        // Add metadata to active Context
        Context::add('trace_id', (string) Str::uuid());
        Context::add('user_id', $request->user()?->id);
        Context::add('ip', $request->ip());

        return $next($request);
    }
}

Automatic Propagation to Queued Jobs and Logs

When jobs are dispatched during this request, their execution in worker processes automatically retains the context:

// Inside a background job:
Log::info('Generating PDF export.');

// Log entry automatically contains:
// {"trace_id":"8a9f...", "user_id":42, "ip":"127.0.0.1"}

Retrieving and Checking Context Values

$traceId = Context::get('trace_id');
$hasUser = Context::has('user_id');

Summary

  • Propagates metadata across HTTP requests, logging engines, and queued jobs.
  • Enables end-to-end distributed transaction tracing.
  • Native to modern Laravel with zero configuration overhead.
Tags: Laravel Logging Queue Architecture
Share on X

// Found an issue or want to contribute a tip? github.com/MrPunyapal/tips