diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a97b938..978eb96 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -12,7 +12,7 @@ jobs: strategy: fail-fast: true matrix: - php: [ 8.1 ] + php: [ 8.1, 8.2, 8.3 ] laravel: [ 9.*, 10.* ] exclude: - php: 8.1 @@ -24,7 +24,7 @@ jobs: steps: - name: Checkout the code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -40,8 +40,3 @@ jobs: - name: Execute the tests run: vendor/bin/phpunit --testdox - - - name: 'Qodana Scan' - uses: JetBrains/qodana-action@main - env: - QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }} diff --git a/src/Middlewares/TreblleMiddleware.php b/src/Middlewares/TreblleMiddleware.php index 29462e1..fcacb62 100644 --- a/src/Middlewares/TreblleMiddleware.php +++ b/src/Middlewares/TreblleMiddleware.php @@ -9,6 +9,7 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Arr; +use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; use Treblle\Exceptions\ConfigurationException; use Treblle\Exceptions\TreblleApiException; @@ -44,7 +45,20 @@ public function handle(Request $request, Closure $next, string $projectId = null self::$start = microtime(true); self::$project = $projectId; - return $next($request); + if (! $request->headers->has('X-TREBLLE-TRACE-ID')) { + $request->headers->add([ + 'X-TREBLLE-TRACE-ID' => $id = Str::uuid(), + ]); + } + + /** @var SymfonyResponse $response */ + $response = $next($request); + + $response->headers->add([ + 'X-TREBLLE-TRACE-ID' => $request->headers->get('X-TREBLLE-TRACE-ID'), + ]); + + return $response; } /** diff --git a/tests/Middleware/TreblleMiddlewareTest.php b/tests/Middleware/TreblleMiddlewareTest.php index 8b02b5c..80fc680 100644 --- a/tests/Middleware/TreblleMiddlewareTest.php +++ b/tests/Middleware/TreblleMiddlewareTest.php @@ -11,21 +11,20 @@ final class TreblleMiddlewareTest extends TestCase { - /** - * @test - * @return void - */ + protected function newMiddleware(): TreblleMiddleware + { + return app()->make( + abstract: TreblleMiddleware::class, + ); + } + + /** @test */ public function it_returns_a_response(): void { $request = new Request(); $response = new Response(); - /** - * @var TreblleMiddleware $middleware - */ - $middleware = app()->make( - abstract: TreblleMiddleware::class, - ); + $middleware = $this->newMiddleware(); $middlewareResponse = $middleware->handle( request: $request, @@ -37,4 +36,23 @@ public function it_returns_a_response(): void actual: $middlewareResponse, ); } + + /** @test */ + public function it_adds_trace_id_to_response(): void + { + $request = new Request(); + $response = new Response(); + + $middleware = $this->newMiddleware(); + + $middlewareResponse = $middleware->handle( + request: $request, + next: fn () => $response, + ); + + $this->assertArrayHasKey( + key: 'x-treblle-trace-id', + array: $middlewareResponse->headers->all(), + ); + } }