From d8ebad3cc1258c272b471f29e5ec27301dfd5c74 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Thu, 18 Jul 2024 13:34:15 +0200 Subject: [PATCH] Improve stacktrace of test event (#926) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Exclude the artisan file as “in-app” * Include the test command as “in-app” * CS --------- Co-authored-by: Michael Hoffmann --- src/Sentry/Laravel/Console/TestCommand.php | 43 +++++++++++++--------- src/Sentry/Laravel/ServiceProvider.php | 5 ++- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/Sentry/Laravel/Console/TestCommand.php b/src/Sentry/Laravel/Console/TestCommand.php index 0903ea36..6fe21d4b 100644 --- a/src/Sentry/Laravel/Console/TestCommand.php +++ b/src/Sentry/Laravel/Console/TestCommand.php @@ -3,6 +3,7 @@ namespace Sentry\Laravel\Console; use Exception; +use Sentry\Event; use Illuminate\Console\Command; use Illuminate\Support\Str; use Psr\Log\AbstractLogger; @@ -18,18 +19,7 @@ class TestCommand extends Command { - /** - * The name and signature of the console command. - * - * @var string - */ protected $signature = 'sentry:test {--transaction} {--dsn=}'; - - /** - * The console command description. - * - * @var string - */ protected $description = 'Generate a test event and send it to Sentry'; /** @@ -90,10 +80,31 @@ public function handle(): int $options = [ 'dsn' => $dsn, + 'before_send' => static function (Event $event): ?Event { + foreach ($event->getExceptions() as $exception) { + $stacktrace = $exception->getStacktrace(); + + if ($stacktrace === null) { + continue; + } + + foreach ($stacktrace->getFrames() as $frame) { + if (str_starts_with($frame->getAbsoluteFilePath(), __DIR__)) { + $frame->setIsInApp(true); + } + } + } + + return $event; + }, + // We include this file as "in-app" so that the events generated have something to show + 'in_app_include' => [__DIR__], + 'in_app_exclude' => [base_path('artisan'), base_path('vendor')], 'traces_sample_rate' => 1.0, ]; if ($laravelClient !== null) { + // Some options are taken from the client as configured by the user $options = array_merge($options, [ 'release' => $laravelClient->getOptions()->getRelease(), 'environment' => $laravelClient->getOptions()->getEnvironment(), @@ -143,6 +154,7 @@ public function log($level, $message, array $context = []): void } }); + // We create a new Hub and Client to prevent user configuration from affecting the test command $hub = new Hub($clientBuilder->getClient()); $this->info('Sending test event...'); @@ -199,14 +211,9 @@ public function log($level, $message, array $context = []): void } /** - * Generate a test exception to send to Sentry. - * - * @param $command - * @param $arg - * - * @return \Exception + * Generate an example exception to send to Sentry. */ - protected function generateTestException($command, $arg): Exception + protected function generateTestException(string $command, array $arg): Exception { // Do something silly try { diff --git a/src/Sentry/Laravel/ServiceProvider.php b/src/Sentry/Laravel/ServiceProvider.php index 36db3403..4177e2b6 100644 --- a/src/Sentry/Laravel/ServiceProvider.php +++ b/src/Sentry/Laravel/ServiceProvider.php @@ -248,7 +248,10 @@ protected function configureAndRegisterClient(): void $options = \array_merge( [ 'prefixes' => [$basePath], - 'in_app_exclude' => ["{$basePath}/vendor"], + 'in_app_exclude' => [ + "{$basePath}/vendor", + "{$basePath}/artisan", + ], ], $userConfig );