Skip to content

Commit

Permalink
fix: keep original matched route for metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
basert committed Nov 15, 2024
1 parent 438f50d commit 15f5dd1
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ class App
*/
protected ?Route $route = null;

/**
* Matched Route
*
* During runtime $this->route might be overwritten with the wildcard route to keep custom functions working with
* paths not declared in the Router. Keep a copy of the original matched app route.
*/
protected ?Route $matchedRoute = null;

/**
* Wildcard route
* If set, this get's executed if no other route is matched
Expand Down Expand Up @@ -716,8 +724,7 @@ public function run(Request $request, Response $response): static
$attributes = [
'url.scheme' => $request->getProtocol(),
'http.request.method' => $request->getMethod(),
// use ->match(fresh: true) to get the matched internal route, not any wildcard route
'http.route' => $this->match($request, fresh: true)?->getPath(),
'http.route' => $this->matchedRoute?->getPath(),
'http.response.status_code' => $response->getStatusCode(),
];
$this->requestDuration->record($requestDuration, $attributes);
Expand Down Expand Up @@ -761,6 +768,7 @@ private function runInternal(Request $request, Response $response): static

$method = $request->getMethod();
$route = $this->match($request);
$this->matchedRoute = $route;
$groups = ($route instanceof Route) ? $route->getGroups() : [];

if (self::REQUEST_METHOD_HEAD == $method) {
Expand Down

0 comments on commit 15f5dd1

Please sign in to comment.