Skip to content

Commit 0e7db17

Browse files
committed
Set the Laravel provided context on the events
1 parent 31a38b7 commit 0e7db17

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Sentry\Laravel\Integration;
4+
5+
use Illuminate\Support\Facades\Context;
6+
use Sentry\Event;
7+
use Sentry\EventHint;
8+
use Sentry\EventType;
9+
use Sentry\Integration\IntegrationInterface;
10+
use Sentry\SentrySdk;
11+
use Sentry\State\Scope;
12+
13+
class LaravelContextIntegration implements IntegrationInterface
14+
{
15+
public function setupOnce(): void
16+
{
17+
// Context was introduced in Laravel 11 so we need to check if we can use it otherwise we skip the event processor
18+
if (!class_exists(Context::class)) {
19+
return;
20+
}
21+
22+
Scope::addGlobalEventProcessor(static function (Event $event, ?EventHint $hint = null): Event {
23+
$self = SentrySdk::getCurrentHub()->getIntegration(self::class);
24+
25+
if (!$self instanceof self) {
26+
return $event;
27+
}
28+
29+
if (!in_array($event->getType(), [EventType::event(), EventType::transaction()], true)) {
30+
return $event;
31+
}
32+
33+
if (!Context::isEmpty()) {
34+
$event->setContext('laravel', Context::all());
35+
}
36+
37+
return $event;
38+
});
39+
}
40+
}

src/Sentry/Laravel/ServiceProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ protected function configureAndRegisterClient(): void
337337
$integrations,
338338
[
339339
new Integration,
340+
new Integration\LaravelContextIntegration,
340341
new Integration\ExceptionContextIntegration,
341342
],
342343
$userIntegrations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Sentry\Integration;
4+
5+
use Exception;
6+
use Illuminate\Support\Facades\Context;
7+
use Sentry\EventType;
8+
use Sentry\Laravel\Integration\LaravelContextIntegration;
9+
use Sentry\Laravel\Tests\TestCase;
10+
use function Sentry\captureException;
11+
12+
class LaravelContextIntegrationTest extends TestCase
13+
{
14+
public function testLaravelContextIntegrationIsRegistered(): void
15+
{
16+
$integration = $this->getSentryHubFromContainer()->getIntegration(LaravelContextIntegration::class);
17+
18+
$this->assertInstanceOf(LaravelContextIntegration::class, $integration);
19+
}
20+
21+
public function testExceptionIsCapturedWithLaravelContext(): void
22+
{
23+
$this->setupTestContext();
24+
25+
captureException(new Exception('Context test'));
26+
27+
$event = $this->getLastSentryEvent();
28+
29+
$this->assertNotNull($event);
30+
$this->assertEquals($event->getType(), EventType::event());
31+
$this->assertContextIsCaptured($event->getContexts());
32+
}
33+
34+
public function testTransactionIsCapturedWithLaravelContext(): void
35+
{
36+
$this->setupTestContext();
37+
38+
$transaction = $this->startTransaction();
39+
$transaction->setSampled(true);
40+
$transaction->finish();
41+
42+
$event = $this->getLastSentryEvent();
43+
44+
$this->assertNotNull($event);
45+
$this->assertEquals($event->getType(), EventType::transaction());
46+
$this->assertContextIsCaptured($event->getContexts());
47+
}
48+
49+
private function setupTestContext(): void
50+
{
51+
Context::flush();
52+
Context::add('foo', 'bar');
53+
Context::addHidden('hidden', 'value');
54+
}
55+
56+
private function assertContextIsCaptured(array $context): void
57+
{
58+
$this->assertArrayHasKey('laravel', $context);
59+
$this->assertArrayHasKey('foo', $context['laravel']);
60+
$this->assertArrayNotHasKey('hidden', $context['laravel']);
61+
$this->assertEquals('bar', $context['laravel']['foo']);
62+
}
63+
}

0 commit comments

Comments
 (0)