app/Customize/EventListener/SentrySubscriber.php line 50

Open in your IDE?
  1. <?php
  2. namespace Customize\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  7. class SentrySubscriber implements EventSubscriberInterface
  8. {
  9.     const TIME_MAX_LOAD_PAGE 5// default load page 5s
  10.     private $startTime;
  11.     private $transaction;
  12.     /***
  13.      * @return string[]
  14.      */
  15.     public static function getSubscribedEvents()
  16.     {
  17.         return [
  18.             KernelEvents::CONTROLLER => 'onControllerStart',
  19.             KernelEvents::TERMINATE => 'onControllerEnd'
  20.         ];
  21.     }
  22.     /***
  23.      * @param ControllerEvent $event
  24.      * @return void
  25.      */
  26.     public function onControllerStart(ControllerEvent $event)
  27.     {
  28.         $this->startTime microtime(true);
  29.         $path $event->getRequest()->getPathInfo();
  30.         if (strpos($path'_wdt') !== false) {
  31.             return;
  32.         }
  33.         $transactionContext = new \Sentry\Tracing\TransactionContext();
  34.         $transactionContext->setName($path);
  35.         $transactionContext->setOp('lm_load_page');
  36.         $this->transaction \Sentry\startTransaction($transactionContext);
  37.     }
  38.     /***
  39.      * @param TerminateEvent $event
  40.      * @return void
  41.      */
  42.     public function onControllerEnd(TerminateEvent $event)
  43.     {
  44.         if (empty($this->transaction) || empty($this->startTime)) return null;
  45.         $endTime microtime(true);
  46.         $loadTime $endTime $this->startTime;
  47.         if ($loadTime >= self::TIME_MAX_LOAD_PAGE) {
  48.             $this->transaction->finish();
  49.         }
  50.     }
  51. }