Skip to content

Commit 0d5d9a0

Browse files
authored
Cleanup event handlers (#592)
1 parent 4bc01ab commit 0d5d9a0

12 files changed

+276
-212
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
- Drop support for Laravel 5.x (#581)
1313
- Remove `Sentry\Integration::extractNameForRoute()`, it's alternative `Sentry\Integration::extractNameAndSourceForRoute()` is marked as `@internal` (#580)
1414
- Remove extracting route name or controller for transaction names (#583). This unifies the transaction names to a more concise format.
15+
- Remove internal `Sentry\Integration::currentTracingSpan()`, use `SentrySdk::getCurrentHub()->getSpan()` if you were using this internal method. (#592)
1516

1617
**Other changes**
1718

1819
- Add support for Dynamic Sampling (#572)
1920
- Set the tracing transaction name on the `Illuminate\Routing\Events\RouteMatched` instead of at the end of the request (#580)
2021
- Add tracing span for Laravel HTTP client requests (#585)
2122
- Simplify Sentry meta tag retrieval, by adding `Sentry\Laravel\Integration::sentryMeta()` (#586)
23+
- Fix tracing with nested queue jobs (mostly when running jobs in the `sync` driver) (#592)
24+
- Add a `http.route` span, this span indicates the time that is spent inside a controller method or route closure (#593)
25+
- Add a `db.transaction` span, this span indicates the time that is spent inside a database transaction (#594)
2226

2327
## 2.14.2
2428

src/Sentry/Laravel/EventHandler.php

Lines changed: 19 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ class EventHandler
140140
/**
141141
* Indicates if we pushed a scope for the queue.
142142
*
143-
* @var bool
143+
* @var int
144144
*/
145-
private $pushedQueueScope = false;
145+
private $pushedQueueScopeCount = 0;
146146

147147
/**
148148
* Indicates if we pushed a scope for Octane.
@@ -173,76 +173,40 @@ public function __construct(Container $container, array $config)
173173
/**
174174
* Attach all event handlers.
175175
*/
176-
public function subscribe(): void
176+
public function subscribe(Dispatcher $dispatcher): void
177177
{
178-
/** @var \Illuminate\Contracts\Events\Dispatcher $dispatcher */
179-
try {
180-
$dispatcher = $this->container->make(Dispatcher::class);
181-
182-
foreach (static::$eventHandlerMap as $eventName => $handler) {
183-
$dispatcher->listen($eventName, [$this, $handler]);
184-
}
185-
} catch (BindingResolutionException $e) {
186-
// If we cannot resolve the event dispatcher we also cannot listen to events
178+
foreach (static::$eventHandlerMap as $eventName => $handler) {
179+
$dispatcher->listen($eventName, [$this, $handler]);
187180
}
188181
}
189182

190183
/**
191184
* Attach all authentication event handlers.
192185
*/
193-
public function subscribeAuthEvents(): void
186+
public function subscribeAuthEvents(Dispatcher $dispatcher): void
194187
{
195-
/** @var \Illuminate\Contracts\Events\Dispatcher $dispatcher */
196-
try {
197-
$dispatcher = $this->container->make(Dispatcher::class);
198-
199-
foreach (static::$authEventHandlerMap as $eventName => $handler) {
200-
$dispatcher->listen($eventName, [$this, $handler]);
201-
}
202-
} catch (BindingResolutionException $e) {
203-
// If we cannot resolve the event dispatcher we also cannot listen to events
188+
foreach (static::$authEventHandlerMap as $eventName => $handler) {
189+
$dispatcher->listen($eventName, [$this, $handler]);
204190
}
205191
}
206192

207193
/**
208194
* Attach all queue event handlers.
209195
*/
210-
public function subscribeOctaneEvents(): void
196+
public function subscribeOctaneEvents(Dispatcher $dispatcher): void
211197
{
212-
/** @var \Illuminate\Contracts\Events\Dispatcher $dispatcher */
213-
try {
214-
$dispatcher = $this->container->make(Dispatcher::class);
215-
216-
foreach (static::$octaneEventHandlerMap as $eventName => $handler) {
217-
$dispatcher->listen($eventName, [$this, $handler]);
218-
}
219-
} catch (BindingResolutionException $e) {
220-
// If we cannot resolve the event dispatcher we also cannot listen to events
198+
foreach (static::$octaneEventHandlerMap as $eventName => $handler) {
199+
$dispatcher->listen($eventName, [$this, $handler]);
221200
}
222201
}
223202

224203
/**
225204
* Attach all queue event handlers.
226-
*
227-
* @param \Illuminate\Queue\QueueManager $queue
228205
*/
229-
public function subscribeQueueEvents(QueueManager $queue): void
206+
public function subscribeQueueEvents(Dispatcher $dispatcher): void
230207
{
231-
$queue->looping(function () {
232-
$this->cleanupScopeForTaskWithinLongRunningProcessWhen($this->pushedQueueScope);
233-
234-
$this->pushedQueueScope = false;
235-
});
236-
237-
/** @var \Illuminate\Contracts\Events\Dispatcher $dispatcher */
238-
try {
239-
$dispatcher = $this->container->make(Dispatcher::class);
240-
241-
foreach (static::$queueEventHandlerMap as $eventName => $handler) {
242-
$dispatcher->listen($eventName, [$this, $handler]);
243-
}
244-
} catch (BindingResolutionException $e) {
245-
// If we cannot resolve the event dispatcher we also cannot listen to events
208+
foreach (static::$queueEventHandlerMap as $eventName => $handler) {
209+
$dispatcher->listen($eventName, [$this, $handler]);
246210
}
247211
}
248212

@@ -382,11 +346,9 @@ private function configureUserScopeFromModel($authUser): void
382346

383347
protected function queueJobProcessingHandler(QueueEvents\JobProcessing $event): void
384348
{
385-
$this->cleanupScopeForTaskWithinLongRunningProcessWhen($this->pushedQueueScope);
386-
387349
$this->prepareScopeForTaskWithinLongRunningProcess();
388350

389-
$this->pushedQueueScope = true;
351+
++$this->pushedQueueScopeCount;
390352

391353
if (!$this->recordQueueInfo) {
392354
return;
@@ -415,11 +377,15 @@ protected function queueJobProcessingHandler(QueueEvents\JobProcessing $event):
415377

416378
protected function queueJobExceptionOccurredHandler(QueueEvents\JobExceptionOccurred $event): void
417379
{
380+
$this->cleanupScopeForTaskWithinLongRunningProcessWhen($this->pushedQueueScopeCount > 0);
381+
418382
$this->afterTaskWithinLongRunningProcess();
419383
}
420384

421385
protected function queueJobProcessedHandler(QueueEvents\JobProcessed $event): void
422386
{
387+
$this->cleanupScopeForTaskWithinLongRunningProcessWhen($this->pushedQueueScopeCount > 0);
388+
423389
$this->afterTaskWithinLongRunningProcess();
424390
}
425391

src/Sentry/Laravel/Integration.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
namespace Sentry\Laravel;
44

55
use Illuminate\Routing\Route;
6-
use Illuminate\Support\Str;
76
use Sentry\SentrySdk;
8-
use Sentry\Tracing\Span;
9-
use Sentry\Tracing\Transaction;
107
use Sentry\Tracing\TransactionSource;
118
use function Sentry\addBreadcrumb;
129
use function Sentry\configureScope;
@@ -118,7 +115,7 @@ public static function extractNameAndSourceForRoute(Route $route): array
118115
{
119116
return [
120117
'/' . ltrim($route->uri(), '/'),
121-
TransactionSource::route()
118+
TransactionSource::route(),
122119
];
123120
}
124121

@@ -140,7 +137,7 @@ public static function sentryMeta(): string
140137
*/
141138
public static function sentryTracingMeta(): string
142139
{
143-
$span = self::currentTracingSpan();
140+
$span = SentrySdk::getCurrentHub()->getSpan();
144141

145142
if ($span === null) {
146143
return '';
@@ -157,36 +154,12 @@ public static function sentryTracingMeta(): string
157154
*/
158155
public static function sentryBaggageMeta(): string
159156
{
160-
$span = self::currentTracingSpan();
157+
$span = SentrySdk::getCurrentHub()->getSpan();
161158

162159
if ($span === null) {
163160
return '';
164161
}
165162

166163
return sprintf('<meta name="baggage" content="%s"/>', $span->toBaggage());
167164
}
168-
169-
/**
170-
* Get the current active tracing span from the scope.
171-
*
172-
* @return \Sentry\Tracing\Transaction|null
173-
*
174-
* @internal This is used internally as an easy way to retrieve the current active transaction.
175-
*/
176-
public static function currentTransaction(): ?Transaction
177-
{
178-
return SentrySdk::getCurrentHub()->getTransaction();
179-
}
180-
181-
/**
182-
* Get the current active tracing span from the scope.
183-
*
184-
* @return \Sentry\Tracing\Span|null
185-
*
186-
* @internal This is used internally as an easy way to retrieve the current active tracing span.
187-
*/
188-
public static function currentTracingSpan(): ?Span
189-
{
190-
return SentrySdk::getCurrentHub()->getSpan();
191-
}
192165
}

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sentry\Laravel;
44

5+
use Illuminate\Contracts\Container\BindingResolutionException;
6+
use Illuminate\Contracts\Events\Dispatcher;
57
use Illuminate\Contracts\Http\Kernel as HttpKernelInterface;
68
use Illuminate\Foundation\Application as Laravel;
79
use Illuminate\Foundation\Http\Kernel as HttpKernel;
@@ -95,18 +97,25 @@ protected function bindEvents(): void
9597

9698
$handler = new EventHandler($this->app, $userConfig);
9799

98-
$handler->subscribe();
100+
try {
101+
/** @var \Illuminate\Contracts\Events\Dispatcher $dispatcher */
102+
$dispatcher = $this->app->make(Dispatcher::class);
99103

100-
if ($this->app->bound('octane')) {
101-
$handler->subscribeOctaneEvents();
102-
}
104+
$handler->subscribe($dispatcher);
103105

104-
if ($this->app->bound('queue')) {
105-
$handler->subscribeQueueEvents($this->app->make('queue'));
106-
}
106+
if ($this->app->bound('octane')) {
107+
$handler->subscribeOctaneEvents($dispatcher);
108+
}
107109

108-
if (isset($userConfig['send_default_pii']) && $userConfig['send_default_pii'] !== false) {
109-
$handler->subscribeAuthEvents();
110+
if ($this->app->bound('queue')) {
111+
$handler->subscribeQueueEvents($dispatcher, $this->app->make('queue'));
112+
}
113+
114+
if (isset($userConfig['send_default_pii']) && $userConfig['send_default_pii'] !== false) {
115+
$handler->subscribeAuthEvents($dispatcher);
116+
}
117+
} catch (BindingResolutionException $e) {
118+
// If we cannot resolve the event dispatcher we also cannot listen to events
110119
}
111120
}
112121

0 commit comments

Comments
 (0)