From 15f5dd17162b166345e0c6598e49670084e7281e Mon Sep 17 00:00:00 2001 From: Fabian Gruber Date: Fri, 15 Nov 2024 09:35:10 +0100 Subject: [PATCH] fix: keep original matched route for metrics --- src/App.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/App.php b/src/App.php index f4673813..b8951282 100755 --- a/src/App.php +++ b/src/App.php @@ -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 @@ -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); @@ -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) {