Skip to content
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

Allow use of current hub scope in Messenger Listener #339

Merged
merged 6 commits into from
May 7, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,6 @@ parameters:
count: 1
path: test/EventListener/ConsoleListenerTest.php

-
message: "#^Property Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\MessengerListenerTest\\:\\:\\$client has no typehint specified\\.$#"
count: 1
path: test/EventListener/MessengerListenerTest.php

-
message: "#^Class Symfony\\\\Component\\\\Messenger\\\\Event\\\\WorkerMessageHandledEvent constructor invoked with 3 parameters, 2 required\\.$#"
count: 1
Expand Down
22 changes: 13 additions & 9 deletions src/EventListener/MessengerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@
namespace Sentry\SentryBundle\EventListener;

use Sentry\FlushableClientInterface;
use Sentry\State\HubInterface;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
use Symfony\Component\Messenger\Exception\HandlerFailedException;

final class MessengerListener
{
/**
* @var FlushableClientInterface
* @var HubInterface
*/
private $client;
private $hub;

/**
* @var bool
*/
private $captureSoftFails;

/**
* @param FlushableClientInterface $client
* @param bool $captureSoftFails
* @param HubInterface $hub
* @param bool $captureSoftFails
*/
public function __construct(FlushableClientInterface $client, bool $captureSoftFails = true)
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
public function __construct(HubInterface $hub, bool $captureSoftFails = true)
{
$this->client = $client;
$this->hub = $hub;
$this->captureSoftFails = $captureSoftFails;
}

Expand All @@ -43,10 +44,10 @@ public function onWorkerMessageFailed(WorkerMessageFailedEvent $event): void

if ($error instanceof HandlerFailedException) {
foreach ($error->getNestedExceptions() as $nestedException) {
$this->client->captureException($nestedException);
$this->hub->captureException($nestedException);
}
} else {
$this->client->captureException($error);
$this->hub->captureException($error);
}

$this->flush();
Expand All @@ -64,6 +65,9 @@ public function onWorkerMessageHandled(WorkerMessageHandledEvent $event): void

private function flush(): void
{
$this->client->flush();
$client = $this->hub->getClient();
if ($client instanceof FlushableClientInterface) {
$client->flush();
}
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
</service>

<service id="Sentry\SentryBundle\EventListener\MessengerListener" class="Sentry\SentryBundle\EventListener\MessengerListener" public="false">
<argument type="service" id="Sentry\FlushableClientInterface" />
<argument type="service" id="Sentry\State\HubInterface" />
<tag name="kernel.event_listener" event="Symfony\Component\Messenger\Event\WorkerMessageFailedEvent" method="onWorkerMessageFailed" priority="%sentry.listener_priorities.worker_error%" />
<tag name="kernel.event_listener" event="Symfony\Component\Messenger\Event\WorkerMessageHandledEvent" method="onWorkerMessageHandled" priority="%sentry.listener_priorities.worker_error%" />
</service>
Expand Down
34 changes: 20 additions & 14 deletions test/EventListener/MessengerListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Sentry\FlushableClientInterface;
use Sentry\SentryBundle\EventListener\MessengerListener;
use Sentry\SentryBundle\Test\BaseTestCase;
use Sentry\State\HubInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
Expand All @@ -13,13 +14,18 @@

class MessengerListenerTest extends BaseTestCase
{
/** @var \Prophecy\Prophecy\ObjectProphecy|FlushableClientInterface */
private $client;
/** @var \Prophecy\Prophecy\ObjectProphecy|HubInterface */
private $hub;

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

$this->client = $this->prophesize(FlushableClientInterface::class);
$this->hub = $this->prophesize(HubInterface::class);
$this->hub->getClient()->willReturn($this->client);
}

public function testSoftFailsAreRecorded(): void
Expand All @@ -34,10 +40,10 @@ public function testSoftFailsAreRecorded(): void
$error = new \RuntimeException();
$wrappedError = new HandlerFailedException($envelope, [$error]);

$this->client->captureException($error)->shouldBeCalled();
$this->hub->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->client->reveal(), true);
$listener = new MessengerListener($this->hub->reveal(), true);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, true);

$listener->onWorkerMessageFailed($event);
Expand All @@ -54,10 +60,10 @@ public function testNonMessengerErrorsAreRecorded(): void

$error = new \RuntimeException();

$this->client->captureException($error)->shouldBeCalled();
$this->hub->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->client->reveal(), true);
$listener = new MessengerListener($this->hub->reveal(), true);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, false);

$listener->onWorkerMessageFailed($event);
Expand All @@ -75,10 +81,10 @@ public function testHardFailsAreRecorded(): void
$error = new \RuntimeException();
$wrappedError = new HandlerFailedException($envelope, [$error]);

$this->client->captureException($error)->shouldBeCalled();
$this->hub->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->client->reveal(), true);
$listener = new MessengerListener($this->hub->reveal(), true);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, false);

$listener->onWorkerMessageFailed($event);
Expand All @@ -96,10 +102,10 @@ public function testSoftFailsAreNotRecorded(): void
$error = new \RuntimeException();
$wrappedError = new HandlerFailedException($envelope, [$error]);

$this->client->captureException($error)->shouldNotBeCalled();
$this->hub->captureException($error)->shouldNotBeCalled();
$this->client->flush()->shouldNotBeCalled();

$listener = new MessengerListener($this->client->reveal(), false);
$listener = new MessengerListener($this->hub->reveal(), false);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, true);

$listener->onWorkerMessageFailed($event);
Expand All @@ -117,10 +123,10 @@ public function testHardFailsAreRecordedWithCaptureSoftDisabled(): void
$error = new \RuntimeException();
$wrappedError = new HandlerFailedException($envelope, [$error]);

$this->client->captureException($error)->shouldBeCalled();
$this->hub->captureException($error)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->client->reveal(), false);
$listener = new MessengerListener($this->hub->reveal(), false);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, false);

$listener->onWorkerMessageFailed($event);
Expand All @@ -140,11 +146,11 @@ public function testHandlerFailedExceptionIsUnwrapped(): void

$event = $this->getMessageFailedEvent($envelope, 'receiver', $wrappedError, false);

$this->client->captureException($error1)->shouldBeCalled();
$this->client->captureException($error2)->shouldBeCalled();
$this->hub->captureException($error1)->shouldBeCalled();
$this->hub->captureException($error2)->shouldBeCalled();
$this->client->flush()->shouldBeCalled();

$listener = new MessengerListener($this->client->reveal());
$listener = new MessengerListener($this->hub->reveal());
$listener->onWorkerMessageFailed($event);
}

Expand All @@ -155,7 +161,7 @@ public function testClientIsFlushedWhenMessageHandled(): void
}

$this->client->flush()->shouldBeCalled();
$listener = new MessengerListener($this->client->reveal());
$listener = new MessengerListener($this->hub->reveal());

$message = (object) ['foo' => 'bar'];
$envelope = Envelope::wrap($message);
Expand Down