1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of Ekino New Relic bundle. |
||
7 | * |
||
8 | * (c) Ekino - Thomas Rabaix <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Ekino\NewRelicBundle\Listener; |
||
15 | |||
16 | use Ekino\NewRelicBundle\NewRelic\Config; |
||
17 | use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface; |
||
18 | use Symfony\Component\Console\ConsoleEvents; |
||
19 | use Symfony\Component\Console\Event\ConsoleCommandEvent; |
||
20 | use Symfony\Component\Console\Event\ConsoleErrorEvent; |
||
21 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
22 | |||
23 | class CommandListener implements EventSubscriberInterface |
||
24 | { |
||
25 | private $interactor; |
||
26 | private $config; |
||
27 | private $ignoredCommands; |
||
28 | |||
29 | public function __construct(Config $config, NewRelicInteractorInterface $interactor, array $ignoredCommands) |
||
30 | { |
||
31 | $this->config = $config; |
||
32 | $this->interactor = $interactor; |
||
33 | $this->ignoredCommands = $ignoredCommands; |
||
34 | } |
||
35 | |||
36 | public static function getSubscribedEvents(): array |
||
37 | { |
||
38 | return [ |
||
39 | ConsoleEvents::COMMAND => ['onConsoleCommand', 0], |
||
40 | ConsoleEvents::ERROR => ['onConsoleError', 0], |
||
41 | ]; |
||
42 | } |
||
43 | |||
44 | public function onConsoleCommand(ConsoleCommandEvent $event): void |
||
45 | { |
||
46 | $command = $event->getCommand(); |
||
47 | $input = $event->getInput(); |
||
48 | |||
49 | if ($this->config->getName()) { |
||
50 | $this->interactor->setApplicationName($this->config->getName(), $this->config->getLicenseKey(), $this->config->getXmit()); |
||
51 | } |
||
52 | $this->interactor->setTransactionName($command->getName()); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
53 | |||
54 | // Due to newrelic's extension implementation, the method `ignoreTransaction` must be called after `setApplicationName` |
||
55 | // see https://discuss.newrelic.com/t/newrelic-ignore-transaction-not-being-honored/5450/5 |
||
56 | if (\in_array($command->getName(), $this->ignoredCommands, true)) { |
||
57 | $this->interactor->ignoreTransaction(); |
||
58 | } |
||
59 | |||
60 | $this->interactor->enableBackgroundJob(); |
||
61 | |||
62 | // send parameters to New Relic |
||
63 | foreach ($input->getOptions() as $key => $value) { |
||
64 | $key = '--'.$key; |
||
65 | if (\is_array($value)) { |
||
66 | foreach ($value as $k => $v) { |
||
67 | $this->interactor->addCustomParameter($key.'['.$k.']', $v); |
||
68 | } |
||
69 | } else { |
||
70 | $this->interactor->addCustomParameter($key, $value); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | foreach ($input->getArguments() as $key => $value) { |
||
75 | if (\is_array($value)) { |
||
76 | foreach ($value as $k => $v) { |
||
77 | $this->interactor->addCustomParameter($key.'['.$k.']', $v); |
||
78 | } |
||
79 | } else { |
||
80 | $this->interactor->addCustomParameter($key, $value); |
||
81 | } |
||
82 | } |
||
83 | } |
||
84 | |||
85 | public function onConsoleError(ConsoleErrorEvent $event): void |
||
86 | { |
||
87 | $this->interactor->noticeThrowable($event->getError()); |
||
88 | } |
||
89 | } |
||
90 |