Skip to content

Commit

Permalink
Use ConditionalQueryWatcher to clean up SlowQueryWatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickomeara committed Jul 26, 2024
1 parent 018728f commit 3d6f44b
Showing 1 changed file with 3 additions and 18 deletions.
21 changes: 3 additions & 18 deletions src/Watchers/SlowQueryWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
namespace Spatie\LaravelRay\Watchers;

use Illuminate\Database\Events\QueryExecuted;
use Illuminate\Support\Facades\Event;
use Spatie\LaravelRay\Payloads\ExecutedQueryPayload;
use Spatie\LaravelRay\Ray;
use Spatie\Ray\Settings\Settings;

class SlowQueryWatcher extends QueryWatcher
class SlowQueryWatcher extends ConditionalQueryWatcher
{
protected $minimumTimeInMs = 500;

Expand All @@ -19,20 +16,8 @@ public function register(): void
$this->enabled = $settings->send_slow_queries_to_ray ?? false;
$this->minimumTimeInMs = $settings->slow_query_threshold_in_ms ?? $this->minimumTimeInMs;

Event::listen(QueryExecuted::class, function (QueryExecuted $query) {
if (! $this->enabled()) {
return;
}

$ray = app(Ray::class);

if ($query->time >= $this->minimumTimeInMs) {
$payload = new ExecutedQueryPayload($query);

$ray->sendRequest($payload);
}

optional($this->rayProxy)->applyCalledMethods($ray);
$this->setConditionalCallback(function (QueryExecuted $query) {
return $query->time >= $this->minimumTimeInMs;
});
}

Expand Down

0 comments on commit 3d6f44b

Please sign in to comment.