vendor/sentry/sentry-symfony/src/EventListener/MessengerListener.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\SentryBundle\EventListener;
  4. use Sentry\Event;
  5. use Sentry\State\HubInterface;
  6. use Sentry\State\Scope;
  7. use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
  8. use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
  9. use Symfony\Component\Messenger\Exception\HandlerFailedException;
  10. use Symfony\Component\Messenger\Stamp\BusNameStamp;
  11. final class MessengerListener
  12. {
  13.     /**
  14.      * @var HubInterface The current hub
  15.      */
  16.     private $hub;
  17.     /**
  18.      * @var bool Whether to capture errors thrown while processing a message that
  19.      *           will be retried
  20.      */
  21.     private $captureSoftFails;
  22.     /**
  23.      * @param HubInterface $hub              The current hub
  24.      * @param bool         $captureSoftFails Whether to capture errors thrown
  25.      *                                       while processing a message that
  26.      *                                       will be retried
  27.      */
  28.     public function __construct(HubInterface $hubbool $captureSoftFails true)
  29.     {
  30.         $this->hub $hub;
  31.         $this->captureSoftFails $captureSoftFails;
  32.     }
  33.     /**
  34.      * This method is called for each message that failed to be handled.
  35.      *
  36.      * @param WorkerMessageFailedEvent $event The event
  37.      */
  38.     public function handleWorkerMessageFailedEvent(WorkerMessageFailedEvent $event): void
  39.     {
  40.         if (!$this->captureSoftFails && $event->willRetry()) {
  41.             return;
  42.         }
  43.         $this->hub->withScope(function (Scope $scope) use ($event): void {
  44.             $envelope $event->getEnvelope();
  45.             $exception $event->getThrowable();
  46.             $scope->setTag('messenger.receiver_name'$event->getReceiverName());
  47.             $scope->setTag('messenger.message_class', \get_class($envelope->getMessage()));
  48.             /** @var BusNameStamp|null $messageBusStamp */
  49.             $messageBusStamp $envelope->last(BusNameStamp::class);
  50.             if (null !== $messageBusStamp) {
  51.                 $scope->setTag('messenger.message_bus'$messageBusStamp->getBusName());
  52.             }
  53.             if ($exception instanceof HandlerFailedException) {
  54.                 foreach ($exception->getNestedExceptions() as $nestedException) {
  55.                     $this->hub->captureException($nestedException);
  56.                 }
  57.             } else {
  58.                 $this->hub->captureException($exception);
  59.             }
  60.         });
  61.         $this->flushClient();
  62.     }
  63.     /**
  64.      * This method is called for each handled message.
  65.      *
  66.      * @param WorkerMessageHandledEvent $event The event
  67.      */
  68.     public function handleWorkerMessageHandledEvent(WorkerMessageHandledEvent $event): void
  69.     {
  70.         // Flush normally happens at shutdown... which only happens in the worker if it is run with a lifecycle limit
  71.         // such as --time=X or --limit=Y. Flush immediately in a background worker.
  72.         $this->flushClient();
  73.     }
  74.     private function flushClient(): void
  75.     {
  76.         $client $this->hub->getClient();
  77.         if (null !== $client) {
  78.             $client->flush();
  79.         }
  80.     }
  81. }