From ee829122203a9ba968336de2ecd30da746f4937d Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Thu, 18 Jul 2024 11:51:45 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Exclude=20the=20artisan=20file=20as=20?= =?UTF-8?q?=E2=80=9Cin-app=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Sentry/Laravel/ServiceProvider.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 ); From fb6e0d01236dd34494c235b087bfeef8d13ada34 Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Thu, 18 Jul 2024 11:51:53 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Include=20the=20test=20command=20as=20?= =?UTF-8?q?=E2=80=9Cin-app=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Sentry/Laravel/Console/TestCommand.php | 43 +++++++++++++--------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Sentry/Laravel/Console/TestCommand.php b/src/Sentry/Laravel/Console/TestCommand.php index 0903ea36..bd1f56c4 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 { From 51f4de98bddba819ff2c65ce69b47ee3b0e0b7c3 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Thu, 18 Jul 2024 12:57:03 +0200 Subject: [PATCH 3/3] CS --- src/Sentry/Laravel/Console/TestCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry/Laravel/Console/TestCommand.php b/src/Sentry/Laravel/Console/TestCommand.php index bd1f56c4..6fe21d4b 100644 --- a/src/Sentry/Laravel/Console/TestCommand.php +++ b/src/Sentry/Laravel/Console/TestCommand.php @@ -89,7 +89,7 @@ public function handle(): int } foreach ($stacktrace->getFrames() as $frame) { - if (str_starts_with($frame->getAbsoluteFilePath(), __DIR__)) { + if (str_starts_with($frame->getAbsoluteFilePath(), __DIR__)) { $frame->setIsInApp(true); } }