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
11 changes: 8 additions & 3 deletions src/Livewire/SlowRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SlowRequests extends Card
/**
* Ordering.
*
* @var 'slowest'|'count'
* @var 'slowest'|'count'|'average'|'total'
*/
#[Url(as: 'slow-requests')]
public string $orderBy = 'slowest';
Expand All @@ -34,12 +34,15 @@ public function render(): Renderable
[$slowRequests, $time, $runAt] = $this->remember(
fn () => $this->aggregate(
'slow_request',
['max', 'count'],
['max', 'count', 'avg'],
match ($this->orderBy) {
'count' => 'count',
'average' => 'avg',
'total' => 'avg_count',
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 @@ public function render(): Renderable
'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
8 changes: 4 additions & 4 deletions tests/Feature/Livewire/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
Pulse::ingest();

Livewire::test(Cache::class, ['lazy' => false])
->assertDontSeeHtml("100.00%\n")
->assertSeeHtml("99.99%\n");
->assertDontSeeHtml("100.00%" . PHP_EOL)
->assertSeeHtml("99.99%" . PHP_EOL);
});

it('does not show decimals for round numbers', function () {
Expand All @@ -68,6 +68,6 @@
Pulse::ingest();

Livewire::test(Cache::class, ['lazy' => false])
->assertDontSeeHtml("50.00%\n")
->assertSeeHtml("50%\n");
->assertDontSeeHtml("50.00%" . PHP_EOL)
->assertSeeHtml("50%" . PHP_EOL);
});
20 changes: 10 additions & 10 deletions tests/Feature/Livewire/SlowRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@

// Add entries outside of the window.
Carbon::setTestNow('2000-01-01 12:00:00');
Pulse::record('slow_request', $request1, 1)->max()->count();
Pulse::record('slow_request', $request2, 1)->max()->count();
Pulse::record('slow_request', $request1, 1)->avg()->max()->count();
Pulse::record('slow_request', $request2, 1)->avg()->max()->count();

// Add entries to the "tail".
Carbon::setTestNow('2000-01-01 12:00:01');
Pulse::record('slow_request', $request1, 1234)->max()->count();
Pulse::record('slow_request', $request1, 2468)->max()->count();
Pulse::record('slow_request', $request2, 1234)->max()->count();
Pulse::record('slow_request', $request1, 1234)->avg()->max()->count();
Pulse::record('slow_request', $request1, 2468)->avg()->max()->count();
Pulse::record('slow_request', $request2, 1234)->avg()->max()->count();

// Add entries to the current buckets.
Carbon::setTestNow('2000-01-01 13:00:00');
Pulse::record('slow_request', $request1, 1000)->max()->count();
Pulse::record('slow_request', $request1, 1000)->max()->count();
Pulse::record('slow_request', $request2, 1000)->max()->count();
Pulse::record('slow_request', $request1, 1000)->avg()->max()->count();
Pulse::record('slow_request', $request1, 1000)->avg()->max()->count();
Pulse::record('slow_request', $request2, 1000)->avg()->max()->count();

Pulse::ingest();

Livewire::test(SlowRequests::class, ['lazy' => false])
->assertViewHas('slowRequests', collect([
(object) ['method' => 'GET', 'uri' => '/users', 'action' => 'FooController@index', 'count' => 4, 'slowest' => 2468, 'threshold' => 1_000],
(object) ['method' => 'GET', 'uri' => '/users/{user}', 'action' => 'Closure', 'count' => 2, 'slowest' => 1234, 'threshold' => 1_000],
(object) ['method' => 'GET', 'uri' => '/users', 'action' => 'FooController@index', 'count' => 4, 'slowest' => 2468, 'threshold' => 1_000, 'avg' => 1425.5, 'total' => 4],
(object) ['method' => 'GET', 'uri' => '/users/{user}', 'action' => 'Closure', 'count' => 2, 'slowest' => 1234, 'threshold' => 1_000, 'avg' => 1117, 'total' => 2],
]));
});
Loading