File "SlowRequests.php"
Full Path: /var/www/drive/laravel/pulse/src/Recorders/SlowRequests.php
File size: 2 KB
MIME-type: text/x-php
Charset: utf-8
<?php
namespace Laravel\Pulse\Recorders;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Carbon;
use Laravel\Pulse\Concerns\ConfiguresAfterResolving;
use Laravel\Pulse\Pulse;
use Symfony\Component\HttpFoundation\Response;
/**
* @internal
*/
class SlowRequests
{
use Concerns\Ignores,
Concerns\LivewireRoutes,
Concerns\Sampling,
Concerns\Thresholds,
ConfiguresAfterResolving;
/**
* Create a new recorder instance.
*/
public function __construct(
protected Pulse $pulse,
) {
//
}
/**
* Register the recorder.
*/
public function register(callable $record, Application $app): void
{
$this->afterResolving(
$app,
Kernel::class,
fn (Kernel $kernel) => $kernel->whenRequestLifecycleIsLongerThan(-1, $record) // @phpstan-ignore method.notFound
);
}
/**
* Record the request.
*/
public function record(Carbon $startedAt, Request $request, Response $response): void
{
if (! $request->route() instanceof Route || ! $this->shouldSample()) {
return;
}
[$path, $via] = $this->resolveRoutePath($request);
if (
$this->shouldIgnore($path) ||
$this->underThreshold($duration = ((int) $startedAt->diffInMilliseconds()), $path)
) {
return;
}
$this->pulse->record(
type: 'slow_request',
key: json_encode([$request->method(), $path, $via], flags: JSON_THROW_ON_ERROR),
value: $duration,
timestamp: $startedAt,
)->max()->count();
if ($userId = $this->pulse->resolveAuthenticatedUserId()) {
$this->pulse->record(
type: 'slow_user_request',
key: (string) $userId,
timestamp: $startedAt,
)->count();
}
}
}