Skip to content

Commit 6d56923

Browse files
authored
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
1 parent 2e77090 commit 6d56923

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
$event->setContext('laravel', Context::all());
34+
35+
return $event;
36+
});
37+
}
38+
}

Diff for: 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
+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
protected function setUp(): void
15+
{
16+
if (!class_exists(Context::class)) {
17+
$this->markTestSkipped('Laravel introduced contexts in version 11.');
18+
}
19+
20+
parent::setUp();
21+
}
22+
23+
public function testLaravelContextIntegrationIsRegistered(): void
24+
{
25+
$integration = $this->getSentryHubFromContainer()->getIntegration(LaravelContextIntegration::class);
26+
27+
$this->assertInstanceOf(LaravelContextIntegration::class, $integration);
28+
}
29+
30+
public function testExceptionIsCapturedWithLaravelContext(): void
31+
{
32+
$this->setupTestContext();
33+
34+
captureException(new Exception('Context test'));
35+
36+
$event = $this->getLastSentryEvent();
37+
38+
$this->assertNotNull($event);
39+
$this->assertEquals($event->getType(), EventType::event());
40+
$this->assertContextIsCaptured($event->getContexts());
41+
}
42+
43+
public function testExceptionIsCapturedWithoutLaravelContextIfEmpty(): void
44+
{
45+
captureException(new Exception('Context test'));
46+
47+
$event = $this->getLastSentryEvent();
48+
49+
$this->assertNotNull($event);
50+
$this->assertEquals($event->getType(), EventType::event());
51+
$this->assertArrayNotHasKey('laravel', $event->getContexts());
52+
}
53+
54+
public function testExceptionIsCapturedWithoutLaravelContextIfOnlyHidden(): void
55+
{
56+
Context::addHidden('hidden', 'value');
57+
58+
captureException(new Exception('Context test'));
59+
60+
$event = $this->getLastSentryEvent();
61+
62+
$this->assertNotNull($event);
63+
$this->assertEquals($event->getType(), EventType::event());
64+
$this->assertArrayNotHasKey('laravel', $event->getContexts());
65+
}
66+
67+
public function testTransactionIsCapturedWithLaravelContext(): void
68+
{
69+
$this->setupTestContext();
70+
71+
$transaction = $this->startTransaction();
72+
$transaction->setSampled(true);
73+
$transaction->finish();
74+
75+
$event = $this->getLastSentryEvent();
76+
77+
$this->assertNotNull($event);
78+
$this->assertEquals($event->getType(), EventType::transaction());
79+
$this->assertContextIsCaptured($event->getContexts());
80+
}
81+
82+
private function setupTestContext(): void
83+
{
84+
Context::flush();
85+
Context::add('foo', 'bar');
86+
Context::addHidden('hidden', 'value');
87+
}
88+
89+
private function assertContextIsCaptured(array $context): void
90+
{
91+
$this->assertArrayHasKey('laravel', $context);
92+
$this->assertArrayHasKey('foo', $context['laravel']);
93+
$this->assertArrayNotHasKey('hidden', $context['laravel']);
94+
$this->assertEquals('bar', $context['laravel']['foo']);
95+
}
96+
}

0 commit comments

Comments
 (0)