Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,11 @@ parameters:
count: 1
path: test/EventListener/MessengerListenerTest.php

-
message: "#^Property Sentry\\\\SentryBundle\\\\Test\\\\EventListener\\\\MessengerListenerTest\\:\\:\\$currentHub 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
2 changes: 1 addition & 1 deletion src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private function configureMessengerListener(ContainerBuilder $container, array $
return;
}

$container->getDefinition(MessengerListener::class)->setArgument(1, $processedConfiguration['capture_soft_fails']);
$container->getDefinition(MessengerListener::class)->setArgument(0, $processedConfiguration['capture_soft_fails']);
}

/**
Expand Down
25 changes: 12 additions & 13 deletions src/EventListener/MessengerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,23 @@

namespace Sentry\SentryBundle\EventListener;

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

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

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

/**
* @param FlushableClientInterface $client
* @param bool $captureSoftFails
* @param bool $captureSoftFails
*/
public function __construct(FlushableClientInterface $client, bool $captureSoftFails = true)
public function __construct(bool $captureSoftFails = true)
{
$this->client = $client;
$this->captureSoftFails = $captureSoftFails;
}

Expand All @@ -46,8 +39,11 @@ public function onWorkerMessageFailed(WorkerMessageFailedEvent $event): void
$error = $error->getPrevious();
}

$this->client->captureException($error);
$this->client->flush();
$hub = SentrySdk::getCurrentHub();
$hub->captureException($error);
if (method_exists($hub->getClient(), 'flush')) {
$hub->getClient()->flush();
}
}

/**
Expand All @@ -57,6 +53,9 @@ public function onWorkerMessageHandled(WorkerMessageHandledEvent $event): void
{
// Flush normally happens at shutdown... which only happens in the worker if it is run with a lifecycle limit
// such as --time=X or --limit=Y. Flush immediately in a background worker.
$this->client->flush();
$hub = SentrySdk::getCurrentHub();
if (method_exists($hub->getClient(), 'flush')) {
$hub->getClient()->flush();
}
}
}
1 change: 0 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
</service>

<service id="Sentry\SentryBundle\EventListener\MessengerListener" class="Sentry\SentryBundle\EventListener\MessengerListener" public="false">
<argument type="service" id="Sentry\FlushableClientInterface" />
<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
28 changes: 17 additions & 11 deletions test/EventListener/MessengerListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Sentry\FlushableClientInterface;
use Sentry\SentryBundle\EventListener\MessengerListener;
use Sentry\SentryBundle\Test\BaseTestCase;
use Sentry\SentrySdk;
use Sentry\State\HubInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
Expand All @@ -14,12 +16,16 @@
class MessengerListenerTest extends BaseTestCase
{
private $client;
private $currentHub;

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

$this->client = $this->prophesize(FlushableClientInterface::class);
$this->currentHub = $this->prophesize(HubInterface::class);
$this->currentHub->getClient()->willReturn($this->client);
SentrySdk::setCurrentHub($this->currentHub->reveal());
}

public function testSoftFailsAreRecorded(): void
Expand All @@ -30,10 +36,10 @@ public function testSoftFailsAreRecorded(): void

$error = new \RuntimeException();

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

$listener = new MessengerListener($this->client->reveal(), true);
$listener = new MessengerListener(true);
$message = (object) ['foo' => 'bar'];
$envelope = Envelope::wrap($message);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, true);
Expand All @@ -49,10 +55,10 @@ public function testHardFailsAreRecorded(): void

$error = new \RuntimeException();

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

$listener = new MessengerListener($this->client->reveal(), true);
$listener = new MessengerListener(true);
$message = (object) ['foo' => 'bar'];
$envelope = Envelope::wrap($message);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, false);
Expand All @@ -68,10 +74,10 @@ public function testSoftFailsAreNotRecorded(): void

$error = new \RuntimeException();

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

$listener = new MessengerListener($this->client->reveal(), false);
$listener = new MessengerListener(false);
$message = (object) ['foo' => 'bar'];
$envelope = Envelope::wrap($message);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, true);
Expand All @@ -87,10 +93,10 @@ public function testHardFailsAreRecordedWithCaptureSoftDisabled(): void

$error = new \RuntimeException();

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

$listener = new MessengerListener($this->client->reveal(), false);
$listener = new MessengerListener(false);
$message = (object) ['foo' => 'bar'];
$envelope = Envelope::wrap($message);
$event = $this->getMessageFailedEvent($envelope, 'receiver', $error, false);
Expand All @@ -111,10 +117,10 @@ public function testHandlerFailedExceptionIsUnwrapped(): void

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

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

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

Expand All @@ -125,7 +131,7 @@ public function testClientIsFlushedWhenMessageHandled(): void
}

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

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