Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Trace ID header to request and response in middleware #85

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
16 changes: 15 additions & 1 deletion src/Middlewares/TreblleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +44,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;
}

/**
Expand Down
33 changes: 27 additions & 6 deletions tests/Middleware/TreblleMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

final class TreblleMiddlewareTest extends TestCase
{
protected function newMiddleware(): TreblleMiddleware
{
return app()->make(
abstract: TreblleMiddleware::class,
);
}

/**
* @test
* @return void
Expand All @@ -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,
Expand All @@ -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(),
);
}
}
Loading