diff --git a/src/Instrumentation/Symfony/src/SymfonyInstrumentation.php b/src/Instrumentation/Symfony/src/SymfonyInstrumentation.php index 3b205355..340e7fb9 100644 --- a/src/Instrumentation/Symfony/src/SymfonyInstrumentation.php +++ b/src/Instrumentation/Symfony/src/SymfonyInstrumentation.php @@ -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) { @@ -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', ''); diff --git a/src/Instrumentation/Symfony/tests/Integration/SymfonyInstrumentationTest.php b/src/Instrumentation/Symfony/tests/Integration/SymfonyInstrumentationTest.php index 7bbe0a60..eddea2c2 100644 --- a/src/Instrumentation/Symfony/tests/Integration/SymfonyInstrumentationTest.php +++ b/src/Instrumentation/Symfony/tests/Integration/SymfonyInstrumentationTest.php @@ -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, @@ -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)); @@ -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); @@ -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)); @@ -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)); @@ -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)); @@ -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)); @@ -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 */