Skip to content

Commit

Permalink
slow queries
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Nov 30, 2023
1 parent dffb8c2 commit 3030138
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/Recorders/SlowQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,37 @@ public function __construct(
*/
public function record(QueryExecuted $event): void
{
$now = CarbonImmutable::now();

if ($event->time < $this->config->get('pulse.recorders.'.self::class.'.threshold')) {
return;
}

if (! $this->shouldSample() || $this->shouldIgnore($event->sql)) {
return;
}

$location = $this->config->get('pulse.recorders.'.self::class.'.location') ? $this->getLocation() : null;

$key = json_encode([$event->sql, $location], flags: JSON_THROW_ON_ERROR);

$this->pulse->record(
type: 'slow_query',
key: $key,
value: (int) $event->time,
timestamp: $now->subMilliseconds((int) $event->time),
)->max()->count();
[$timestampMs, $duration, $sql, $location] = [
CarbonImmutable::now()->getTimestampMs(),
(int) $event->time,
$event->sql,
$this->config->get('pulse.recorders.'.self::class.'.location')
? $this->resolveLocation()
: null,
];

$this->pulse->lazy(function () use ($timestampMs, $duration, $sql, $location) {
if (
$this->underThreshold($duration) ||
! $this->shouldSample() ||
$this->shouldIgnore($sql)
) {
return;
}

$this->pulse->record(
type: 'slow_query',
key: json_encode([$sql, $location], flags: JSON_THROW_ON_ERROR),
value: $duration,
timestamp: $timestampMs - $duration,
)->max()->count();
});
}

/**
* Get the location of the query.
* Resolve the location of the query.
*/
protected function getLocation(): string
protected function resolveLocation(): string
{
$backtrace = collect(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS))->skip(2);

Expand Down

0 comments on commit 3030138

Please sign in to comment.