Skip to content

Commit 7729117

Browse files
authored
Refactor queue integration (#692)
1 parent 63fa831 commit 7729117

File tree

9 files changed

+253
-249
lines changed

9 files changed

+253
-249
lines changed

src/Sentry/Laravel/EventHandler.php

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Illuminate\Http\Client\Events as HttpClientEvents;
1515
use Illuminate\Http\Request;
1616
use Illuminate\Log\Events as LogEvents;
17-
use Illuminate\Queue\Events as QueueEvents;
1817
use Illuminate\Routing\Events as RoutingEvents;
1918
use Laravel\Octane\Events as Octane;
2019
use Laravel\Sanctum\Events as Sanctum;
@@ -55,18 +54,6 @@ class EventHandler
5554
Sanctum\TokenAuthenticated::class => 'sanctumTokenAuthenticated', // Since Sanctum 2.13
5655
];
5756

58-
/**
59-
* Map queue event handlers to events.
60-
*
61-
* @var array
62-
*/
63-
protected static $queueEventHandlerMap = [
64-
QueueEvents\JobProcessed::class => 'queueJobProcessed',
65-
QueueEvents\JobProcessing::class => 'queueJobProcessing',
66-
QueueEvents\WorkerStopping::class => 'queueWorkerStopping',
67-
QueueEvents\JobExceptionOccurred::class => 'queueJobExceptionOccurred',
68-
];
69-
7057
/**
7158
* Map Octane event handlers to events.
7259
*
@@ -114,13 +101,6 @@ class EventHandler
114101
*/
115102
private $recordLaravelLogs;
116103

117-
/**
118-
* Indicates if we should add queue info to the breadcrumbs.
119-
*
120-
* @var bool
121-
*/
122-
private $recordQueueInfo;
123-
124104
/**
125105
* Indicates if we should add command info to the breadcrumbs.
126106
*
@@ -149,13 +129,6 @@ class EventHandler
149129
*/
150130
private $recordHttpClientRequests;
151131

152-
/**
153-
* Indicates if we pushed a scope for the queue.
154-
*
155-
* @var int
156-
*/
157-
private $pushedQueueScopeCount = 0;
158-
159132
/**
160133
* Indicates if we pushed a scope for Octane.
161134
*
@@ -176,7 +149,6 @@ public function __construct(Container $container, array $config)
176149
$this->recordSqlQueries = ($config['breadcrumbs.sql_queries'] ?? $config['breadcrumbs']['sql_queries'] ?? true) === true;
177150
$this->recordSqlBindings = ($config['breadcrumbs.sql_bindings'] ?? $config['breadcrumbs']['sql_bindings'] ?? false) === true;
178151
$this->recordLaravelLogs = ($config['breadcrumbs.logs'] ?? $config['breadcrumbs']['logs'] ?? true) === true;
179-
$this->recordQueueInfo = ($config['breadcrumbs.queue_info'] ?? $config['breadcrumbs']['queue_info'] ?? true) === true;
180152
$this->recordCommandInfo = ($config['breadcrumbs.command_info'] ?? $config['breadcrumbs']['command_info'] ?? true) === true;
181153
$this->recordOctaneTickInfo = ($config['breadcrumbs.octane_tick_info'] ?? $config['breadcrumbs']['octane_tick_info'] ?? true) === true;
182154
$this->recordOctaneTaskInfo = ($config['breadcrumbs.octane_task_info'] ?? $config['breadcrumbs']['octane_task_info'] ?? true) === true;
@@ -204,7 +176,7 @@ public function subscribeAuthEvents(Dispatcher $dispatcher): void
204176
}
205177

206178
/**
207-
* Attach all queue event handlers.
179+
* Attach all Octane event handlers.
208180
*/
209181
public function subscribeOctaneEvents(Dispatcher $dispatcher): void
210182
{
@@ -213,16 +185,6 @@ public function subscribeOctaneEvents(Dispatcher $dispatcher): void
213185
}
214186
}
215187

216-
/**
217-
* Attach all queue event handlers.
218-
*/
219-
public function subscribeQueueEvents(Dispatcher $dispatcher): void
220-
{
221-
foreach (static::$queueEventHandlerMap as $eventName => $handler) {
222-
$dispatcher->listen($eventName, [$this, $handler]);
223-
}
224-
}
225-
226188
/**
227189
* Pass through the event and capture any errors.
228190
*
@@ -407,57 +369,6 @@ private function configureUserScopeFromModel($authUser): void
407369
});
408370
}
409371

410-
protected function queueJobProcessingHandler(QueueEvents\JobProcessing $event): void
411-
{
412-
$this->cleanupScopeForTaskWithinLongRunningProcessWhen($this->pushedQueueScopeCount > 0);
413-
414-
$this->prepareScopeForTaskWithinLongRunningProcess();
415-
416-
++$this->pushedQueueScopeCount;
417-
418-
if (!$this->recordQueueInfo) {
419-
return;
420-
}
421-
422-
$job = [
423-
'job' => $event->job->getName(),
424-
'queue' => $event->job->getQueue(),
425-
'attempts' => $event->job->attempts(),
426-
'connection' => $event->connectionName,
427-
];
428-
429-
// Resolve name exists only from Laravel 5.3+
430-
if (method_exists($event->job, 'resolveName')) {
431-
$job['resolved'] = $event->job->resolveName();
432-
}
433-
434-
Integration::addBreadcrumb(new Breadcrumb(
435-
Breadcrumb::LEVEL_INFO,
436-
Breadcrumb::TYPE_DEFAULT,
437-
'queue.job',
438-
'Processing queue job',
439-
$job
440-
));
441-
}
442-
443-
protected function queueJobExceptionOccurredHandler(QueueEvents\JobExceptionOccurred $event): void
444-
{
445-
$this->afterTaskWithinLongRunningProcess();
446-
}
447-
448-
protected function queueJobProcessedHandler(QueueEvents\JobProcessed $event): void
449-
{
450-
$this->cleanupScopeForTaskWithinLongRunningProcessWhen($this->pushedQueueScopeCount > 0);
451-
452-
$this->afterTaskWithinLongRunningProcess();
453-
}
454-
455-
protected function queueWorkerStoppingHandler(QueueEvents\WorkerStopping $event): void
456-
{
457-
// Flush any and all events that were possibly generated by queue jobs
458-
Integration::flushEvents();
459-
}
460-
461372
protected function commandStartingHandler(ConsoleEvents\CommandStarting $event): void
462373
{
463374
if ($event->command) {
@@ -630,7 +541,6 @@ private function logLevelToBreadcrumbLevel(string $level): string
630541
*/
631542
private function afterTaskWithinLongRunningProcess(): void
632543
{
633-
// Flush any and all events that were possibly generated by queue jobs
634544
Integration::flushEvents();
635545
}
636546

src/Sentry/Laravel/Features/CacheIntegration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class CacheIntegration extends Feature
1919

2020
public function isApplicable(): bool
2121
{
22-
return $this->isTracingFeatureEnabled('redis_commands') || $this->isBreadcrumbFeatureEnabled('cache');
22+
return $this->isTracingFeatureEnabled('redis_commands')
23+
|| $this->isBreadcrumbFeatureEnabled('cache');
2324
}
2425

2526
public function setup(Dispatcher $events): void

src/Sentry/Laravel/Features/LivewirePackageIntegration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public function isApplicable(): bool
2727
return false;
2828
}
2929

30-
return $this->isTracingFeatureEnabled(self::FEATURE_KEY) || $this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY);
30+
return $this->isTracingFeatureEnabled(self::FEATURE_KEY)
31+
|| $this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY);
3132
}
3233

3334
public function setup(LivewireManager $livewireManager): void

0 commit comments

Comments
 (0)