diff --git a/src/Negotiation/NegotiationServiceProvider.php b/src/Negotiation/NegotiationServiceProvider.php index 435fb02..91abb2a 100644 --- a/src/Negotiation/NegotiationServiceProvider.php +++ b/src/Negotiation/NegotiationServiceProvider.php @@ -4,6 +4,7 @@ namespace JustSteveKing\Toolbox\Negotiation; +use Illuminate\Container\Container; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Http\Kernel as KernelContract; use Illuminate\Foundation\Http\Kernel; @@ -71,11 +72,11 @@ public function boot(): void }); if ($tracing && ($options['propagate_trace'] ?? true) === true) { - $app = $this->app; - - $this->callAfterResolving(HttpClient::class, static function (HttpClient $http) use ($app): void { - $http->globalRequestMiddleware(static function (RequestInterface $request) use ($app): RequestInterface { - $current = $app->make(CurrentTrace::class); + $this->callAfterResolving(HttpClient::class, static function (HttpClient $http): void { + $http->globalRequestMiddleware(static function (RequestInterface $request): RequestInterface { + // Resolved from the current container, not the one that booted: Octane serves + // each request from a sandbox, and the trace is scoped to it. + $current = Container::getInstance()->make(CurrentTrace::class); // Something else, such as an OpenTelemetry instrumentation, may have set it. if ($current->context === null || $request->hasHeader('traceparent')) { diff --git a/src/ProblemDetails/ProblemDetailsServiceProvider.php b/src/ProblemDetails/ProblemDetailsServiceProvider.php index 4f3e0f6..fc9a915 100644 --- a/src/ProblemDetails/ProblemDetailsServiceProvider.php +++ b/src/ProblemDetails/ProblemDetailsServiceProvider.php @@ -4,6 +4,7 @@ namespace JustSteveKing\Toolbox\ProblemDetails; +use Illuminate\Container\Container; use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Events\Dispatcher; @@ -97,13 +98,15 @@ public function boot(): void }); } - $this->callAfterResolving(ExceptionHandler::class, function (ExceptionHandler $handler): void { + $this->callAfterResolving(ExceptionHandler::class, static function (ExceptionHandler $handler): void { if (! $handler instanceof Handler) { return; } + // Resolved from the current container, not the one that booted: Octane serves each + // request from a sandbox, and the request ID that becomes "instance" is scoped to it. $handler->renderable( - fn (Throwable $e, Request $request): ?JsonResponse => $this->app->make(ProblemRenderer::class)->render($e, $request), + static fn (Throwable $e, Request $request): ?JsonResponse => Container::getInstance()->make(ProblemRenderer::class)->render($e, $request), ); }); } diff --git a/tests/Feature/OctaneSandboxTest.php b/tests/Feature/OctaneSandboxTest.php new file mode 100644 index 0000000..2ec6216 --- /dev/null +++ b/tests/Feature/OctaneSandboxTest.php @@ -0,0 +1,79 @@ +forgetScopedInstances(); + + Container::setInstance($sandbox); + + try { + return $callback($sandbox); + } finally { + Container::setInstance($base); + } +} + +it('identifies a problem by the request ID of the sandbox serving the request', function (): void { + // The booted application's own request state, which must not be what is rendered. + app(CurrentRequestId::class)->id = '01890a5d-ac96-774b-bcce-b302099a8057'; + + $response = inOctaneSandbox(function (Application $sandbox) { + $sandbox->make(CurrentRequestId::class)->id = '0192d4f2-7c3e-7a41-9b2f-6f1d2c3b4a59'; + + return $sandbox->make(ExceptionHandler::class)->render( + Request::create('api/gone', server: ['HTTP_ACCEPT' => 'application/json']), + new NotFoundHttpException, + ); + }); + + expect(json_decode((string) $response->getContent(), true)) + ->toHaveKey('instance', 'urn:uuid:0192d4f2-7c3e-7a41-9b2f-6f1d2c3b4a59'); +}); + +it('carries the trace of the sandbox serving the request onto outgoing HTTP client requests', function (): void { + Http::fake(); + + // The booted application's own trace, which must not leak onto this request's calls. + app(CurrentTrace::class)->context = new TraceContext('11111111111111111111111111111111', '2222222222222222', 1); + + inOctaneSandbox(function (Application $sandbox): void { + $current = $sandbox->make(CurrentTrace::class); + $current->context = new TraceContext('4bf92f3577b34da6a3ce929d0e0e4736', '00f067aa0ba902b7', 1); + $current->state = 'vendor=abc'; + + Http::get('https://upstream.example.com/'); + }); + + Http::assertSent(fn (ClientRequest $request) => $request->header('traceparent') === ['00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'] + && $request->header('tracestate') === ['vendor=abc']); +});