Skip to content

Commit d8ebad3

Browse files
stayallivecleptric
andauthored
Improve stacktrace of test event (#926)
* Exclude the artisan file as “in-app” * Include the test command as “in-app” * CS --------- Co-authored-by: Michael Hoffmann <[email protected]>
1 parent 391c9dd commit d8ebad3

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/Sentry/Laravel/Console/TestCommand.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Sentry\Laravel\Console;
44

55
use Exception;
6+
use Sentry\Event;
67
use Illuminate\Console\Command;
78
use Illuminate\Support\Str;
89
use Psr\Log\AbstractLogger;
@@ -18,18 +19,7 @@
1819

1920
class TestCommand extends Command
2021
{
21-
/**
22-
* The name and signature of the console command.
23-
*
24-
* @var string
25-
*/
2622
protected $signature = 'sentry:test {--transaction} {--dsn=}';
27-
28-
/**
29-
* The console command description.
30-
*
31-
* @var string
32-
*/
3323
protected $description = 'Generate a test event and send it to Sentry';
3424

3525
/**
@@ -90,10 +80,31 @@ public function handle(): int
9080

9181
$options = [
9282
'dsn' => $dsn,
83+
'before_send' => static function (Event $event): ?Event {
84+
foreach ($event->getExceptions() as $exception) {
85+
$stacktrace = $exception->getStacktrace();
86+
87+
if ($stacktrace === null) {
88+
continue;
89+
}
90+
91+
foreach ($stacktrace->getFrames() as $frame) {
92+
if (str_starts_with($frame->getAbsoluteFilePath(), __DIR__)) {
93+
$frame->setIsInApp(true);
94+
}
95+
}
96+
}
97+
98+
return $event;
99+
},
100+
// We include this file as "in-app" so that the events generated have something to show
101+
'in_app_include' => [__DIR__],
102+
'in_app_exclude' => [base_path('artisan'), base_path('vendor')],
93103
'traces_sample_rate' => 1.0,
94104
];
95105

96106
if ($laravelClient !== null) {
107+
// Some options are taken from the client as configured by the user
97108
$options = array_merge($options, [
98109
'release' => $laravelClient->getOptions()->getRelease(),
99110
'environment' => $laravelClient->getOptions()->getEnvironment(),
@@ -143,6 +154,7 @@ public function log($level, $message, array $context = []): void
143154
}
144155
});
145156

157+
// We create a new Hub and Client to prevent user configuration from affecting the test command
146158
$hub = new Hub($clientBuilder->getClient());
147159

148160
$this->info('Sending test event...');
@@ -199,14 +211,9 @@ public function log($level, $message, array $context = []): void
199211
}
200212

201213
/**
202-
* Generate a test exception to send to Sentry.
203-
*
204-
* @param $command
205-
* @param $arg
206-
*
207-
* @return \Exception
214+
* Generate an example exception to send to Sentry.
208215
*/
209-
protected function generateTestException($command, $arg): Exception
216+
protected function generateTestException(string $command, array $arg): Exception
210217
{
211218
// Do something silly
212219
try {

src/Sentry/Laravel/ServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ protected function configureAndRegisterClient(): void
248248
$options = \array_merge(
249249
[
250250
'prefixes' => [$basePath],
251-
'in_app_exclude' => ["{$basePath}/vendor"],
251+
'in_app_exclude' => [
252+
"{$basePath}/vendor",
253+
"{$basePath}/artisan",
254+
],
252255
],
253256
$userConfig
254257
);

0 commit comments

Comments
 (0)