Skip to content

Commit

Permalink
Add current Laravel context to event (#869)
Browse files Browse the repository at this point in the history
* 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
stayallive authored Mar 25, 2024
1 parent 2e77090 commit 6d56923
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Sentry/Laravel/Integration/LaravelContextIntegration.php
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;
});
}
}
1 change: 1 addition & 0 deletions src/Sentry/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ protected function configureAndRegisterClient(): void
$integrations,
[
new Integration,
new Integration\LaravelContextIntegration,
new Integration\ExceptionContextIntegration,
],
$userIntegrations
Expand Down
96 changes: 96 additions & 0 deletions test/Sentry/Integration/LaravelContextIntegrationTest.php
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']);
}
}

0 comments on commit 6d56923

Please sign in to comment.