Skip to content

Track subrequests with scopes #198

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 5 commits into from
Mar 5, 2019
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
2 changes: 2 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ public function getConfigTreeBuilder()
->children();
$listenerPriorities->scalarNode('request')
->defaultValue(1);
$listenerPriorities->scalarNode('sub_request')
->defaultValue(1);
$listenerPriorities->scalarNode('console')
->defaultValue(1);

Expand Down
38 changes: 38 additions & 0 deletions src/EventListener/SubRequestListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Sentry\SentryBundle\EventListener;

use Sentry\State\Hub;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

final class SubRequestListener
{
/**
* Pushes a new {@see Scope} for each SubRequest
*
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event): void
{
if ($event->isMasterRequest()) {
return;
}

Hub::getCurrent()->pushScope();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you not be adding the scope variables, such as route, in the same way as is done in https://github.com/getsentry/sentry-symfony/blob/master/src/EventListener/RequestListener.php#L88

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may depend on what we want to report. The main route is still the one of the master request, and I'm still thinking about if we need to add more data in that scope, or override the previous information.

IMHO, the subrequest info shouldn't override, but I'm not sure if it's in this bundle's scope to add that, or if I should leave it to the end user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I see what you mean, you still want to see the actual request,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. You'll still free to add tags, extra context or whatever to the scope, and if you do it inside the subrequest, it will be popped out and reverted at the end of it.

Does it make sense to you? Do you think it's a good approach?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, though like you say, would be nice to automatically add something about the subrequest, like $scope->setTag('route', $matchedRoute); is done for master, maybe just with sub_request_route or something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, for example at work we use subrequests and we add some information to the extra context such as:

  • current_request_url
  • current_request_method
  • master_request_url
  • master_request_method
  • is_master_request

This way we know if an event got generated from a request or a sub request

}

/**
* Pops a {@see Scope} for each finished SubRequest
*
* @param FinishRequestEvent $event
*/
public function onKernelFinishRequest(FinishRequestEvent $event): void
{
if ($event->isMasterRequest()) {
return;
}

Hub::getCurrent()->popScope();
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="%sentry.listener_priorities.request%" />
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController" priority="%sentry.listener_priorities.request%" />
</service>

<service id="Sentry\SentryBundle\EventListener\SubRequestListener" class="Sentry\SentryBundle\EventListener\SubRequestListener" public="false">
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="%sentry.listener_priorities.sub_request%" />
<tag name="kernel.event_listener" event="kernel.finish_request" method="onKernelFinishRequest" priority="%sentry.listener_priorities.sub_request%" />
</service>
</services>
</container>
1 change: 1 addition & 0 deletions test/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public function testConfigurationDefaults(): void
'dsn' => null,
'listener_priorities' => [
'request' => 1,
'sub_request' => 1,
'console' => 1,
],
'options' => [
Expand Down
22 changes: 16 additions & 6 deletions test/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
use Sentry\Integration\IntegrationInterface;
use Sentry\Options;
use Sentry\SentryBundle\DependencyInjection\SentryExtension;
use Sentry\SentryBundle\EventListener\ConsoleListener;
use Sentry\SentryBundle\EventListener\RequestListener;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -21,8 +19,6 @@

class SentryExtensionTest extends TestCase
{
private const REQUEST_LISTENER_TEST_PUBLIC_ALIAS = 'sentry.request_listener.public_alias';
private const CONSOLE_LISTENER_TEST_PUBLIC_ALIAS = 'sentry.console_listener.public_alias';
private const OPTIONS_TEST_PUBLIC_ALIAS = 'sentry.options.public_alias';

public function testDataProviderIsMappingTheRightNumberOfOptions(): void
Expand Down Expand Up @@ -62,9 +58,25 @@ public function testOptionsDefaultValues(): void
$this->assertSame(['var/cache', $vendorDir], $options->getInAppExcludedPaths());

$this->assertSame(1, $container->getParameter('sentry.listener_priorities.request'));
$this->assertSame(1, $container->getParameter('sentry.listener_priorities.sub_request'));
$this->assertSame(1, $container->getParameter('sentry.listener_priorities.console'));
}

public function testListenerPriorities(): void
{
$container = $this->getContainer([
'listener_priorities' => [
'request' => 123,
'sub_request' => 456,
'console' => 789,
],
]);

$this->assertSame(123, $container->getParameter('sentry.listener_priorities.request'));
$this->assertSame(456, $container->getParameter('sentry.listener_priorities.sub_request'));
$this->assertSame(789, $container->getParameter('sentry.listener_priorities.console'));
}

/**
* @dataProvider optionsValueProvider
*/
Expand Down Expand Up @@ -326,8 +338,6 @@ private function getContainer(array $configuration = []): Container
$containerBuilder->set('request_stack', $mockRequestStack);
$containerBuilder->set('event_dispatcher', $mockEventDispatcher);
$containerBuilder->setAlias(self::OPTIONS_TEST_PUBLIC_ALIAS, new Alias(Options::class, true));
$containerBuilder->setAlias(self::REQUEST_LISTENER_TEST_PUBLIC_ALIAS, new Alias(RequestListener::class, true));
$containerBuilder->setAlias(self::CONSOLE_LISTENER_TEST_PUBLIC_ALIAS, new Alias(ConsoleListener::class, true));

$beforeSend = new Definition('callable');
$beforeSend->setFactory([CallbackMock::class, 'createCallback']);
Expand Down
83 changes: 83 additions & 0 deletions test/EventListener/SubRequestListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Sentry\SentryBundle\Test\EventListener;

use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\EventListener\SubRequestListener;
use Sentry\State\Hub;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class SubRequestListenerTest extends TestCase
{
private $currentHub;

protected function setUp()
{
parent::setUp();

$this->currentHub = $this->prophesize(HubInterface::class);

Hub::setCurrent($this->currentHub->reveal());
}

public function testOnKernelRequestWithMasterRequest(): void
{
$listener = new SubRequestListener();

$subRequestEvent = $this->prophesize(GetResponseEvent::class);
$subRequestEvent->isMasterRequest()
->willReturn(true);

$this->currentHub->pushScope()
->shouldNotBeCalled();

$listener->onKernelRequest($subRequestEvent->reveal());
}

public function testOnKernelRequestWithSubRequest(): void
{
$listener = new SubRequestListener();

$subRequestEvent = $this->prophesize(GetResponseEvent::class);
$subRequestEvent->isMasterRequest()
->willReturn(false);

$this->currentHub->pushScope()
->shouldBeCalledTimes(1)
->willReturn(new Scope());

$listener->onKernelRequest($subRequestEvent->reveal());
}

public function testOnKernelFinishRequestWithMasterRequest(): void
{
$listener = new SubRequestListener();

$subRequestEvent = $this->prophesize(FinishRequestEvent::class);
$subRequestEvent->isMasterRequest()
->willReturn(true);

$this->currentHub->popScope()
->shouldNotBeCalled();

$listener->onKernelFinishRequest($subRequestEvent->reveal());
}

public function testOnKernelFinishRequestWithSubRequest(): void
{
$listener = new SubRequestListener();

$subRequestEvent = $this->prophesize(FinishRequestEvent::class);
$subRequestEvent->isMasterRequest()
->willReturn(false);

$this->currentHub->popScope()
->shouldBeCalledTimes(1)
->willReturn(true);

$listener->onKernelFinishRequest($subRequestEvent->reveal());
}
}
33 changes: 30 additions & 3 deletions test/SentryBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
use Sentry\SentryBundle\DependencyInjection\SentryExtension;
use Sentry\SentryBundle\EventListener\ConsoleListener;
use Sentry\SentryBundle\EventListener\RequestListener;
use Sentry\SentryBundle\EventListener\SubRequestListener;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelEvents;

class SentryBundleTest extends TestCase
{
Expand All @@ -21,7 +24,7 @@ public function testContainerHasConsoleListenerConfiguredCorrectly(): void
$expectedTag = [
'kernel.event_listener' => [
[
'event' => 'console.command',
'event' => ConsoleEvents::COMMAND,
'method' => 'onConsoleCommand',
'priority' => '%sentry.listener_priorities.console%',
],
Expand All @@ -40,12 +43,12 @@ public function testContainerHasRequestListenerConfiguredCorrectly(): void
$expectedTag = [
'kernel.event_listener' => [
[
'event' => 'kernel.request',
'event' => KernelEvents::REQUEST,
'method' => 'onKernelRequest',
'priority' => '%sentry.listener_priorities.request%',
],
[
'event' => 'kernel.controller',
'event' => KernelEvents::CONTROLLER,
'method' => 'onKernelController',
'priority' => '%sentry.listener_priorities.request%',
],
Expand All @@ -55,6 +58,30 @@ public function testContainerHasRequestListenerConfiguredCorrectly(): void
$this->assertSame($expectedTag, $consoleListener->getTags());
}

public function testContainerHasSubRequestListenerConfiguredCorrectly(): void
{
$container = $this->getContainer();

$consoleListener = $container->getDefinition(SubRequestListener::class);

$expectedTag = [
'kernel.event_listener' => [
[
'event' => KernelEvents::REQUEST,
'method' => 'onKernelRequest',
'priority' => '%sentry.listener_priorities.sub_request%',
],
[
'event' => KernelEvents::FINISH_REQUEST,
'method' => 'onKernelFinishRequest',
'priority' => '%sentry.listener_priorities.sub_request%',
],
],
];

$this->assertSame($expectedTag, $consoleListener->getTags());
}

private function getContainer(array $configuration = []): ContainerBuilder
{
$containerBuilder = new ContainerBuilder();
Expand Down