|
6 | 6 | from fastapi import FastAPI, Request, Response |
7 | 7 | from starlette.datastructures import Headers |
8 | 8 | from starlette.responses import StreamingResponse |
9 | | -from starlette.routing import Mount, Route, WebSocketRoute |
| 9 | +from starlette.routing import BaseRoute, Mount, Route, WebSocketRoute |
10 | 10 |
|
11 | 11 | from ols import config, constants, version |
12 | 12 | from ols.app import metrics, routers |
@@ -191,11 +191,24 @@ async def stream_response_body( |
191 | 191 |
|
192 | 192 | routers.include_routers(app) |
193 | 193 |
|
194 | | -app_routes_paths = [ |
195 | | - route.path |
196 | | - for route in app.routes |
197 | | - if isinstance(route, (Mount, Route, WebSocketRoute)) |
198 | | -] |
| 194 | + |
| 195 | +def _collect_app_route_paths(routes: list[BaseRoute]) -> list[str]: |
| 196 | + """Collect route paths for metrics middleware, including included routers.""" |
| 197 | + paths: list[str] = [] |
| 198 | + for route in routes: |
| 199 | + if hasattr(route, "effective_candidates"): |
| 200 | + for candidate in route.effective_candidates(): |
| 201 | + if hasattr(candidate, "effective_candidates"): |
| 202 | + paths.extend(_collect_app_route_paths([candidate])) |
| 203 | + elif path := getattr(candidate, "path", None): |
| 204 | + paths.append(path) |
| 205 | + continue |
| 206 | + if isinstance(route, (Mount, Route, WebSocketRoute)) and route.path is not None: |
| 207 | + paths.append(route.path) |
| 208 | + return paths |
| 209 | + |
| 210 | + |
| 211 | +app_routes_paths = _collect_app_route_paths(app.routes) |
199 | 212 |
|
200 | 213 | cleanup_offload_storage(config.ols_config.offload_storage_path) |
201 | 214 |
|
|
0 commit comments