Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
22 changes: 21 additions & 1 deletion resources/views/livewire/slow-requests.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@
<x-pulse::icons.arrows-left-right />
</x-slot:icon>
<x-slot:actions>
@php
$message = <<<MESSAGE
Count: X / Y → X requests above threshold out of Y total requests

Average: average request duration across all requests

Slowest: maximum request duration across all requests
MESSAGE;
@endphp
<button title="{{ $message }}" @click="alert(@js($message))">
<x-pulse::icons.information-circle class="w-5 h-5 stroke-gray-400 dark:stroke-gray-600" />
</button>

<x-pulse::select
wire:model.live="orderBy"
id="select-slow-requests-order-by"
label="Sort by"
:options="[
'slowest' => 'slowest',
'count' => 'count',
'average' => 'average',
'total' => 'total',
]"
@change="loading = true"
/>
Expand All @@ -31,12 +46,14 @@
<col width="100%" />
<col width="0%" />
<col width="0%" />
<col width="0%" />
</colgroup>
<x-pulse::thead>
<tr>
<x-pulse::th>Method</x-pulse::th>
<x-pulse::th>Route</x-pulse::th>
<x-pulse::th class="text-right">Count</x-pulse::th>
<x-pulse::th class="text-right">Average</x-pulse::th>
<x-pulse::th class="text-right">Slowest</x-pulse::th>
</tr>
</x-pulse::thead>
Expand Down Expand Up @@ -66,9 +83,12 @@
@if ($config['sample_rate'] < 1)
<span title="Sample rate: {{ $config['sample_rate'] }}, Raw value: {{ number_format($slowRequest->count) }}">~{{ number_format($slowRequest->count * (1 / $config['sample_rate'])) }}</span>
@else
{{ number_format($slowRequest->count) }}
{{ number_format($slowRequest->count) }} / {{ number_format($slowRequest->total) }}
@endif
</x-pulse::td>
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300">
<strong>{{ number_format($slowRequest->avg) }}</strong> ms
</x-pulse::td>
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300">
@if ($slowRequest->slowest === null)
<strong>Unknown</strong>
Expand Down
9 changes: 7 additions & 2 deletions src/Livewire/SlowRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
[$slowRequests, $time, $runAt] = $this->remember(
fn () => $this->aggregate(
'slow_request',
['max', 'count'],
['max', 'count', 'avg'],
match ($this->orderBy) {
'count' => 'count',
'average' => 'avg',

Check failure on line 40 in src/Livewire/SlowRequests.php

View workflow job for this annotation

GitHub Actions / tests / Static Analysis

Match arm comparison between 'slowest' and 'average' is always false.
'total' => 'avg_count',

Check failure on line 41 in src/Livewire/SlowRequests.php

View workflow job for this annotation

GitHub Actions / tests / Static Analysis

Match arm comparison between 'slowest' and 'total' is always false.
default => 'max',
},
)->map(function ($row) {
)
->map(function ($row) {
[$method, $uri, $action] = json_decode($row->key, flags: JSON_THROW_ON_ERROR);

return (object) [
Expand All @@ -48,6 +51,8 @@
'action' => $action,
'count' => $row->count,
'slowest' => $row->max,
'avg' => $row->avg,
'total' => $row->avg_count,
'threshold' => $this->threshold($uri, SlowRequestsRecorder::class),
];
}),
Expand Down
19 changes: 13 additions & 6 deletions src/Recorders/SlowRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,26 @@ public function record(Carbon $startedAt, Request $request, Response $response):

[$path, $via] = $this->resolveRoutePath($request);

if (
$this->shouldIgnore($path) ||
$this->underThreshold($duration = ((int) $startedAt->diffInMilliseconds()), $path)
) {
if ($this->shouldIgnore($path)) {
return;
}

$this->pulse->record(
$duration = ((int) $startedAt->diffInMilliseconds());

// Record average and max duration for all requests.
$entry = $this->pulse->record(
type: 'slow_request',
key: json_encode([$request->method(), $path, $via], flags: JSON_THROW_ON_ERROR),
value: $duration,
timestamp: $startedAt,
)->max()->count();
)->avg()->max();

if ($this->underThreshold($duration, $path)) {
return;
}

// Record count of slow requests
$entry->count();

if ($userId = $this->pulse->resolveAuthenticatedUserId()) {
$this->pulse->record(
Expand Down
12 changes: 12 additions & 0 deletions src/Storage/DatabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ public function aggregate(

$orderBy ??= $aggregates[0];

if (in_array('avg', $aggregates)) {
$aggregates[] = 'avg_count';
}

/** @phpstan-ignore return.type */
return $this->connection()
->query()
Expand All @@ -561,6 +565,7 @@ public function aggregate(
'max' => "max({$this->wrap('max')})",
'sum' => "sum({$this->wrap('sum')})",
'avg' => "avg({$this->wrap('avg')})",
'avg_count' => "sum({$this->wrap('avg_count')})",
}." as {$this->wrap($aggregate)}");
}

Expand All @@ -581,6 +586,7 @@ public function aggregate(
'max' => "max({$this->wrap('value')})",
'sum' => "sum({$this->wrap('value')})",
'avg' => "avg({$this->wrap('value')})",
'avg_count' => 'count(*)',
}." as {$this->wrap($aggregate)}");
}

Expand All @@ -593,6 +599,10 @@ public function aggregate(

// Buckets
foreach ($aggregates as $currentAggregate) {
if ($currentAggregate === 'avg_count') {
continue;
}

$query->unionAll(function (Builder $query) use ($type, $aggregates, $currentAggregate, $period, $oldestBucket) {
$query->select('key_hash');

Expand All @@ -605,6 +615,8 @@ public function aggregate(
'sum' => "sum({$this->wrap('value')})",
'avg' => "avg({$this->wrap('value')})",
}." as {$this->wrap($aggregate)}");
} elseif ($aggregate === 'avg_count' && $currentAggregate === 'avg') {
$query->selectRaw("sum({$this->wrap('count')}) as {$this->wrap('avg_count')}");
} else {
$query->selectRaw("null as {$this->wrap($aggregate)}");
}
Expand Down
Loading