Skip to content

Commit

Permalink
Distinguish Livewire requests
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Oct 30, 2023
1 parent 941f16e commit 6c81a06
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
13 changes: 11 additions & 2 deletions src/Queries/SlowRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,27 @@ public function __invoke(Interval $interval): Collection
->map(function (stdClass $row) use ($routes) {
[$method, $uri] = explode(' ', $row->route, 2);

preg_match('/(.*?)(?:\s\((.*)\))?$/', $uri, $matches);

[$uri, $via] = [$matches[1], $matches[2] ?? null];

$domain = Str::before($uri, '/');

if ($domain) {
$uri = '/'.Str::after($uri, '/');
}

$path = $uri === '/' ? $uri : ltrim($uri, '/');
if ($via) {
$action = 'via '.$via;
} else {
$path = $uri === '/' ? $uri : ltrim($uri, '/');
$action = ($route = $routes[$method][$domain.$path] ?? null) ? (string) $route->getActionName() : null;
}

return (object) [
'uri' => $domain.$uri,
'method' => $method,
'action' => ($route = $routes[$method][$domain.$path] ?? null) ? (string) $route->getActionName() : null,
'action' => $action,
'count' => (int) $row->count,
'slowest' => (int) $row->slowest,
];
Expand Down
28 changes: 12 additions & 16 deletions src/Recorders/Requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,27 @@ public function record(Carbon $startedAt, Request $request, Response $response):
}

$path = $route->getDomain().Str::start($route->uri(), '/');
$via = null;

if (! $this->shouldSample() || $this->shouldIgnore($path) || $this->shouldIgnoreLivewireRequest($request)) {
if ($route->named('*livewire.update')) {
$snapshot = json_decode($request->input('components.0.snapshot'));

if (isset($snapshot->memo->path)) {
$via = $path;
$path = Str::start($snapshot->memo->path, '/');
}
}

if (! $this->shouldSample() || $this->shouldIgnore($path)) {
return null;
}

return new Entry($this->table, [
'date' => $startedAt->toDateTimeString(),
'route' => $request->method().' '.$path,
'route' => $request->method().' '.$path.($via ? " ($via)" : ''),
'duration' => $duration = $startedAt->diffInMilliseconds(),
'user_id' => $this->pulse->authenticatedUserIdResolver(),
'slow' => $duration >= $this->config->get('pulse.recorders.'.static::class.'.threshold'),
]);
}

/**
* Determine whether any Livewire component updates should be ignored.
*/
protected function shouldIgnoreLivewireRequest(Request $request): bool
{
if (! ($route = $request->route()) instanceof Route || ! $route->named('*livewire.update')) {
return false;
}

return $request
->collect('components.*.snapshot')
->contains(fn ($snapshot) => $this->shouldIgnore(Str::start(json_decode($snapshot)->memo->path, '/')));
}
}

0 comments on commit 6c81a06

Please sign in to comment.