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

Improve stacktrace of test event #926

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
Loading