From ebb6e26cd4694846392cf7280a286da873378ef0 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Fri, 12 Apr 2024 12:45:36 +0200 Subject: [PATCH] Move console integration to a feature class --- src/Sentry/Laravel/EventHandler.php | 75 ---------------- .../Laravel/Features/ConsoleIntegration.php | 89 +++++++++++++++++++ src/Sentry/Laravel/ServiceProvider.php | 1 + .../ConsoleIntegrationTest.php} | 2 +- 4 files changed, 91 insertions(+), 76 deletions(-) create mode 100644 src/Sentry/Laravel/Features/ConsoleIntegration.php rename test/Sentry/{EventHandler/ConsoleEventsTest.php => Features/ConsoleIntegrationTest.php} (97%) diff --git a/src/Sentry/Laravel/EventHandler.php b/src/Sentry/Laravel/EventHandler.php index 7a9eddaa..1596042f 100644 --- a/src/Sentry/Laravel/EventHandler.php +++ b/src/Sentry/Laravel/EventHandler.php @@ -4,7 +4,6 @@ use Exception; use Illuminate\Auth\Events as AuthEvents; -use Illuminate\Console\Events as ConsoleEvents; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Container\Container; @@ -21,8 +20,6 @@ use Sentry\Laravel\Tracing\Middleware; use Sentry\SentrySdk; use Sentry\State\Scope; -use Symfony\Component\Console\Input\ArgvInput; -use Symfony\Component\Console\Input\InputInterface; class EventHandler { @@ -35,8 +32,6 @@ class EventHandler LogEvents\MessageLogged::class => 'messageLogged', RoutingEvents\RouteMatched::class => 'routeMatched', DatabaseEvents\QueryExecuted::class => 'queryExecuted', - ConsoleEvents\CommandStarting::class => 'commandStarting', - ConsoleEvents\CommandFinished::class => 'commandFinished', ]; /** @@ -96,13 +91,6 @@ class EventHandler */ private $recordLaravelLogs; - /** - * Indicates if we should add command info to the breadcrumbs. - * - * @var bool - */ - private $recordCommandInfo; - /** * Indicates if we should add tick info to the breadcrumbs. * @@ -137,7 +125,6 @@ public function __construct(Container $container, array $config) $this->recordSqlQueries = ($config['breadcrumbs.sql_queries'] ?? $config['breadcrumbs']['sql_queries'] ?? true) === true; $this->recordSqlBindings = ($config['breadcrumbs.sql_bindings'] ?? $config['breadcrumbs']['sql_bindings'] ?? false) === true; $this->recordLaravelLogs = ($config['breadcrumbs.logs'] ?? $config['breadcrumbs']['logs'] ?? true) === true; - $this->recordCommandInfo = ($config['breadcrumbs.command_info'] ?? $config['breadcrumbs']['command_info'] ?? true) === true; $this->recordOctaneTickInfo = ($config['breadcrumbs.octane_tick_info'] ?? $config['breadcrumbs']['octane_tick_info'] ?? true) === true; $this->recordOctaneTaskInfo = ($config['breadcrumbs.octane_task_info'] ?? $config['breadcrumbs']['octane_task_info'] ?? true) === true; } @@ -316,68 +303,6 @@ private function configureUserScopeFromModel($authUser): void }); } - protected function commandStartingHandler(ConsoleEvents\CommandStarting $event): void - { - if ($event->command) { - Integration::configureScope(static function (Scope $scope) use ($event): void { - $scope->setTag('command', $event->command); - }); - - if (!$this->recordCommandInfo) { - return; - } - - Integration::addBreadcrumb(new Breadcrumb( - Breadcrumb::LEVEL_INFO, - Breadcrumb::TYPE_DEFAULT, - 'artisan.command', - 'Starting Artisan command: ' . $event->command, - [ - 'input' => $this->extractConsoleCommandInput($event->input), - ] - )); - } - } - - protected function commandFinishedHandler(ConsoleEvents\CommandFinished $event): void - { - if ($this->recordCommandInfo) { - Integration::addBreadcrumb(new Breadcrumb( - Breadcrumb::LEVEL_INFO, - Breadcrumb::TYPE_DEFAULT, - 'artisan.command', - 'Finished Artisan command: ' . $event->command, - [ - 'exit' => $event->exitCode, - 'input' => $this->extractConsoleCommandInput($event->input), - ] - )); - } - - // Flush any and all events that were possibly generated by the command - Integration::flushEvents(); - - Integration::configureScope(static function (Scope $scope): void { - $scope->removeTag('command'); - }); - } - - /** - * Extract the command input arguments if possible. - * - * @param \Symfony\Component\Console\Input\InputInterface|null $input - * - * @return string|null - */ - private function extractConsoleCommandInput(?InputInterface $input): ?string - { - if ($input instanceof ArgvInput) { - return (string)$input; - } - - return null; - } - protected function octaneRequestReceivedHandler(Octane\RequestReceived $event): void { $this->prepareScopeForOctane(); diff --git a/src/Sentry/Laravel/Features/ConsoleIntegration.php b/src/Sentry/Laravel/Features/ConsoleIntegration.php new file mode 100644 index 00000000..a329ee41 --- /dev/null +++ b/src/Sentry/Laravel/Features/ConsoleIntegration.php @@ -0,0 +1,89 @@ +listen(ConsoleEvents\CommandStarting::class, [$this, 'commandStarting']); + $events->listen(ConsoleEvents\CommandFinished::class, [$this, 'commandFinished']); + } + + public function commandStarting(ConsoleEvents\CommandStarting $event): void + { + if (!$event->command) { + return; + } + + Integration::configureScope(static function (Scope $scope) use ($event): void { + $scope->setTag('command', $event->command); + }); + + if ($this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY)) { + Integration::addBreadcrumb(new Breadcrumb( + Breadcrumb::LEVEL_INFO, + Breadcrumb::TYPE_DEFAULT, + 'artisan.command', + 'Starting Artisan command: ' . $event->command, + [ + 'input' => $this->extractConsoleCommandInput($event->input), + ] + )); + } + } + + public function commandFinished(ConsoleEvents\CommandFinished $event): void + { + if ($this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY)) { + Integration::addBreadcrumb(new Breadcrumb( + Breadcrumb::LEVEL_INFO, + Breadcrumb::TYPE_DEFAULT, + 'artisan.command', + 'Finished Artisan command: ' . $event->command, + [ + 'exit' => $event->exitCode, + 'input' => $this->extractConsoleCommandInput($event->input), + ] + )); + } + + // Flush any and all events that were possibly generated by the command + Integration::flushEvents(); + + Integration::configureScope(static function (Scope $scope): void { + $scope->removeTag('command'); + }); + } + + /** + * Extract the command input arguments if possible. + * + * @param \Symfony\Component\Console\Input\InputInterface|null $input + * + * @return string|null + */ + private function extractConsoleCommandInput(?InputInterface $input): ?string + { + if ($input instanceof ArgvInput) { + return (string)$input; + } + + return null; + } +} diff --git a/src/Sentry/Laravel/ServiceProvider.php b/src/Sentry/Laravel/ServiceProvider.php index d1eba711..aa7a1d56 100644 --- a/src/Sentry/Laravel/ServiceProvider.php +++ b/src/Sentry/Laravel/ServiceProvider.php @@ -65,6 +65,7 @@ class ServiceProvider extends BaseServiceProvider Features\LogIntegration::class, Features\CacheIntegration::class, Features\QueueIntegration::class, + Features\ConsoleIntegration::class, Features\Storage\Integration::class, Features\HttpClientIntegration::class, Features\FolioPackageIntegration::class, diff --git a/test/Sentry/EventHandler/ConsoleEventsTest.php b/test/Sentry/Features/ConsoleIntegrationTest.php similarity index 97% rename from test/Sentry/EventHandler/ConsoleEventsTest.php rename to test/Sentry/Features/ConsoleIntegrationTest.php index c5ad179c..1f265479 100644 --- a/test/Sentry/EventHandler/ConsoleEventsTest.php +++ b/test/Sentry/Features/ConsoleIntegrationTest.php @@ -7,7 +7,7 @@ use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\BufferedOutput; -class ConsoleEventsTest extends TestCase +class ConsoleIntegrationTest extends TestCase { public function testCommandBreadcrumbIsRecordedWhenEnabled(): void {