Skip to content

Push to the sentry transaction #160

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ public function getConfigTreeBuilder()
->children()
->scalarNode('request')->defaultValue(0)->end()
->scalarNode('kernel_exception')->defaultValue(0)->end()
->scalarNode('finish_request')->defaultValue(0)->end()
->scalarNode('console_exception')->defaultValue(0)->end()
->scalarNode('console_terminate')->defaultValue(0)->end()
->end()
->end()
->end()
Expand Down
24 changes: 23 additions & 1 deletion src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -82,6 +84,10 @@ public function setClient(\Raven_Client $client)
*/
public function onKernelRequest(GetResponseEvent $event): void
{
if (($route = $event->getRequest()->attributes->get('_route'))) {
$this->client->transaction->push($route);
}

if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
Expand Down Expand Up @@ -115,6 +121,13 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
$this->client->captureException($exception);
}

public function onFinishRequest(FinishRequestEvent $event): void
{
if (($route = $event->getRequest()->attributes->get('_route'))) {
$this->client->transaction->pop($route);
}
}

/**
* This method only ensures that the client and error handlers are registered at the start of the command
* execution cycle, and not only on exceptions
Expand All @@ -125,7 +138,9 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
*/
public function onConsoleCommand(ConsoleCommandEvent $event): void
{
// only triggers loading of client, does not need to do anything.
if (($command = $event->getCommand())) {
$this->client->transaction->push($command->getName());
}
}

public function onConsoleError(ConsoleErrorEvent $event): void
Expand All @@ -138,6 +153,13 @@ public function onConsoleException(ConsoleExceptionEvent $event): void
$this->handleConsoleError($event);
}

public function onFinishCommand(ConsoleTerminateEvent $event): void
{
if (($command = $event->getCommand())) {
$this->client->transaction->pop($command->getName());
}
}

/**
* @param ConsoleExceptionEvent|ConsoleErrorEvent $event
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="%sentry.listener_priorities.request%" />
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="%sentry.listener_priorities.kernel_exception%" />
<tag name="kernel.event_listener" event="kernel.finish_request" method="onFinishRequest" priority="%sentry.listener_priorities.finish_request%" />
<tag name="kernel.event_listener" event="console.command" method="onConsoleCommand" />
<tag name="kernel.event_listener" event="console.error" method="onConsoleError" priority="%sentry.listener_priorities.console_exception%" />
<tag name="kernel.event_listener" event="console.terminate" method="onFinishCommand" priority="%sentry.listener_priorities.console_terminate%" />
</service>
</services>
</container>
10 changes: 10 additions & 0 deletions test/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,22 @@ public function test_that_it_has_proper_event_listener_tags_for_exception_listen
'method' => 'onKernelException',
'priority' => '%sentry.listener_priorities.kernel_exception%',
],
[
'event' => 'kernel.finish_request',
'method' => 'onFinishRequest',
'priority' => '%sentry.listener_priorities.finish_request%',
],
['event' => 'console.command', 'method' => 'onConsoleCommand'],
[
'event' => 'console.error',
'method' => 'onConsoleError',
'priority' => '%sentry.listener_priorities.console_exception%',
],
[
'event' => 'console.terminate',
'method' => 'onFinishCommand',
'priority' => '%sentry.listener_priorities.console_terminate%',
],
],
$tags
);
Expand Down
148 changes: 148 additions & 0 deletions test/EventListener/ExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
use Sentry\SentryBundle\EventListener\SentryExceptionListenerInterface;
use Sentry\SentryBundle\SentrySymfonyEvents;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
Expand Down Expand Up @@ -81,6 +84,10 @@ public function test_that_user_data_is_not_set_on_subrequest()
{
$mockEvent = $this->createMock(GetResponseEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockEvent
->expects($this->once())
->method('getRequestType')
Expand All @@ -107,6 +114,10 @@ public function test_that_user_data_is_not_set_if_token_storage_not_present()

$mockEvent = $this->createMock(GetResponseEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockEvent
->expects($this->once())
->method('getRequestType')
Expand Down Expand Up @@ -136,6 +147,10 @@ public function test_that_user_data_is_not_set_if_authorization_checker_not_pres

$mockEvent = $this->createMock(GetResponseEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockEvent
->expects($this->once())
->method('getRequestType')
Expand Down Expand Up @@ -167,6 +182,10 @@ public function test_that_user_data_is_not_set_if_token_not_present()

$mockEvent = $this->createMock(GetResponseEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockEvent
->expects($this->once())
->method('getRequestType')
Expand Down Expand Up @@ -209,6 +228,10 @@ public function test_that_user_data_is_not_set_if_not_authorized()

$mockEvent = $this->createMock(GetResponseEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockEvent->expects($this->once())
->method('getRequestType')
->willReturn(HttpKernelInterface::MASTER_REQUEST);
Expand Down Expand Up @@ -254,6 +277,10 @@ public function test_that_username_is_set_from_user_interface_if_token_present_a

$mockEvent = $this->createMock(GetResponseEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockEvent
->expects($this->once())
->method('getRequestType')
Expand Down Expand Up @@ -303,6 +330,10 @@ public function test_that_username_is_set_from_user_interface_if_token_present_a

$mockEvent = $this->createMock(GetResponseEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockEvent
->expects($this->once())
->method('getRequestType')
Expand Down Expand Up @@ -358,6 +389,10 @@ public function test_that_username_is_set_from_user_interface_if_token_present_a

$mockEvent = $this->createMock(GetResponseEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockEvent
->expects($this->once())
->method('getRequestType')
Expand Down Expand Up @@ -404,6 +439,10 @@ public function test_that_ip_is_set_from_request_stack()

$mockEvent = $this->createMock(GetResponseEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockRequest = $this->createMock(Request::class);

$mockRequest
Expand Down Expand Up @@ -438,6 +477,59 @@ public function test_that_ip_is_set_from_request_stack()
$listener->onKernelRequest($mockEvent);
}

public function test_that_it_sets_transaction_from_route()
{
$mockEvent = $this->createMock(GetResponseEvent::class);

$request = new Request();
$request->attributes->set('_route', 'my_route');
$mockEvent
->expects($this->once())
->method('getRequest')
->willReturn($request);

$mockEvent
->method('getRequestType')
->willReturn(HttpKernelInterface::SUB_REQUEST);

$mockTransactionStack = $this->createMock(\Raven_TransactionStack::class);
$this->mockSentryClient->transaction = $mockTransactionStack;

$mockTransactionStack
->expects($this->once())
->method('push')
->with('my_route');

$this->containerBuilder->compile();
$listener = $this->getListener();
$listener->onKernelRequest($mockEvent);
}

public function test_that_it_pops_transaction_from_route()
{
$mockEvent = $this->createMock(FinishRequestEvent::class);

$request = new Request();
$request->attributes->set('_route', 'my_route');
$mockEvent
->expects($this->once())
->method('getRequest')
->willReturn($request);

$mockTransactionStack = $this->createMock(\Raven_TransactionStack::class);
$this->mockSentryClient->transaction = $mockTransactionStack;

$mockTransactionStack
->expects($this->once())
->method('pop')
->with('my_route');

$this->containerBuilder->compile();
/** @var ExceptionListener $listener */
$listener = $this->getListener();
$listener->onFinishRequest($mockEvent);
}

public function test_regression_with_unauthenticated_user_token_PR_78()
{
$mockToken = $this->createMock(TokenInterface::class);
Expand All @@ -447,6 +539,10 @@ public function test_regression_with_unauthenticated_user_token_PR_78()

$mockEvent = $this->createMock(GetResponseEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockEvent
->expects($this->once())
->method('getRequestType')
Expand All @@ -465,6 +561,10 @@ public function test_that_it_does_not_report_http_exception_if_included_in_captu
{
$mockEvent = $this->createMock(GetResponseForExceptionEvent::class);

$mockEvent
->method('getRequest')
->willReturn(new Request());

$mockEvent
->expects($this->once())
->method('getException')
Expand Down Expand Up @@ -510,6 +610,30 @@ public function test_that_it_captures_exception()
$listener->onKernelException($mockEvent);
}

public function test_that_it_sets_transaction_from_command()
{
$mockEvent = $this->createMock(ConsoleCommandEvent::class);

$command = new Command('my_command');
$mockEvent
->expects($this->once())
->method('getCommand')
->willReturn($command);

$mockTransactionStack = $this->createMock(\Raven_TransactionStack::class);
$this->mockSentryClient->transaction = $mockTransactionStack;

$mockTransactionStack
->expects($this->once())
->method('push')
->with('my_command');

$this->containerBuilder->compile();
/** @var ExceptionListener $listener */
$listener = $this->getListener();
$listener->onConsoleCommand($mockEvent);
}

/**
* @dataProvider mockCommandProvider
*/
Expand Down Expand Up @@ -593,6 +717,30 @@ public function test_that_it_captures_console_error(?Command $mockCommand, strin
$listener->onConsoleError($event);
}

public function test_that_it_pops_transaction_from_command()
{
$mockEvent = $this->createMock(ConsoleTerminateEvent::class);

$command = new Command('my_command');
$mockEvent
->expects($this->once())
->method('getCommand')
->willReturn($command);

$mockTransactionStack = $this->createMock(\Raven_TransactionStack::class);
$this->mockSentryClient->transaction = $mockTransactionStack;

$mockTransactionStack
->expects($this->once())
->method('pop')
->with('my_command');

$this->containerBuilder->compile();
/** @var ExceptionListener $listener */
$listener = $this->getListener();
$listener->onFinishCommand($mockEvent);
}

public function mockCommandProvider()
{
$mockCommand = $this->createMock(Command::class);
Expand Down