Skip to content

Refactor queue integration #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 1 addition & 91 deletions src/Sentry/Laravel/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Illuminate\Http\Client\Events as HttpClientEvents;
use Illuminate\Http\Request;
use Illuminate\Log\Events as LogEvents;
use Illuminate\Queue\Events as QueueEvents;
use Illuminate\Routing\Events as RoutingEvents;
use Laravel\Octane\Events as Octane;
use Laravel\Sanctum\Events as Sanctum;
Expand Down Expand Up @@ -55,18 +54,6 @@ class EventHandler
Sanctum\TokenAuthenticated::class => 'sanctumTokenAuthenticated', // Since Sanctum 2.13
];

/**
* Map queue event handlers to events.
*
* @var array
*/
protected static $queueEventHandlerMap = [
QueueEvents\JobProcessed::class => 'queueJobProcessed',
QueueEvents\JobProcessing::class => 'queueJobProcessing',
QueueEvents\WorkerStopping::class => 'queueWorkerStopping',
QueueEvents\JobExceptionOccurred::class => 'queueJobExceptionOccurred',
];

/**
* Map Octane event handlers to events.
*
Expand Down Expand Up @@ -114,13 +101,6 @@ class EventHandler
*/
private $recordLaravelLogs;

/**
* Indicates if we should add queue info to the breadcrumbs.
*
* @var bool
*/
private $recordQueueInfo;

/**
* Indicates if we should add command info to the breadcrumbs.
*
Expand Down Expand Up @@ -149,13 +129,6 @@ class EventHandler
*/
private $recordHttpClientRequests;

/**
* Indicates if we pushed a scope for the queue.
*
* @var int
*/
private $pushedQueueScopeCount = 0;

/**
* Indicates if we pushed a scope for Octane.
*
Expand All @@ -176,7 +149,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->recordQueueInfo = ($config['breadcrumbs.queue_info'] ?? $config['breadcrumbs']['queue_info'] ?? 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 @@ -204,7 +176,7 @@ public function subscribeAuthEvents(Dispatcher $dispatcher): void
}

/**
* Attach all queue event handlers.
* Attach all Octane event handlers.
*/
public function subscribeOctaneEvents(Dispatcher $dispatcher): void
{
Expand All @@ -213,16 +185,6 @@ public function subscribeOctaneEvents(Dispatcher $dispatcher): void
}
}

/**
* Attach all queue event handlers.
*/
public function subscribeQueueEvents(Dispatcher $dispatcher): void
{
foreach (static::$queueEventHandlerMap as $eventName => $handler) {
$dispatcher->listen($eventName, [$this, $handler]);
}
}

/**
* Pass through the event and capture any errors.
*
Expand Down Expand Up @@ -407,57 +369,6 @@ private function configureUserScopeFromModel($authUser): void
});
}

protected function queueJobProcessingHandler(QueueEvents\JobProcessing $event): void
{
$this->cleanupScopeForTaskWithinLongRunningProcessWhen($this->pushedQueueScopeCount > 0);

$this->prepareScopeForTaskWithinLongRunningProcess();

++$this->pushedQueueScopeCount;

if (!$this->recordQueueInfo) {
return;
}

$job = [
'job' => $event->job->getName(),
'queue' => $event->job->getQueue(),
'attempts' => $event->job->attempts(),
'connection' => $event->connectionName,
];

// Resolve name exists only from Laravel 5.3+
if (method_exists($event->job, 'resolveName')) {
$job['resolved'] = $event->job->resolveName();
}

Integration::addBreadcrumb(new Breadcrumb(
Breadcrumb::LEVEL_INFO,
Breadcrumb::TYPE_DEFAULT,
'queue.job',
'Processing queue job',
$job
));
}

protected function queueJobExceptionOccurredHandler(QueueEvents\JobExceptionOccurred $event): void
{
$this->afterTaskWithinLongRunningProcess();
}

protected function queueJobProcessedHandler(QueueEvents\JobProcessed $event): void
{
$this->cleanupScopeForTaskWithinLongRunningProcessWhen($this->pushedQueueScopeCount > 0);

$this->afterTaskWithinLongRunningProcess();
}

protected function queueWorkerStoppingHandler(QueueEvents\WorkerStopping $event): void
{
// Flush any and all events that were possibly generated by queue jobs
Integration::flushEvents();
}

protected function commandStartingHandler(ConsoleEvents\CommandStarting $event): void
{
if ($event->command) {
Expand Down Expand Up @@ -630,7 +541,6 @@ private function logLevelToBreadcrumbLevel(string $level): string
*/
private function afterTaskWithinLongRunningProcess(): void
{
// Flush any and all events that were possibly generated by queue jobs
Integration::flushEvents();
}

Expand Down
3 changes: 2 additions & 1 deletion src/Sentry/Laravel/Features/CacheIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class CacheIntegration extends Feature

public function isApplicable(): bool
{
return $this->isTracingFeatureEnabled('redis_commands') || $this->isBreadcrumbFeatureEnabled('cache');
return $this->isTracingFeatureEnabled('redis_commands')
|| $this->isBreadcrumbFeatureEnabled('cache');
}

public function setup(Dispatcher $events): void
Expand Down
3 changes: 2 additions & 1 deletion src/Sentry/Laravel/Features/LivewirePackageIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function isApplicable(): bool
return false;
}

return $this->isTracingFeatureEnabled(self::FEATURE_KEY) || $this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY);
return $this->isTracingFeatureEnabled(self::FEATURE_KEY)
|| $this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY);
}

public function setup(LivewireManager $livewireManager): void
Expand Down
Loading