-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add current Laravel context to event (#869)
* Add context to events and transactions * Always set the context becuase the SDK ignores empty contexts anyway * Test empty context do not set empty contexts on the event
- Loading branch information
1 parent
2e77090
commit 6d56923
Showing
3 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/Sentry/Laravel/Integration/LaravelContextIntegration.php
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,38 @@ | ||
<?php | ||
|
||
namespace Sentry\Laravel\Integration; | ||
|
||
use Illuminate\Support\Facades\Context; | ||
use Sentry\Event; | ||
use Sentry\EventHint; | ||
use Sentry\EventType; | ||
use Sentry\Integration\IntegrationInterface; | ||
use Sentry\SentrySdk; | ||
use Sentry\State\Scope; | ||
|
||
class LaravelContextIntegration implements IntegrationInterface | ||
{ | ||
public function setupOnce(): void | ||
{ | ||
// Context was introduced in Laravel 11 so we need to check if we can use it otherwise we skip the event processor | ||
if (!class_exists(Context::class)) { | ||
return; | ||
} | ||
|
||
Scope::addGlobalEventProcessor(static function (Event $event, ?EventHint $hint = null): Event { | ||
$self = SentrySdk::getCurrentHub()->getIntegration(self::class); | ||
|
||
if (!$self instanceof self) { | ||
return $event; | ||
} | ||
|
||
if (!in_array($event->getType(), [EventType::event(), EventType::transaction()], true)) { | ||
return $event; | ||
} | ||
|
||
$event->setContext('laravel', Context::all()); | ||
|
||
return $event; | ||
}); | ||
} | ||
} |
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,96 @@ | ||
<?php | ||
|
||
namespace Sentry\Integration; | ||
|
||
use Exception; | ||
use Illuminate\Support\Facades\Context; | ||
use Sentry\EventType; | ||
use Sentry\Laravel\Integration\LaravelContextIntegration; | ||
use Sentry\Laravel\Tests\TestCase; | ||
use function Sentry\captureException; | ||
|
||
class LaravelContextIntegrationTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
if (!class_exists(Context::class)) { | ||
$this->markTestSkipped('Laravel introduced contexts in version 11.'); | ||
} | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function testLaravelContextIntegrationIsRegistered(): void | ||
{ | ||
$integration = $this->getSentryHubFromContainer()->getIntegration(LaravelContextIntegration::class); | ||
|
||
$this->assertInstanceOf(LaravelContextIntegration::class, $integration); | ||
} | ||
|
||
public function testExceptionIsCapturedWithLaravelContext(): void | ||
{ | ||
$this->setupTestContext(); | ||
|
||
captureException(new Exception('Context test')); | ||
|
||
$event = $this->getLastSentryEvent(); | ||
|
||
$this->assertNotNull($event); | ||
$this->assertEquals($event->getType(), EventType::event()); | ||
$this->assertContextIsCaptured($event->getContexts()); | ||
} | ||
|
||
public function testExceptionIsCapturedWithoutLaravelContextIfEmpty(): void | ||
{ | ||
captureException(new Exception('Context test')); | ||
|
||
$event = $this->getLastSentryEvent(); | ||
|
||
$this->assertNotNull($event); | ||
$this->assertEquals($event->getType(), EventType::event()); | ||
$this->assertArrayNotHasKey('laravel', $event->getContexts()); | ||
} | ||
|
||
public function testExceptionIsCapturedWithoutLaravelContextIfOnlyHidden(): void | ||
{ | ||
Context::addHidden('hidden', 'value'); | ||
|
||
captureException(new Exception('Context test')); | ||
|
||
$event = $this->getLastSentryEvent(); | ||
|
||
$this->assertNotNull($event); | ||
$this->assertEquals($event->getType(), EventType::event()); | ||
$this->assertArrayNotHasKey('laravel', $event->getContexts()); | ||
} | ||
|
||
public function testTransactionIsCapturedWithLaravelContext(): void | ||
{ | ||
$this->setupTestContext(); | ||
|
||
$transaction = $this->startTransaction(); | ||
$transaction->setSampled(true); | ||
$transaction->finish(); | ||
|
||
$event = $this->getLastSentryEvent(); | ||
|
||
$this->assertNotNull($event); | ||
$this->assertEquals($event->getType(), EventType::transaction()); | ||
$this->assertContextIsCaptured($event->getContexts()); | ||
} | ||
|
||
private function setupTestContext(): void | ||
{ | ||
Context::flush(); | ||
Context::add('foo', 'bar'); | ||
Context::addHidden('hidden', 'value'); | ||
} | ||
|
||
private function assertContextIsCaptured(array $context): void | ||
{ | ||
$this->assertArrayHasKey('laravel', $context); | ||
$this->assertArrayHasKey('foo', $context['laravel']); | ||
$this->assertArrayNotHasKey('hidden', $context['laravel']); | ||
$this->assertEquals('bar', $context['laravel']['foo']); | ||
} | ||
} |