Skip to content

Commit 7460a08

Browse files
authored
Fix job event and scope handling (#351)
1 parent e81e4f7 commit 7460a08

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Add `send_default_pii` option by default to published config file (#340)
66
- Update `.gitattributes` to exclude more files from dist release (#341)
7+
- Fixed scope data in queue jobs being lost in some cases (#351)
78

89
## 1.7.1
910

src/Sentry/Laravel/EventHandler.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Illuminate\Queue\Events\JobProcessed;
1414
use Illuminate\Queue\Events\JobProcessing;
1515
use Illuminate\Queue\Events\WorkerStopping;
16+
use Illuminate\Queue\QueueManager;
1617
use Illuminate\Routing\Events\RouteMatched;
1718
use Illuminate\Routing\Route;
1819
use Illuminate\Support\Str;
@@ -98,6 +99,13 @@ class EventHandler
9899
*/
99100
private $recordQueueInfo;
100101

102+
/**
103+
* Indicates if we pushed a scope for the queue.
104+
*
105+
* @var bool
106+
*/
107+
private $pushedQueueScope = false;
108+
101109
/**
102110
* EventHandler constructor.
103111
*
@@ -135,9 +143,16 @@ public function subscribeAuthEvents()
135143

136144
/**
137145
* Attach all queue event handlers.
146+
*
147+
* @param \Illuminate\Queue\QueueManager $queue
138148
*/
139-
public function subscribeQueueEvents()
149+
public function subscribeQueueEvents(QueueManager $queue)
140150
{
151+
$queue->looping(function () {
152+
$this->cleanupScopeForQueuedJob();
153+
$this->afterQueuedJob();
154+
});
155+
141156
foreach (static::$queueEventHandlerMap as $eventName => $handler) {
142157
$this->events->listen($eventName, [$this, $handler]);
143158
}
@@ -365,7 +380,7 @@ protected function authenticatedHandler(Authenticated $event)
365380
*/
366381
protected function queueJobProcessingHandler(JobProcessing $event)
367382
{
368-
$this->beforeQueuedJob();
383+
$this->prepareScopeForQueuedJob();
369384

370385
if (!$this->recordQueueInfo) {
371386
return;
@@ -478,18 +493,34 @@ protected function commandFinishedHandler(CommandFinished $event)
478493
Integration::flushEvents();
479494
}
480495

481-
private function beforeQueuedJob()
496+
private function afterQueuedJob(): void
497+
{
498+
// Flush any and all events that were possibly generated by queue jobs
499+
Integration::flushEvents();
500+
}
501+
502+
private function prepareScopeForQueuedJob(): void
482503
{
483-
// When a job starts, we want to push a new scope
504+
$this->cleanupScopeForQueuedJob();
505+
484506
SentrySdk::getCurrentHub()->pushScope();
507+
508+
$this->pushedQueueScope = true;
509+
510+
// When a job starts, we want to make sure the scope is cleared of breadcrumbs
511+
SentrySdk::getCurrentHub()->configureScope(static function (Scope $scope) {
512+
$scope->clearBreadcrumbs();
513+
});
485514
}
486515

487-
private function afterQueuedJob()
516+
private function cleanupScopeForQueuedJob(): void
488517
{
489-
// Flush any and all events that were possibly generated by queue jobs
490-
Integration::flushEvents();
518+
if (!$this->pushedQueueScope) {
519+
return;
520+
}
491521

492-
// We have added a scope when the job started processing
493522
SentrySdk::getCurrentHub()->popScope();
523+
524+
$this->pushedQueueScope = false;
494525
}
495526
}

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ protected function bindEvents(): void
7575

7676
$handler->subscribe();
7777

78-
$handler->subscribeQueueEvents();
78+
if ($this->app->bound('queue')) {
79+
$handler->subscribeQueueEvents($this->app->queue);
80+
}
7981

8082
if (isset($userConfig['send_default_pii']) && $userConfig['send_default_pii'] !== false) {
8183
$handler->subscribeAuthEvents();

test/Sentry/SentryLaravelTestCase.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Sentry\Laravel\Tests;
44

5+
use ReflectionMethod;
56
use Sentry\Breadcrumb;
7+
use Sentry\State\Scope;
8+
use ReflectionProperty;
69
use Sentry\State\HubInterface;
710
use Sentry\Laravel\ServiceProvider;
811
use Orchestra\Testbench\TestCase as LaravelTestCase;
@@ -52,17 +55,21 @@ protected function getHubFromContainer(): HubInterface
5255
return $this->app->make('sentry');
5356
}
5457

55-
protected function getCurrentBreadcrumbs(): array
58+
protected function getCurrentScope(): Scope
5659
{
5760
$hub = $this->getHubFromContainer();
5861

59-
$method = new \ReflectionMethod($hub, 'getScope');
62+
$method = new ReflectionMethod($hub, 'getScope');
6063
$method->setAccessible(true);
6164

62-
/** @var \Sentry\State\Scope $scope */
63-
$scope = $method->invoke($hub);
65+
return $method->invoke($hub);
66+
}
67+
68+
protected function getCurrentBreadcrumbs(): array
69+
{
70+
$scope = $this->getCurrentScope();
6471

65-
$property = new \ReflectionProperty($scope, 'breadcrumbs');
72+
$property = new ReflectionProperty($scope, 'breadcrumbs');
6673
$property->setAccessible(true);
6774

6875
return $property->getValue($scope);

0 commit comments

Comments
 (0)