Skip to content
Merged
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
11 changes: 6 additions & 5 deletions src/Negotiation/NegotiationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')) {
Expand Down
7 changes: 5 additions & 2 deletions src/ProblemDetails/ProblemDetailsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
);
});
}
Expand Down
79 changes: 79 additions & 0 deletions tests/Feature/OctaneSandboxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

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;

/*
* Octane serves each request from a sandbox: a clone of the booted application, made the
* container instance, with its scoped bindings fresh. Request state lives in the sandbox, so
* anything resolved while handling the request has to come from there, not from the application
* that booted. These tests build that sandbox by hand, so Octane does not need to be installed.
*/

/**
* @template T
*
* @param Closure(Application): T $callback
* @return T
*/
function inOctaneSandbox(Closure $callback): mixed
{
$base = Container::getInstance();
assert($base instanceof Application);

$sandbox = clone $base;
$sandbox->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']);
});
Loading