Middleware approach
A middleware runs on every authenticated request. Push user context into a request-scoped container your telemetry tool reads.
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class AttachUserTelemetry
{
public function handle($request, Closure $next)
{
if (Auth::check()) {
$user = Auth::user();
// Push into Laravel's log context — appears on every log line
Log::withContext([
'user_id' => $user->id,
'user_email' => $user->email,
'user_plan' => $user->subscription_plan ?? null,
]);
// For monitoring SDKs that expose a user-setter (NightOwl / Nightwatch)
if (function_exists('nightwatch')) {
nightwatch()->setUser([
'id' => $user->id,
'email' => $user->email,
'plan' => $user->subscription_plan ?? null,
]);
}
}
return $next($request);
}
}Register it on routes that should be tracked:
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
\App\Http\Middleware\AttachUserTelemetry::class,
]);
$middleware->api(append: [
\App\Http\Middleware\AttachUserTelemetry::class,
]);
})What to attach (and what not to)
ATTACH
- User ID (stable, non-secret)
- Email (useful for support)
- Plan or subscription tier
- Team / org ID for B2B apps
- Feature flag variant
AVOID
- Passwords, password hashes
- Payment card info, bank details
- Government IDs, SSNs
- Full addresses, phone numbers
- Any field users haven't consented to share
Frontend → backend correlation
For full visibility, your front end should generate a request ID and pass it via header on every API call. The backend picks it up and tags all telemetry with it.
Frontend (every fetch)
const requestId = crypto.randomUUID();
fetch('/api/orders', {
headers: {
'X-Request-Id': requestId,
// ...
},
});public function handle($request, Closure $next)
{
$id = $request->header('X-Request-Id') ?? (string) \Illuminate\Support\Str::uuid();
$request->attributes->set('request_id', $id);
Log::withContext(['request_id' => $id]);
return tap($next($request), fn ($response) => $response->header('X-Request-Id', $id));
}THE EASY WAY
NightOwl ships with a per-user view
The Nightwatch package captures user_id and email automatically when attached to the request. NightOwl exposes a Users page; click a user to see their detail view with FILTER BY tabs for Requests, Jobs, Exceptions, and Logs in one place. Perfect for support investigations.
composer require nightowl/agent
php artisan nightowl:install