Skip to content

Improve how we signal that a route was matched #772

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

Merged
merged 6 commits into from
Nov 15, 2023
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
3 changes: 3 additions & 0 deletions src/Sentry/Laravel/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Laravel\Sanctum\Events as Sanctum;
use RuntimeException;
use Sentry\Breadcrumb;
use Sentry\Laravel\Tracing\Middleware;
use Sentry\Laravel\Util\WorksWithUris;
use Sentry\SentrySdk;
use Sentry\State\Scope;
Expand Down Expand Up @@ -216,6 +217,8 @@ protected function routeMatchedHandler(RoutingEvents\RouteMatched $match): void
return;
}

Middleware::signalRouteWasMatched();

[$routeName] = Integration::extractNameAndSourceForRoute($match->route);

Integration::addBreadcrumb(new Breadcrumb(
Expand Down
3 changes: 3 additions & 0 deletions src/Sentry/Laravel/Features/FolioPackageIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Laravel\Folio\Pipeline\MatchedView;
use Sentry\Breadcrumb;
use Sentry\Laravel\Integration;
use Sentry\Laravel\Tracing\Middleware;
use Sentry\SentrySdk;
use Sentry\Tracing\TransactionSource;

Expand All @@ -29,6 +30,8 @@ public function onBoot(Dispatcher $events): void

public function handleViewMatched(ViewMatched $matched): void
{
Middleware::signalRouteWasMatched();

$routeName = $this->extractRouteForMatchedView($matched->matchedView, $matched->mountPath);

Integration::addBreadcrumb(new Breadcrumb(
Expand Down
35 changes: 31 additions & 4 deletions src/Sentry/Laravel/Tracing/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

use function Sentry\continueTrace;

/**
* @internal
*/
class Middleware
{
/**
Expand Down Expand Up @@ -59,6 +62,13 @@ class Middleware
*/
private $registeredTerminatingCallback = false;

/**
* Whether a defined route was matched in the application.
*
* @var bool
*/
private $didRouteMatch = false;

/**
* Construct the Sentry tracing middleware.
*
Expand All @@ -80,8 +90,8 @@ public function __construct($app, bool $continueAfterResponse = true)
*/
public function handle(Request $request, Closure $next)
{
if (app()->bound(HubInterface::class)) {
$this->startTransaction($request, app(HubInterface::class));
if ($this->app->bound(HubInterface::class)) {
$this->startTransaction($request, $this->app->make(HubInterface::class));
}

return $next($request);
Expand All @@ -98,12 +108,12 @@ public function handle(Request $request, Closure $next)
public function terminate(Request $request, $response): void
{
// If there is no transaction or the HubInterface is not bound in the container there is nothing for us to do
if ($this->transaction === null || !app()->bound(HubInterface::class)) {
if ($this->transaction === null || !$this->app->bound(HubInterface::class)) {
return;
}

// We stop here if a route has not been matched unless we are configured to trace missing routes
if (config('sentry.tracing.missing_routes', false) === false && $request->route() === null) {
if (!$this->didRouteMatch && config('sentry.tracing.missing_routes', false) === false) {
return;
}

Expand Down Expand Up @@ -154,6 +164,9 @@ public function setBootedTimestamp(?float $timestamp = null): void

private function startTransaction(Request $request, HubInterface $sentry): void
{
// Reset our internal state in case we are handling multiple requests (e.g. in Octane)
$this->didRouteMatch = false;

// Try $_SERVER['REQUEST_TIME_FLOAT'] then LARAVEL_START and fallback to microtime(true) if neither are defined
$requestStartTime = $request->server(
'REQUEST_TIME_FLOAT',
Expand Down Expand Up @@ -260,4 +273,18 @@ private function finishTransaction(): void
$this->transaction->finish();
$this->transaction = null;
}

private function internalSignalRouteWasMatched(): void
{
$this->didRouteMatch = true;
}

public static function signalRouteWasMatched(): void
{
if (!app()->bound(self::class)) {
return;
}

app(self::class)->internalSignalRouteWasMatched();
}
}
2 changes: 1 addition & 1 deletion test/Sentry/ClientBuilderDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testClientHasEnvironmentSetFromDecorator(): void
{
$this->assertEquals(
'from_service_container',
$this->getClientFromContainer()->getOptions()->getEnvironment()
$this->getSentryClientFromContainer()->getOptions()->getEnvironment()
);
}
}
4 changes: 2 additions & 2 deletions test/Sentry/EventHandler/ConsoleEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testCommandBreadcrumbIsRecordedWhenEnabled(): void

$this->dispatchCommandStartEvent();

$lastBreadcrumb = $this->getLastBreadcrumb();
$lastBreadcrumb = $this->getLastSentryBreadcrumb();

$this->assertEquals('Starting Artisan command: test:command', $lastBreadcrumb->getMessage());
$this->assertEquals('--foo=bar', $lastBreadcrumb->getMetadata()['input']);
Expand All @@ -35,7 +35,7 @@ public function testCommandBreadcrumIsNotRecordedWhenDisabled(): void

$this->dispatchCommandStartEvent();

$this->assertEmpty($this->getCurrentBreadcrumbs());
$this->assertEmpty($this->getCurrentSentryBreadcrumbs());
}

private function dispatchCommandStartEvent(): void
Expand Down
8 changes: 4 additions & 4 deletions test/Sentry/EventHandler/DatabaseEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testSqlQueriesAreRecordedWhenEnabled(): void
$this->getMockedConnection()
));

$lastBreadcrumb = $this->getLastBreadcrumb();
$lastBreadcrumb = $this->getLastSentryBreadcrumb();

$this->assertEquals($query, $lastBreadcrumb->getMessage());
}
Expand All @@ -44,7 +44,7 @@ public function testSqlBindingsAreRecordedWhenEnabled(): void
$this->getMockedConnection()
));

$lastBreadcrumb = $this->getLastBreadcrumb();
$lastBreadcrumb = $this->getLastSentryBreadcrumb();

$this->assertEquals($query, $lastBreadcrumb->getMessage());
$this->assertEquals($bindings, $lastBreadcrumb->getMetadata()['bindings']);
Expand All @@ -65,7 +65,7 @@ public function testSqlQueriesAreRecordedWhenDisabled(): void
$this->getMockedConnection()
));

$this->assertEmpty($this->getCurrentBreadcrumbs());
$this->assertEmpty($this->getCurrentSentryBreadcrumbs());
}

public function testSqlBindingsAreRecordedWhenDisabled(): void
Expand All @@ -83,7 +83,7 @@ public function testSqlBindingsAreRecordedWhenDisabled(): void
$this->getMockedConnection()
));

$lastBreadcrumb = $this->getLastBreadcrumb();
$lastBreadcrumb = $this->getLastSentryBreadcrumb();

$this->assertEquals($query, $lastBreadcrumb->getMessage());
$this->assertFalse(isset($lastBreadcrumb->getMetadata()['bindings']));
Expand Down
4 changes: 2 additions & 2 deletions test/Sentry/EventHandler/LogEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testLaravelLogsAreRecordedWhenEnabled(): void
$context = ['1']
));

$lastBreadcrumb = $this->getLastBreadcrumb();
$lastBreadcrumb = $this->getLastSentryBreadcrumb();

$this->assertEquals($level, $lastBreadcrumb->getLevel());
$this->assertEquals($message, $lastBreadcrumb->getMessage());
Expand All @@ -38,6 +38,6 @@ public function testLaravelLogsAreRecordedWhenDisabled(): void

$this->dispatchLaravelEvent(new MessageLogged('debug', 'test message'));

$this->assertEmpty($this->getCurrentBreadcrumbs());
$this->assertEmpty($this->getCurrentSentryBreadcrumbs());
}
}
22 changes: 11 additions & 11 deletions test/Sentry/EventHandler/QueueEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ public function testQueueJobPushesAndPopsScopeWithBreadcrumbs(): void
{
dispatch(new QueueEventsTestJobWithBreadcrumb);

$this->assertCount(0, $this->getCurrentBreadcrumbs());
$this->assertCount(0, $this->getCurrentSentryBreadcrumbs());
}

public function testQueueJobThatReportsPushesAndPopsScopeWithBreadcrumbs(): void
{
dispatch(new QueueEventsTestJobThatReportsAnExceptionWithBreadcrumb);

$this->assertCount(0, $this->getCurrentBreadcrumbs());
$this->assertCount(0, $this->getCurrentSentryBreadcrumbs());

$this->assertNotNull($this->getLastEvent());
$this->assertNotNull($this->getLastSentryEvent());

$event = $this->getLastEvent();
$event = $this->getLastSentryEvent();

$this->assertCount(2, $event->getBreadcrumbs());
}
Expand All @@ -41,12 +41,12 @@ public function testQueueJobThatThrowsLeavesPushedScopeWithBreadcrumbs(): void

// We still expect to find the breadcrumbs from the job here so they are attached to reported exceptions

$this->assertCount(2, $this->getCurrentBreadcrumbs());
$this->assertCount(2, $this->getCurrentSentryBreadcrumbs());

$firstBreadcrumb = $this->getCurrentBreadcrumbs()[0];
$firstBreadcrumb = $this->getCurrentSentryBreadcrumbs()[0];
$this->assertEquals('queue.job', $firstBreadcrumb->getCategory());

$secondBreadcrumb = $this->getCurrentBreadcrumbs()[1];
$secondBreadcrumb = $this->getCurrentSentryBreadcrumbs()[1];
$this->assertEquals('test', $secondBreadcrumb->getCategory());
}

Expand All @@ -66,12 +66,12 @@ public function testQueueJobsThatThrowPopsAndPushesScopeWithBreadcrumbsBeforeNew

// We only expect to find the breadcrumbs from the second job here

$this->assertCount(2, $this->getCurrentBreadcrumbs());
$this->assertCount(2, $this->getCurrentSentryBreadcrumbs());

$firstBreadcrumb = $this->getCurrentBreadcrumbs()[0];
$firstBreadcrumb = $this->getCurrentSentryBreadcrumbs()[0];
$this->assertEquals('queue.job', $firstBreadcrumb->getCategory());

$secondBreadcrumb = $this->getCurrentBreadcrumbs()[1];
$secondBreadcrumb = $this->getCurrentSentryBreadcrumbs()[1];
$this->assertEquals('test #2', $secondBreadcrumb->getMessage());
}

Expand All @@ -83,7 +83,7 @@ public function testQueueJobsWithBreadcrumbSetInBetweenKeepsNonJobBreadcrumbsOnC

dispatch(new QueueEventsTestJobWithBreadcrumb);

$this->assertCount(1, $this->getCurrentBreadcrumbs());
$this->assertCount(1, $this->getCurrentSentryBreadcrumbs());
}
}

Expand Down
12 changes: 6 additions & 6 deletions test/Sentry/Features/CacheIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ public function testCacheBreadcrumbForWriteAndHitIsRecorded(): void
{
Cache::put($key = 'foo', 'bar');

$this->assertEquals("Written: {$key}", $this->getLastBreadcrumb()->getMessage());
$this->assertEquals("Written: {$key}", $this->getLastSentryBreadcrumb()->getMessage());

Cache::get('foo');

$this->assertEquals("Read: {$key}", $this->getLastBreadcrumb()->getMessage());
$this->assertEquals("Read: {$key}", $this->getLastSentryBreadcrumb()->getMessage());
}

public function testCacheBreadcrumbForWriteAndForgetIsRecorded(): void
{
Cache::put($key = 'foo', 'bar');

$this->assertEquals("Written: {$key}", $this->getLastBreadcrumb()->getMessage());
$this->assertEquals("Written: {$key}", $this->getLastSentryBreadcrumb()->getMessage());

Cache::forget($key);

$this->assertEquals("Forgotten: {$key}", $this->getLastBreadcrumb()->getMessage());
$this->assertEquals("Forgotten: {$key}", $this->getLastSentryBreadcrumb()->getMessage());
}

public function testCacheBreadcrumbForMissIsRecorded(): void
{
Cache::get($key = 'foo');

$this->assertEquals("Missed: {$key}", $this->getLastBreadcrumb()->getMessage());
$this->assertEquals("Missed: {$key}", $this->getLastSentryBreadcrumb()->getMessage());
}

public function testCacheBreadcrumbIsNotRecordedWhenDisabled(): void
Expand All @@ -46,6 +46,6 @@ public function testCacheBreadcrumbIsNotRecordedWhenDisabled(): void

Cache::get('foo');

$this->assertEmpty($this->getCurrentBreadcrumbs());
$this->assertEmpty($this->getCurrentSentryBreadcrumbs());
}
}
18 changes: 10 additions & 8 deletions test/Sentry/Features/ConsoleIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ class ConsoleIntegrationTest extends TestCase
public function testScheduleMacro(): void
{
/** @var Event $scheduledEvent */
$scheduledEvent = $this->getScheduler()->call(function () {})->sentryMonitor('test-monitor');
$scheduledEvent = $this->getScheduler()
->call(function () {})
->sentryMonitor('test-monitor');

$scheduledEvent->run($this->app);

// We expect a total of 2 events to be sent to Sentry:
// 1. The start check-in event
// 2. The finish check-in event
$this->assertEquals(2, $this->getEventsCount());
$this->assertSentryCheckInCount(2);

$finishCheckInEvent = $this->getLastEvent();
$finishCheckInEvent = $this->getLastSentryEvent();

$this->assertNotNull($finishCheckInEvent->getCheckIn());
$this->assertEquals('test-monitor', $finishCheckInEvent->getCheckIn()->getMonitorSlug());
Expand All @@ -48,9 +50,9 @@ public function testScheduleMacroWithTimeZone(): void
// We expect a total of 2 events to be sent to Sentry:
// 1. The start check-in event
// 2. The finish check-in event
$this->assertEquals(2, $this->getEventsCount());
$this->assertSentryCheckInCount(2);

$finishCheckInEvent = $this->getLastEvent();
$finishCheckInEvent = $this->getLastSentryEvent();

$this->assertNotNull($finishCheckInEvent->getCheckIn());
$this->assertEquals($expectedTimezone, $finishCheckInEvent->getCheckIn()->getMonitorConfig()->getTimezone());
Expand All @@ -66,9 +68,9 @@ public function testScheduleMacroAutomaticSlug(): void
// We expect a total of 2 events to be sent to Sentry:
// 1. The start check-in event
// 2. The finish check-in event
$this->assertEquals(2, $this->getEventsCount());
$this->assertSentryCheckInCount(2);

$finishCheckInEvent = $this->getLastEvent();
$finishCheckInEvent = $this->getLastSentryEvent();

$this->assertNotNull($finishCheckInEvent->getCheckIn());
$this->assertEquals('scheduled_artisan-inspire', $finishCheckInEvent->getCheckIn()->getMonitorSlug());
Expand All @@ -89,7 +91,7 @@ public function testScheduleMacroWithoutDsnSet(): void

$scheduledEvent->run($this->app);

$this->assertEquals(0, $this->getEventsCount());
$this->assertSentryCheckInCount(0);
}

public function testScheduleMacroIsRegistered(): void
Expand Down
Loading