From cdc3ed15d07e20c2142dd22311916b65bfa013be Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Tue, 20 Oct 2026 11:38:28 +0100 Subject: [PATCH 1/2] Render problems from the container serving the request, so Octane keeps their instance The renderable callback resolved ProblemRenderer from the application that booted the provider. Under Octane each request is served from a sandboxed clone whose scoped CurrentRequestId holds the request ID, so problems rendered from the base application lost their urn:uuid instance while the X-Request-Id header was still sent. The callback now resolves from Container::getInstance(), which is the sandbox under Octane and the application itself otherwise. --- .../ProblemDetailsServiceProvider.php | 7 ++- tests/Feature/OctaneSandboxTest.php | 57 +++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/OctaneSandboxTest.php 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..24076fb --- /dev/null +++ b/tests/Feature/OctaneSandboxTest.php @@ -0,0 +1,57 @@ +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'); +}); From cf2c40efbd4a81c62d61287b7da2ec7d6ffec696 Mon Sep 17 00:00:00 2001 From: Steve McDougall Date: Tue, 20 Oct 2026 11:38:39 +0100 Subject: [PATCH 2/2] Propagate the trace of the request being served onto outgoing HTTP client requests under Octane The HTTP client middleware read CurrentTrace from the application captured at boot. Under Octane that is not the sandbox serving the request, so outgoing requests carried no traceparent or tracestate, and anything that ever set the base application's trace would have leaked it onto every request. The middleware now resolves CurrentTrace from Container::getInstance() when the request is sent. --- .../NegotiationServiceProvider.php | 11 +++++----- tests/Feature/OctaneSandboxTest.php | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) 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/tests/Feature/OctaneSandboxTest.php b/tests/Feature/OctaneSandboxTest.php index 24076fb..2ec6216 100644 --- a/tests/Feature/OctaneSandboxTest.php +++ b/tests/Feature/OctaneSandboxTest.php @@ -5,8 +5,12 @@ use Illuminate\Container\Container; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Foundation\Application; +use Illuminate\Http\Client\Request as ClientRequest; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Http; use JustSteveKing\Toolbox\Negotiation\CurrentRequestId; +use JustSteveKing\Toolbox\Negotiation\CurrentTrace; +use JustSteveKing\Toolbox\Negotiation\TraceContext; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /* @@ -55,3 +59,21 @@ function inOctaneSandbox(Closure $callback): mixed 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']); +});