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

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