app/Service/EventSubscriber/ApmMetadataSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace Sq\Service\EventSubscriber;
  3. use Psr\Container\ContainerInterface;
  4. use Sq\Entity\Apm\TransactionInterface;
  5. use Sq\Event\Kernel\InitialiseContainerEvent;
  6. use Sq\Service\Apm\ApmInterface;
  7. use Sq\Service\Log\LogBuilder;
  8. use Sq\Service\Log\LogBuilderInterface;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ApmMetadataSubscriber implements EventSubscriberInterface
  12. {
  13.     private ApmInterface $apm;
  14.     private ContainerInterface $container;
  15.     private LogBuilderInterface $logBuilder;
  16.     private ParameterBagInterface $parameterBag;
  17.     public function __construct(ParameterBagInterface $parameterBagContainerInterface $containerApmInterface $apmLogBuilderInterface $logBuilder)
  18.     {
  19.         $this->apm $apm;
  20.         $this->container $container;
  21.         $this->logBuilder $logBuilder;
  22.         $this->parameterBag $parameterBag;
  23.     }
  24.     /** @inheritDoc */
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             InitialiseContainerEvent::class => ['onInitializeContainer'],
  29.         ];
  30.     }
  31.     /**
  32.      * @param \Sq\Event\Kernel\InitialiseContainerEvent $event
  33.      */
  34.     public function onInitializeContainer(InitialiseContainerEvent $event): void
  35.     {
  36.         if ($this->apm->isTracking())
  37.         {
  38.             $transaction $this->apm->getCurrentTransaction();
  39.             $this->addScriptId($transaction);
  40.             $transaction->addAttribute('instance_id'$this->parameterBag->get('instance.identifier'));
  41.             $transaction->addAttribute('app_name'$this->parameterBag->get('kernel.app_name'));
  42.             $transaction->addAttribute('app_type'$this->parameterBag->get('kernel.app_type'));
  43.         }
  44.     }
  45.     protected function addScriptId(TransactionInterface $transaction)
  46.     {
  47.         if ($this->logBuilder instanceof LogBuilder)
  48.         {
  49.             $transaction->addAttribute("script_id"$this->logBuilder->getProcessId());
  50.         }
  51.     }
  52. }