honeybadger-io/honeybadger-laravel

View on GitHub
src/Middleware/AssignRequestId.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Honeybadger\HoneybadgerLaravel\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Response;

class AssignRequestId
{
    /**
     * Add request id to the log context.
     *
     * @param Closure(Request): (Response) $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if ($request->headers->has('Request-Id')) {
            $requestId = $request->headers->get('Request-Id');
        }
        else if ($request->headers->has('X-Request-Id')) {
            $requestId = $request->headers->get('X-Request-Id');
        }
        else {
            $requestId = (string) Str::uuid();

            $request->headers->set('X-Request-Id', $requestId);
        }

        Log::shareContext([
            'requestId' => $requestId
        ]);

        return $next($request);
    }
}