Skip to content

Commit ab8d1d0

Browse files
rjd22ste93cry
andauthored
Finish the transaction at the kernel.terminate event rather than at the kernel.finish_request event (#465)
Co-authored-by: Stefano Arlandini <[email protected]>
1 parent e0ce070 commit ab8d1d0

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

src/EventListener/TracingRequestListener.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Sentry\Tracing\Transaction;
88
use Sentry\Tracing\TransactionContext;
99
use Symfony\Component\HttpFoundation\Request;
10-
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1110

1211
/**
1312
* This event listener acts on the master requests and starts a transaction
@@ -44,16 +43,12 @@ public function handleKernelRequestEvent(RequestListenerRequestEvent $event): vo
4443

4544
/**
4645
* This method is called for each request handled by the framework and
47-
* ends the tracing.
46+
* ends the tracing on terminate after the client received the response.
4847
*
49-
* @param FinishRequestEvent $event The event
48+
* @param RequestListenerTerminateEvent $event The event
5049
*/
51-
public function handleKernelFinishRequestEvent(FinishRequestEvent $event): void
50+
public function handleKernelTerminateEvent(RequestListenerTerminateEvent $event): void
5251
{
53-
if (!$event->isMasterRequest()) {
54-
return;
55-
}
56-
5752
$transaction = $this->hub->getTransaction();
5853

5954
if (null === $transaction) {

src/Resources/config/services.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
<argument type="service" id="Sentry\State\HubInterface" />
5959

6060
<tag name="kernel.event_listener" event="kernel.request" method="handleKernelRequestEvent" priority="4" />
61-
<tag name="kernel.event_listener" event="kernel.finish_request" method="handleKernelFinishRequestEvent" priority="5" />
6261
<tag name="kernel.event_listener" event="kernel.response" method="handleKernelResponseEvent" priority="15" />
62+
<tag name="kernel.event_listener" event="kernel.terminate" method="handleKernelTerminateEvent" priority="5" />
6363
</service>
6464

6565
<service id="Sentry\SentryBundle\EventListener\TracingSubRequestListener" class="Sentry\SentryBundle\EventListener\TracingSubRequestListener">

tests/EventListener/TracingRequestListenerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Sentry\Options;
1111
use Sentry\SentryBundle\EventListener\RequestListenerRequestEvent;
1212
use Sentry\SentryBundle\EventListener\RequestListenerResponseEvent;
13+
use Sentry\SentryBundle\EventListener\RequestListenerTerminateEvent;
1314
use Sentry\SentryBundle\EventListener\TracingRequestListener;
1415
use Sentry\State\HubInterface;
1516
use Sentry\Tracing\SpanId;
@@ -371,4 +372,37 @@ public function testHandleResponseRequestEventDoesNothingIfNoTransactionIsSetOnH
371372
new Response()
372373
));
373374
}
375+
376+
/**
377+
* @group time-sensitive
378+
*/
379+
public function testHandleKernelTerminateEvent(): void
380+
{
381+
$transaction = new Transaction(new TransactionContext());
382+
383+
$this->hub->expects($this->once())
384+
->method('getTransaction')
385+
->willReturn($transaction);
386+
387+
$this->listener->handleKernelTerminateEvent(new RequestListenerTerminateEvent(
388+
$this->createMock(HttpKernelInterface::class),
389+
new Request(),
390+
new Response()
391+
));
392+
393+
$this->assertSame(microtime(true), $transaction->getEndTimestamp());
394+
}
395+
396+
public function testHandleKernelTerminateEventDoesNothingIfNoTransactionIsSetOnHub(): void
397+
{
398+
$this->hub->expects($this->once())
399+
->method('getTransaction')
400+
->willReturn(null);
401+
402+
$this->listener->handleKernelTerminateEvent(new RequestListenerTerminateEvent(
403+
$this->createMock(HttpKernelInterface::class),
404+
new Request(),
405+
new Response()
406+
));
407+
}
374408
}

0 commit comments

Comments
 (0)