-
Notifications
You must be signed in to change notification settings - Fork 31
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
Submounted routes use incorrect path in labels #40
Comments
Hello! I've faced the same issue. @staticmethod
def get_path_template(request: Request) -> Tuple[str, bool]:
for route in request.app.routes:
match, _ = route.matches(request.scope)
if match == Match.FULL:
return route.path, True
return request.url.path, False to something like @staticmethod
def get_path_template(request: Request) -> Tuple[str, bool]:
for route in request.app.routes:
if isinstance(route, Mount):
for inner_route in route.routes:
match, _ = inner_route.matches(request.scope)
if match == Match.FULL:
return route.path, True
else:
match, _ = route.matches(request.scope)
if match == Match.FULL:
return route.path, True
return request.url.path, False |
This snippet handles more cases (incl. inner mount objects): import dataclasses, copy
@dataclasses.dataclass
class RouteMatch:
name: str = ''
pattern: str = ''
def find_matched_route(scope: Scope, routes: typing.Iterable[BaseRoute]) -> RouteMatch | None:
for route in routes:
if isinstance(route, (Mount, Host)):
mount_match, child_scope = route.matches(scope)
if mount_match == Match.FULL:
scope.update(child_scope)
if match := find_matched_route(scope, route.routes):
return match
elif isinstance(route, Route):
match, child_scope = route.matches(scope)
if match == Match.FULL:
return RouteMatch(name=route.name, pattern=route.path_format)
scope_copy = copy.copy(scope) # scope will be mutated so make a copy!
route_match = find_matched_route(scope_copy, scope['app'].routes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When submounting routes, rather than the full path template being used, only the mount prefix is used.
Running the following app:
Then making the following requests:
Gives the following output (I only included one metric as an example, but it's the same for all of them). Note the label for the request to
localhost:8000/bar/baz
has a path label of/bar
.The text was updated successfully, but these errors were encountered: