-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement notifications tracing and breadcrumbs (#852)
* Implement notifications tracing and breadcrumbs * Disable view tracing for notification tests * 🙈 --------- Co-authored-by: Michi Hoffmann <cleptric@users.noreply.github.com>
- Loading branch information
1 parent
7edd171
commit 1b045d5
Showing
5 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Features; | ||
|
||
use Illuminate\Contracts\Events\Dispatcher; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Notifications\Events\NotificationSending; | ||
use Illuminate\Notifications\Events\NotificationSent; | ||
use Sentry\Breadcrumb; | ||
use Sentry\Laravel\Features\Concerns\TracksPushedScopesAndSpans; | ||
use Sentry\Laravel\Integration; | ||
use Sentry\SentrySdk; | ||
use Sentry\Tracing\SpanContext; | ||
use Sentry\Tracing\SpanStatus; | ||
|
||
class NotificationsIntegration extends Feature | ||
{ | ||
use TracksPushedScopesAndSpans; | ||
|
||
private const FEATURE_KEY = 'notifications'; | ||
|
||
public function isApplicable(): bool | ||
{ | ||
return $this->isTracingFeatureEnabled(self::FEATURE_KEY) | ||
|| $this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY); | ||
} | ||
|
||
public function onBoot(Dispatcher $events): void | ||
{ | ||
if ($this->isTracingFeatureEnabled(self::FEATURE_KEY)) { | ||
$events->listen(NotificationSending::class, [$this, 'handleNotificationSending']); | ||
} | ||
|
||
$events->listen(NotificationSent::class, [$this, 'handleNotificationSent']); | ||
} | ||
|
||
public function handleNotificationSending(NotificationSending $event): void | ||
{ | ||
$parentSpan = SentrySdk::getCurrentHub()->getSpan(); | ||
|
||
if ($parentSpan === null) { | ||
return; | ||
} | ||
|
||
$context = (new SpanContext) | ||
->setOp('notification.send') | ||
->setData([ | ||
'id' => $event->notification->id, | ||
'channel' => $event->channel, | ||
'notifiable' => $this->formatNotifiable($event->notifiable), | ||
'notification' => get_class($event->notification), | ||
]) | ||
->setDescription($event->channel); | ||
|
||
$this->pushSpan($parentSpan->startChild($context)); | ||
} | ||
|
||
public function handleNotificationSent(NotificationSent $event): void | ||
{ | ||
$this->finishSpanWithStatus(SpanStatus::ok()); | ||
|
||
if ($this->isBreadcrumbFeatureEnabled(self::FEATURE_KEY)) { | ||
Integration::addBreadcrumb(new Breadcrumb( | ||
Breadcrumb::LEVEL_INFO, | ||
Breadcrumb::TYPE_DEFAULT, | ||
'notification.sent', | ||
'Sent notification', | ||
[ | ||
'channel' => $event->channel, | ||
'notifiable' => $this->formatNotifiable($event->notifiable), | ||
'notification' => get_class($event->notification), | ||
] | ||
)); | ||
} | ||
} | ||
|
||
private function finishSpanWithStatus(SpanStatus $status): void | ||
{ | ||
$span = $this->maybePopSpan(); | ||
|
||
if ($span !== null) { | ||
$span->setStatus($status); | ||
$span->finish(); | ||
} | ||
} | ||
|
||
private function formatNotifiable(object $notifiable): string | ||
{ | ||
$notifiable = get_class($notifiable); | ||
|
||
if ($notifiable instanceof Model) { | ||
$notifiable .= "({$notifiable->getKey()})"; | ||
} | ||
|
||
return $notifiable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Sentry\Features; | ||
|
||
use Illuminate\Support\Facades\Mail; | ||
use Illuminate\Support\Facades\Notification; | ||
use Sentry\Laravel\Tests\TestCase; | ||
use Sentry\Tracing\Span; | ||
use Sentry\Tracing\SpanStatus; | ||
|
||
class NotificationsIntegrationTest extends TestCase | ||
{ | ||
protected $defaultSetupConfig = [ | ||
'sentry.tracing.views' => false, | ||
]; | ||
|
||
public function testSpanIsRecorded(): void | ||
{ | ||
$span = $this->sendNotificationAndRetrieveSpan(); | ||
|
||
$this->assertEquals('mail', $span->getDescription()); | ||
$this->assertEquals('mail', $span->getData()['channel']); | ||
$this->assertEquals('notification.send', $span->getOp()); | ||
$this->assertEquals(SpanStatus::ok(), $span->getStatus()); | ||
} | ||
|
||
public function testSpanIsNotRecordedWhenDisabled(): void | ||
{ | ||
$this->resetApplicationWithConfig([ | ||
'sentry.tracing.notifications.enabled' => false, | ||
]); | ||
|
||
$this->sendNotificationAndExpectNoSpan(); | ||
} | ||
|
||
public function testBreadcrumbIsRecorded(): void | ||
{ | ||
$this->sendTestNotification(); | ||
|
||
$this->assertCount(1, $this->getCurrentSentryBreadcrumbs()); | ||
|
||
$breadcrumb = $this->getLastSentryBreadcrumb(); | ||
|
||
$this->assertEquals('notification.sent', $breadcrumb->getCategory()); | ||
} | ||
|
||
public function testBreadcrumbIsNotRecordedWhenDisabled(): void | ||
{ | ||
$this->resetApplicationWithConfig([ | ||
'sentry.breadcrumbs.notifications.enabled' => false, | ||
]); | ||
|
||
$this->sendTestNotification(); | ||
|
||
$this->assertCount(0, $this->getCurrentSentryBreadcrumbs()); | ||
} | ||
|
||
private function sendTestNotification(): void | ||
{ | ||
// We fake the mail so that no actual email is sent but the notification is still sent with all it's events | ||
Mail::fake(); | ||
|
||
Notification::route('mail', 'sentry@example.com')->notifyNow(new NotificationsIntegrationTestNotification); | ||
} | ||
|
||
private function sendNotificationAndRetrieveSpan(): Span | ||
{ | ||
$transaction = $this->startTransaction(); | ||
|
||
$this->sendTestNotification(); | ||
|
||
$spans = $transaction->getSpanRecorder()->getSpans(); | ||
|
||
$this->assertCount(2, $spans); | ||
|
||
return $spans[1]; | ||
} | ||
|
||
private function sendNotificationAndExpectNoSpan(): void | ||
{ | ||
$transaction = $this->startTransaction(); | ||
|
||
$this->sendTestNotification(); | ||
|
||
$spans = $transaction->getSpanRecorder()->getSpans(); | ||
|
||
$this->assertCount(1, $spans); | ||
} | ||
} | ||
|
||
class NotificationsIntegrationTestNotification extends \Illuminate\Notifications\Notification | ||
{ | ||
public function via($notifiable) | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
public function toMail($notifiable) | ||
{ | ||
return new \Illuminate\Notifications\Messages\MailMessage; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters