vendor/sentry/sentry-symfony/src/EventListener/TracingConsoleListener.php line 50

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\SentryBundle\EventListener;
  4. use Sentry\State\HubInterface;
  5. use Sentry\Tracing\Span;
  6. use Sentry\Tracing\SpanContext;
  7. use Sentry\Tracing\Transaction;
  8. use Sentry\Tracing\TransactionContext;
  9. use Symfony\Component\Console\Command\Command;
  10. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  11. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  12. /**
  13.  * This listener either starts a {@see Transaction} or a child {@see Span} when
  14.  * a console command is executed to allow measuring the application performances.
  15.  */
  16. final class TracingConsoleListener
  17. {
  18.     /**
  19.      * @var HubInterface The current hub
  20.      */
  21.     private $hub;
  22.     /**
  23.      * @var string[] The list of commands for which distributed tracing must be skipped
  24.      */
  25.     private $excludedCommands;
  26.     /**
  27.      * Constructor.
  28.      *
  29.      * @param HubInterface $hub              The current hub
  30.      * @param string[]     $excludedCommands The list of commands for which distributed tracing must be skipped
  31.      */
  32.     public function __construct(HubInterface $hub, array $excludedCommands = [])
  33.     {
  34.         $this->hub $hub;
  35.         $this->excludedCommands $excludedCommands;
  36.     }
  37.     /**
  38.      * Handles the execution of a console command by starting a new {@see Transaction}
  39.      * if it doesn't exists, or a child {@see Span} if it does.
  40.      *
  41.      * @param ConsoleCommandEvent $event The event
  42.      */
  43.     public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
  44.     {
  45.         $command $event->getCommand();
  46.         if ($this->isCommandExcluded($command)) {
  47.             return;
  48.         }
  49.         $currentSpan $this->hub->getSpan();
  50.         if (null === $currentSpan) {
  51.             $transactionContext = new TransactionContext();
  52.             $transactionContext->setOp('console.command');
  53.             $transactionContext->setName($this->getSpanName($command));
  54.             $span $this->hub->startTransaction($transactionContext);
  55.         } else {
  56.             $spanContext = new SpanContext();
  57.             $spanContext->setOp('console.command');
  58.             $spanContext->setDescription($this->getSpanName($command));
  59.             $span $currentSpan->startChild($spanContext);
  60.         }
  61.         $this->hub->setSpan($span);
  62.     }
  63.     /**
  64.      * Handles the termination of a console command by stopping the active {@see Span}
  65.      * or {@see Transaction}.
  66.      *
  67.      * @param ConsoleTerminateEvent $event The event
  68.      */
  69.     public function handleConsoleTerminateEvent(ConsoleTerminateEvent $event): void
  70.     {
  71.         if ($this->isCommandExcluded($event->getCommand())) {
  72.             return;
  73.         }
  74.         $span $this->hub->getSpan();
  75.         if (null !== $span) {
  76.             $span->finish();
  77.         }
  78.     }
  79.     private function getSpanName(?Command $command): string
  80.     {
  81.         if (null === $command || null === $command->getName()) {
  82.             return '<unnamed command>';
  83.         }
  84.         return $command->getName();
  85.     }
  86.     private function isCommandExcluded(?Command $command): bool
  87.     {
  88.         if (null === $command) {
  89.             return true;
  90.         }
  91.         return \in_array($command->getName(), $this->excludedCommandstrue);
  92.     }
  93. }