diff --git a/src/Middleware/CakeSentryPerformanceMiddleware.php b/src/Middleware/CakeSentryPerformanceMiddleware.php index 40cf675..e2437d5 100644 --- a/src/Middleware/CakeSentryPerformanceMiddleware.php +++ b/src/Middleware/CakeSentryPerformanceMiddleware.php @@ -15,6 +15,7 @@ use Cake\Datasource\ConnectionManager; use Cake\Event\EventManager; +use Cake\Http\Server; use CakeSentry\Database\Log\CakeSentryLog; use CakeSentry\EventListener; use CakeSentry\QuerySpanTrait; @@ -92,7 +93,15 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface SentrySdk::getCurrentHub()->setSpan($transaction); $transaction->setHttpStatus($response->getStatusCode()); - $transaction->finish(); + + if (function_exists('fastcgi_finish_request') && method_exists(Server::class, 'terminate')) { + // Send the transaction to sentry after the client has received the response + EventManager::instance()->on('Server.terminate', function () use ($transaction): void { + $transaction->finish(); + }); + } else { + $transaction->finish(); + } return $response; }