From a24e6833e13a069d67eaaa71df62f9f61971eb43 Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Tue, 24 Oct 2023 18:27:11 +0100 Subject: [PATCH 1/3] Adding Trace ID header to request and response in middleware --- src/Middlewares/TreblleMiddleware.php | 14 ++++++++- tests/Middleware/TreblleMiddlewareTest.php | 33 ++++++++++++++++++---- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/Middlewares/TreblleMiddleware.php b/src/Middlewares/TreblleMiddleware.php index 99e71d5..9e0caf8 100644 --- a/src/Middlewares/TreblleMiddleware.php +++ b/src/Middlewares/TreblleMiddleware.php @@ -8,6 +8,7 @@ use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; +use Illuminate\Support\Str; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; use Treblle\Exceptions\ConfigurationException; use Treblle\Exceptions\TreblleApiException; @@ -43,7 +44,18 @@ public function handle(Request $request, Closure $next, string $projectId = null self::$start = microtime(true); self::$project = $projectId; - return $next($request); + $request->headers->add([ + 'X-TREBLLE-TRACE-ID' => $id = Str::uuid(), + ]); + + /** @var SymfonyResponse $response */ + $response = $next($request); + + $response->headers->add([ + 'X-TREBLLE-TRACE-ID' => $id, + ]); + + return $response; } /** diff --git a/tests/Middleware/TreblleMiddlewareTest.php b/tests/Middleware/TreblleMiddlewareTest.php index 8b02b5c..0b7f1bc 100644 --- a/tests/Middleware/TreblleMiddlewareTest.php +++ b/tests/Middleware/TreblleMiddlewareTest.php @@ -11,6 +11,13 @@ final class TreblleMiddlewareTest extends TestCase { + protected function newMiddleware(): TreblleMiddleware + { + return app()->make( + abstract: TreblleMiddleware::class, + ); + } + /** * @test * @return void @@ -20,12 +27,7 @@ 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 +39,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(), + ); + } } From d2df102ebafabe763ab8a46989836f56d0f852a3 Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Tue, 24 Oct 2023 18:30:35 +0100 Subject: [PATCH 2/3] Making sure the trace that comes through stays with the request --- src/Middlewares/TreblleMiddleware.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Middlewares/TreblleMiddleware.php b/src/Middlewares/TreblleMiddleware.php index 9e0caf8..d527cbe 100644 --- a/src/Middlewares/TreblleMiddleware.php +++ b/src/Middlewares/TreblleMiddleware.php @@ -44,15 +44,17 @@ public function handle(Request $request, Closure $next, string $projectId = null self::$start = microtime(true); self::$project = $projectId; - $request->headers->add([ - 'X-TREBLLE-TRACE-ID' => $id = Str::uuid(), - ]); + 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' => $id, + 'X-TREBLLE-TRACE-ID' => $request->headers->get('X-TREBLLE-TRACE-ID'), ]); return $response; From bc61a3dce985ae75f5c4a635cfe179a01714e228 Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Tue, 24 Oct 2023 18:35:22 +0100 Subject: [PATCH 3/3] Removing Qodana from CI --- .github/workflows/tests.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a97b938..0177a20 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 }}