Skip to content

Commit

Permalink
Refactor the console scheduling integration (#884)
Browse files Browse the repository at this point in the history
* Rename the console scheduling integration

* Move console integration to a feature class
  • Loading branch information
stayallive committed Apr 17, 2024
1 parent e5f2154 commit 7604023
Show file tree
Hide file tree
Showing 7 changed files with 461 additions and 446 deletions.
75 changes: 0 additions & 75 deletions src/Sentry/Laravel/EventHandler.php
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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',
];

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 7604023

Please sign in to comment.