Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow outgoing requests #40

Merged
merged 4 commits into from
Oct 23, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ public function up(): void
$table->text('uri');
$table->char('uri_hash', 16)->charset('binary')->virtualAs('UNHEX(MD5(`uri`))');
$table->unsignedInteger('duration');
$table->boolean('slow');

$table->index(['uri_hash', 'date', 'duration']);
$table->index(['date']); // trim
$table->index(['uri_hash']); // slow_outgoing_requests
$table->index(['slow', 'date', 'uri_hash', 'duration']); // slow_outgoing_requests
});
}

Expand Down
18 changes: 13 additions & 5 deletions src/Queries/SlowOutgoingRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Carbon\CarbonInterval as Interval;
use Illuminate\Config\Repository;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Laravel\Pulse\Recorders\OutgoingRequests;

/**
* @internal
Expand Down Expand Up @@ -35,13 +35,21 @@ public function __invoke(Interval $interval): Collection
{
$now = new CarbonImmutable;

return $this->connection()->table('pulse_outgoing_requests')
->selectRaw('MAX(`uri`) AS `uri`, COUNT(*) AS `count`, MAX(`duration`) AS `slowest`')
return $this->connection()->query()->select([
'count',
'slowest',
'uri' => fn (Builder $query) => $query->select('uri')
->from('pulse_outgoing_requests', as: 'child')
->whereRaw('`child`.`uri_hash` = `parent`.`uri_hash`')
->limit(1),
])->fromSub(fn (Builder $query) => $query->selectRaw('`uri_hash`, MAX(`duration`) as `slowest`, COUNT(*) as `count`')
->from('pulse_outgoing_requests')
->where('slow', true)
->where('date', '>', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('duration', '>=', $this->config->get('pulse.recorders.'.OutgoingRequests::class.'.threshold'))
->groupBy('uri_hash')
->orderByDesc('slowest')
->limit(101)
->orderByDesc('count')
->limit(101), as: 'parent')
->get();
}
}
2 changes: 1 addition & 1 deletion src/Queries/SlowRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __invoke(Interval $interval): Collection
->groupBy('route_hash')
->orderByDesc('slowest')
->orderByDesc('count')
->limit(100), as: 'parent')
->limit(101), as: 'parent')
->get()
->map(fn (stdClass $row) => (object) [
'route' => (string) $row->route,
Expand Down
3 changes: 2 additions & 1 deletion src/Recorders/OutgoingRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public function record(RequestInterface $request, CarbonImmutable $startedAt): ?
return new Entry($this->table, [
'uri' => $this->normalizeUri($request),
'date' => $startedAt->toDateTimeString(),
'duration' => $startedAt->diffInMilliseconds($endedAt),
'duration' => $duration = $startedAt->diffInMilliseconds($endedAt),
'user_id' => $this->pulse->authenticatedUserIdResolver(),
'slow' => $duration >= $this->config->get('pulse.recorders.'.static::class.'.threshold'),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Recorders/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function record(Carbon $startedAt, Request $request, Response $response):
'route' => $request->method().' '.$path,
'duration' => $duration = $startedAt->diffInMilliseconds(),
'user_id' => $this->pulse->authenticatedUserIdResolver(),
'slow' => $duration >= $this->config->get('pulse.recorders.'.self::class.'.threshold'),
'slow' => $duration >= $this->config->get('pulse.recorders.'.static::class.'.threshold'),
]);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/Livewire/SlowOutgoingRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

it('renders slow outgoing requests', function () {
Pulse::ignore(fn () => DB::table('pulse_outgoing_requests')->insert([
['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.com', 'duration' => 1234],
['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.com', 'duration' => 2468],
['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.org', 'duration' => 123],
['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.org', 'duration' => 1234],
['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.com', 'duration' => 1234, 'slow' => true],
['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.com', 'duration' => 2468, 'slow' => true],
['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.org', 'duration' => 123, 'slow' => false],
['date' => '2000-01-02 03:04:05', 'uri' => 'GET http://example.org', 'duration' => 1234, 'slow' => true],
]));
Carbon::setTestNow('2000-01-02 03:04:15');

Expand Down