Skip to content

Commit

Permalink
Fix boundaries for Symfony HttpKernel auto instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
dciprian-petrisor committed Nov 28, 2024
1 parent f6a567b commit 19f787e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/Instrumentation/Symfony/src/SymfonyInstrumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ public static function register(): void
array $params,
?Response $response,
?\Throwable $exception
): void {
$scope = Context::storage()->scope();
if (null === $scope || null === $exception) {
return;
}

$span = Span::fromContext($scope->context());
$scope->detach();
$span->recordException($exception, [
TraceAttributes::EXCEPTION_ESCAPED => true,
]);
if (null !== $response && $response->getStatusCode() >= Response::HTTP_INTERNAL_SERVER_ERROR) {
$span->setStatus(StatusCode::STATUS_ERROR, $exception->getMessage());
}
}
);

hook(
HttpKernel::class,
'terminate',
post: static function (
HttpKernel $kernel,
array $params,
?\Throwable $exception
): void {
$scope = Context::storage()->scope();
if (null === $scope) {
Expand All @@ -89,6 +113,7 @@ public static function register(): void
$span = Span::fromContext($scope->context());

$request = ($params[0] instanceof Request) ? $params[0] : null;
$response = ($params[1] instanceof Response) ? $params[1] : null;
if (null !== $request) {
$routeName = $request->attributes->get('_route', '');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function test_http_kernel_handle_exception(): void
$this->assertCount(0, $this->storage);

$response = $kernel->handle(new Request());
$kernel->terminate(new Request(), $response);

$this->assertArrayHasKey(
TraceResponsePropagator::TRACERESPONSE,
Expand All @@ -51,6 +52,7 @@ public function test_http_kernel_marks_root_as_erroneous(): void
$this->assertCount(0, $this->storage);

$response = $kernel->handle(new Request(), HttpKernelInterface::MAIN_REQUEST, true);
$kernel->terminate(new Request(), $response);

$this->assertCount(1, $this->storage);
$this->assertSame(500, $this->storage[0]->getAttributes()->get(TraceAttributes::HTTP_RESPONSE_STATUS_CODE));
Expand All @@ -72,6 +74,7 @@ public function test_http_kernel_handle_attributes(): void
$request->attributes->set('_route', 'test_route');

$response = $kernel->handle($request);
$kernel->terminate($request, $response);

$attributes = $this->storage[0]->getAttributes();
$this->assertCount(1, $this->storage);
Expand Down Expand Up @@ -106,6 +109,8 @@ public function test_http_kernel_handle_stream_response(): void
$this->assertCount(0, $this->storage);

$response = $kernel->handle(new Request());
$kernel->terminate(new Request(), $response);

$this->assertCount(1, $this->storage);
$this->assertNull($this->storage[0]->getAttributes()->get(TraceAttributes::HTTP_RESPONSE_BODY_SIZE));

Expand All @@ -128,6 +133,8 @@ public function test_http_kernel_handle_binary_file_response(): void
$this->assertCount(0, $this->storage);

$response = $kernel->handle(new Request());
$kernel->terminate(new Request(), $response);

$this->assertCount(1, $this->storage);
$this->assertNull($this->storage[0]->getAttributes()->get(TraceAttributes::HTTP_RESPONSE_BODY_SIZE));

Expand All @@ -152,6 +159,8 @@ public function test_http_kernel_handle_with_empty_route(): void
$request->attributes->set('_route', '');

$response = $kernel->handle($request, HttpKernelInterface::MAIN_REQUEST, true);
$kernel->terminate(new Request(), $response);

$this->assertCount(1, $this->storage);
$this->assertFalse($this->storage[0]->getAttributes()->has(TraceAttributes::HTTP_ROUTE));

Expand All @@ -174,6 +183,8 @@ public function test_http_kernel_handle_without_route(): void
$this->assertCount(0, $this->storage);

$response = $kernel->handle(new Request(), HttpKernelInterface::MAIN_REQUEST, true);
$kernel->terminate(new Request(), $response);

$this->assertCount(1, $this->storage);
$this->assertFalse($this->storage[0]->getAttributes()->has(TraceAttributes::HTTP_ROUTE));

Expand All @@ -197,7 +208,9 @@ public function test_http_kernel_handle_subrequest(): void
$request = new Request();
$request->attributes->set('_controller', 'ErrorController');

$kernel->handle($request, HttpKernelInterface::SUB_REQUEST);
$response = $kernel->handle($request, HttpKernelInterface::SUB_REQUEST);
$kernel->terminate($request, $response);

$this->assertCount(1, $this->storage);

/** @var ImmutableSpan $span */
Expand Down

0 comments on commit 19f787e

Please sign in to comment.