vendor/ekino/newrelic-bundle/Listener/RequestListener.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Ekino New Relic bundle.
  5.  *
  6.  * (c) Ekino - Thomas Rabaix <thomas.rabaix@ekino.com>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Ekino\NewRelicBundle\Listener;
  12. use Ekino\NewRelicBundle\NewRelic\Config;
  13. use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
  14. use Ekino\NewRelicBundle\TransactionNamingStrategy\TransactionNamingStrategyInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  17. use Symfony\Component\HttpKernel\Event\RequestEvent;
  18. use Symfony\Component\HttpKernel\HttpKernelInterface;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. class RequestListener implements EventSubscriberInterface
  21. {
  22.     private $ignoredRoutes;
  23.     private $ignoredPaths;
  24.     private $config;
  25.     private $interactor;
  26.     private $transactionNamingStrategy;
  27.     private $symfonyCache;
  28.     public function __construct(
  29.         Config $config,
  30.         NewRelicInteractorInterface $interactor,
  31.         array $ignoreRoutes,
  32.         array $ignoredPaths,
  33.         TransactionNamingStrategyInterface $transactionNamingStrategy,
  34.         bool $symfonyCache false
  35.     ) {
  36.         $this->config $config;
  37.         $this->interactor $interactor;
  38.         $this->ignoredRoutes $ignoreRoutes;
  39.         $this->ignoredPaths $ignoredPaths;
  40.         $this->transactionNamingStrategy $transactionNamingStrategy;
  41.         $this->symfonyCache $symfonyCache;
  42.     }
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             KernelEvents::REQUEST => [
  47.                  ['setApplicationName'255],
  48.                  ['setIgnoreTransaction'31],
  49.                  ['setTransactionName', -10],
  50.             ],
  51.         ];
  52.     }
  53.     public function setApplicationName(KernelRequestEvent $event): void
  54.     {
  55.         if (!$this->isEventValid($event)) {
  56.             return;
  57.         }
  58.         $appName $this->config->getName();
  59.         if (!$appName) {
  60.             return;
  61.         }
  62.         if ($this->symfonyCache) {
  63.             $this->interactor->startTransaction($appName);
  64.         }
  65.         // Set application name if different from ini configuration
  66.         if ($appName !== ini_get('newrelic.appname')) {
  67.             $this->interactor->setApplicationName($appName$this->config->getLicenseKey(), $this->config->getXmit());
  68.         }
  69.     }
  70.     public function setTransactionName(KernelRequestEvent $event): void
  71.     {
  72.         if (!$this->isEventValid($event)) {
  73.             return;
  74.         }
  75.         $transactionName $this->transactionNamingStrategy->getTransactionName($event->getRequest());
  76.         $this->interactor->setTransactionName($transactionName);
  77.     }
  78.     public function setIgnoreTransaction(KernelRequestEvent $event): void
  79.     {
  80.         if (!$this->isEventValid($event)) {
  81.             return;
  82.         }
  83.         $request $event->getRequest();
  84.         if (\in_array($request->get('_route'), $this->ignoredRoutestrue)) {
  85.             $this->interactor->ignoreTransaction();
  86.         }
  87.         if (\in_array($request->getPathInfo(), $this->ignoredPathstrue)) {
  88.             $this->interactor->ignoreTransaction();
  89.         }
  90.     }
  91.     /**
  92.      * Make sure we should consider this event. Example: make sure it is a master request.
  93.      */
  94.     private function isEventValid(KernelRequestEvent $event): bool
  95.     {
  96.         return HttpKernelInterface::MASTER_REQUEST === $event->getRequestType();
  97.     }
  98. }
  99. if (!class_exists(KernelRequestEvent::class)) {
  100.     if (class_exists(RequestEvent::class)) {
  101.         class_alias(RequestEvent::class, KernelRequestEvent::class);
  102.     } else {
  103.         class_alias(GetResponseEvent::class, KernelRequestEvent::class);
  104.     }
  105. }