Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current Laravel context to event #869

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
stayallive marked this conversation as resolved.
Show resolved Hide resolved
$this->assertEquals('bar', $context['laravel']['foo']);
}
}
Loading