Skip to content

Commit

Permalink
Improve stacktrace of test event (#926)
Browse files Browse the repository at this point in the history
* Exclude the artisan file as “in-app”

* Include the test command as “in-app”

* CS

---------

Co-authored-by: Michael Hoffmann <michael.hoffmann@sentry.io>
  • Loading branch information
stayallive and cleptric authored Jul 18, 2024
1 parent 391c9dd commit d8ebad3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
43 changes: 25 additions & 18 deletions src/Sentry/Laravel/Console/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';

/**
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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...');
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion src/Sentry/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down

0 comments on commit d8ebad3

Please sign in to comment.