Skip to content

Commit

Permalink
Add ability to disable location capture and grouping (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher authored Nov 7, 2023
1 parent d5185ba commit 81acaa0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
2 changes: 2 additions & 0 deletions config/pulse.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
Recorders\Exceptions::class => [
'enabled' => env('PULSE_EXCEPTIONS_ENABLED', true),
'sample_rate' => env('PULSE_EXCEPTIONS_SAMPLE_RATE', 1),
'location' => env('PULSE_EXCEPTIONS_LOCATION', true),
'ignore' => [
// '/^Package\\\\Exceptions\\\\/',
],
Expand Down Expand Up @@ -184,6 +185,7 @@
'enabled' => env('PULSE_SLOW_QUERIES_ENABLED', true),
'sample_rate' => env('PULSE_SLOW_QUERIES_SAMPLE_RATE', 1),
'threshold' => env('PULSE_SLOW_QUERY_THRESHOLD', 1000),
'location' => env('PULSE_SLOW_QUERY_LOCATION', true),
'ignore' => [
'/(["`])pulse_[\w]+?\1/', // Pulse tables
],
Expand Down
42 changes: 21 additions & 21 deletions src/Recorders/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function record(Throwable $e): ?Entry
{
$now = new CarbonImmutable();

[$class, $location] = $this->getDetails($e);
$class = $this->getClass($e);

if (! $this->shouldSample() || $this->shouldIgnore($class)) {
return null;
Expand All @@ -64,35 +64,35 @@ public function record(Throwable $e): ?Entry
return new Entry($this->table, [
'date' => $now->toDateTimeString(),
'class' => $class,
'location' => $location,
'location' => $this->config->get('pulse.recorders.'.self::class.'.location') ? $this->getLocation($e) : '',
'user_id' => $this->pulse->authenticatedUserIdResolver(),
]);
}

/**
* Get the exception details.
* Get the exception class to record.
*
* @return array{0: class-string<Throwable>, 1: string}
* @return class-string<Throwable>
*/
protected function getDetails(Throwable $e): array
protected function getClass(Throwable $e): string
{
return match (true) {
$e instanceof \Illuminate\View\ViewException => [
get_class($e->getPrevious()), // @phpstan-ignore argument.type
$this->getLocationFromViewException($e),
],

$e instanceof \Spatie\LaravelIgnition\Exceptions\ViewException => [ // @phpstan-ignore class.notFound
get_class($e->getPrevious()), // @phpstan-ignore argument.type, class.notFound
$this->formatLocation($e->getFile(), $e->getLine()), // @phpstan-ignore class.notFound, class.notFound
],

default => [
get_class($e),
$this->getLocation($e),
]
return match (true) { // @phpstan-ignore return.type
$e instanceof \Illuminate\View\ViewException,
$e instanceof \Spatie\LaravelIgnition\Exceptions\ViewException => get_class($e->getPrevious()), // @phpstan-ignore class.notFound class.notFound argument.type
default => get_class($e),
};
}

/**
* Get the exception location to record.
*/
protected function getLocation(Throwable $e): string
{
return match (true) {
$e instanceof \Illuminate\View\ViewException => $this->getLocationFromViewException($e),
$e instanceof \Spatie\LaravelIgnition\Exceptions\ViewException => $this->formatLocation($e->getFile(), $e->getLine()), // @phpstan-ignore class.notFound class.notFound class.notFound
default => $this->getLocationFromTrace($e)
};
}

/*
Expand All @@ -109,7 +109,7 @@ protected function getLocationFromViewException(Throwable $e): string
/**
* Get the location for the given exception.
*/
protected function getLocation(Throwable $e): string
protected function getLocationFromTrace(Throwable $e): string
{
$firstNonVendorFrame = collect($e->getTrace())
->firstWhere(fn (array $frame) => isset($frame['file']) && ! $this->isInternalFile($frame['file']));
Expand Down
2 changes: 1 addition & 1 deletion src/Recorders/SlowQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function record(QueryExecuted $event): ?Entry
return new Entry($this->table, [
'date' => $now->subMilliseconds((int) $event->time)->toDateTimeString(),
'sql' => $event->sql,
'location' => $this->getLocation(),
'location' => $this->config->get('pulse.recorders.'.self::class.'.location') ? $this->getLocation() : '',
'duration' => (int) $event->time,
'user_id' => $this->pulse->authenticatedUserIdResolver(),
]);
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/Recorders/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@
]);
});

it('can disable capturing the location', function () {
Config::set('pulse.recorders.'.Exceptions::class.'.location', false);
Carbon::setTestNow('2000-01-02 03:04:05');

report(new RuntimeException('Expected exception.'));
Pulse::store(app(Ingest::class));

$exceptions = Pulse::ignore(fn () => DB::table('pulse_exceptions')->get());
expect($exceptions)->toHaveCount(1);
expect($exceptions[0])->toHaveProperties([
'date' => '2000-01-02 03:04:05',
'user_id' => null,
'class' => 'RuntimeException',
'location' => '',
]);
});

it('captures the authenticated user', function () {
Auth::login(User::make(['id' => '567']));

Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/Recorders/SlowQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@
'sql' => 'select * from users',
'duration' => 5000,
]);
expect($queries[0]->location)->not->toBe('');
});

it('can disable capturing the location', function () {
Config::set('pulse.recorders.'.SlowQueries::class.'.location', false);
Carbon::setTestNow('2000-01-02 03:04:05');
prependListener(QueryExecuted::class, function (QueryExecuted $event) {
$event->time = 5000;
});

DB::connection()->statement('select * from users');
Pulse::store(app(Ingest::class));

expect(Pulse::entries())->toHaveCount(0);
$queries = Pulse::ignore(fn () => DB::table('pulse_slow_queries')->get());
expect($queries)->toHaveCount(1);
expect($queries[0])->toHaveProperties([
'date' => '2000-01-02 03:04:00',
'user_id' => null,
'sql' => 'select * from users',
'location' => '',
'duration' => 5000,
]);
});

it('does not ingest queries under the slow query threshold', function () {
Expand Down

0 comments on commit 81acaa0

Please sign in to comment.