Skip to content

Improve symfony response times when tracing is enabled #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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: 3 additions & 8 deletions src/EventListener/TracingRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;

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

/**
* This method is called for each request handled by the framework and
* ends the tracing.
* ends the tracing on terminate after the client received the response.
*
* @param FinishRequestEvent $event The event
* @param RequestListenerTerminateEvent $event The event
*/
public function handleKernelFinishRequestEvent(FinishRequestEvent $event): void
public function handleKernelTerminateEvent(RequestListenerTerminateEvent $event): void
{
if (!$event->isMasterRequest()) {
return;
}

$transaction = $this->hub->getTransaction();

if (null === $transaction) {
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<argument type="service" id="Sentry\State\HubInterface" />

<tag name="kernel.event_listener" event="kernel.request" method="handleKernelRequestEvent" priority="4" />
<tag name="kernel.event_listener" event="kernel.finish_request" method="handleKernelFinishRequestEvent" priority="5" />
<tag name="kernel.event_listener" event="kernel.response" method="handleKernelResponseEvent" priority="15" />
<tag name="kernel.event_listener" event="kernel.terminate" method="handleKernelTerminateEvent" priority="5" />
</service>

<service id="Sentry\SentryBundle\EventListener\TracingSubRequestListener" class="Sentry\SentryBundle\EventListener\TracingSubRequestListener">
Expand Down
34 changes: 34 additions & 0 deletions tests/EventListener/TracingRequestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Sentry\Options;
use Sentry\SentryBundle\EventListener\RequestListenerRequestEvent;
use Sentry\SentryBundle\EventListener\RequestListenerResponseEvent;
use Sentry\SentryBundle\EventListener\RequestListenerTerminateEvent;
use Sentry\SentryBundle\EventListener\TracingRequestListener;
use Sentry\State\HubInterface;
use Sentry\Tracing\SpanId;
Expand Down Expand Up @@ -371,4 +372,37 @@ public function testHandleResponseRequestEventDoesNothingIfNoTransactionIsSetOnH
new Response()
));
}

/**
* @group time-sensitive
*/
public function testHandleKernelTerminateEvent(): void
{
$transaction = new Transaction(new TransactionContext());

$this->hub->expects($this->once())
->method('getTransaction')
->willReturn($transaction);

$this->listener->handleKernelTerminateEvent(new RequestListenerTerminateEvent(
$this->createMock(HttpKernelInterface::class),
new Request(),
new Response()
));

$this->assertSame(microtime(true), $transaction->getEndTimestamp());
}

public function testHandleKernelTerminateEventDoesNothingIfNoTransactionIsSetOnHub(): void
{
$this->hub->expects($this->once())
->method('getTransaction')
->willReturn(null);

$this->listener->handleKernelTerminateEvent(new RequestListenerTerminateEvent(
$this->createMock(HttpKernelInterface::class),
new Request(),
new Response()
));
}
}