<?php 
 
namespace Customize\EventListener; 
 
use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use Symfony\Component\HttpKernel\Event\ControllerEvent; 
use Symfony\Component\HttpKernel\KernelEvents; 
use Symfony\Component\HttpKernel\Event\TerminateEvent; 
 
 
class SentrySubscriber implements EventSubscriberInterface 
{ 
    const TIME_MAX_LOAD_PAGE = 5; // default load page 5s 
    private $startTime; 
    private $transaction; 
 
 
    /*** 
     * @return string[] 
     */ 
    public static function getSubscribedEvents() 
    { 
        return [ 
            KernelEvents::CONTROLLER => 'onControllerStart', 
            KernelEvents::TERMINATE => 'onControllerEnd' 
        ]; 
    } 
 
    /*** 
     * @param ControllerEvent $event 
     * @return void 
     */ 
    public function onControllerStart(ControllerEvent $event) 
    { 
        $this->startTime = microtime(true); 
        $path = $event->getRequest()->getPathInfo(); 
        if (strpos($path, '_wdt') !== false) { 
            return; 
        } 
        $transactionContext = new \Sentry\Tracing\TransactionContext(); 
        $transactionContext->setName($path); 
        $transactionContext->setOp('lm_load_page'); 
        $this->transaction = \Sentry\startTransaction($transactionContext); 
    } 
 
    /*** 
     * @param TerminateEvent $event 
     * @return void 
     */ 
    public function onControllerEnd(TerminateEvent $event) 
    { 
        if (empty($this->transaction) || empty($this->startTime)) return null; 
 
        $endTime = microtime(true); 
        $loadTime = $endTime - $this->startTime; 
        if ($loadTime >= self::TIME_MAX_LOAD_PAGE) { 
            $this->transaction->finish(); 
        } 
    } 
}