-
Notifications
You must be signed in to change notification settings - Fork 619
fix(express): Ensure 404 routes don't attach route attribute #2843
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
Changes from 4 commits
214b126
726cdcf
1df9019
006539c
2cb552a
2b9dd88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,3 +213,91 @@ | |
|
|
||
| return; | ||
| }; | ||
|
|
||
| export function getConstructedRoute(req: { | ||
| originalUrl: PatchedRequest['originalUrl']; | ||
| [_LAYERS_STORE_PROPERTY]?: string[]; | ||
| }) { | ||
| const layersStore: string[] = Array.isArray(req[_LAYERS_STORE_PROPERTY]) | ||
| ? (req[_LAYERS_STORE_PROPERTY] as string[]) | ||
| : []; | ||
|
|
||
| const meaningfulPaths = layersStore.filter( | ||
| path => path !== '/' && path !== '/*' | ||
| ); | ||
|
|
||
| if (meaningfulPaths.length === 1 && meaningfulPaths[0] === '*') { | ||
| return '*'; | ||
| } | ||
|
|
||
| // Join parts and remove duplicate slashes | ||
| return meaningfulPaths.join('').replace(/\/{2,}/g, '/'); | ||
| } | ||
|
|
||
| /** | ||
| * Extracts the actual matched route from Express request for OpenTelemetry instrumentation. | ||
| * Returns the route that should be used as the http.route attribute. | ||
| * | ||
| * @param req - The Express request object with layers store | ||
| * @param layersStoreProperty - The property name where layer paths are stored | ||
| * @returns The matched route string or undefined if no valid route is found | ||
| */ | ||
| export function getActualMatchedRoute(req: { | ||
| originalUrl: PatchedRequest['originalUrl']; | ||
| [_LAYERS_STORE_PROPERTY]?: string[]; | ||
| }): string | undefined { | ||
| const layersStore: string[] = Array.isArray(req[_LAYERS_STORE_PROPERTY]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: same here |
||
| ? (req[_LAYERS_STORE_PROPERTY] as string[]) | ||
| : []; | ||
|
|
||
| // If no layers are stored, no route can be determined | ||
| if (layersStore.length === 0) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // Handle root path case - if all paths are root, only return root if originalUrl is also root | ||
| // The layer store also includes root paths in case a non-existing url was requested | ||
| if (layersStore.every(path => path === '/')) { | ||
| return req.originalUrl === '/' ? '/' : undefined; | ||
| } | ||
|
|
||
| const constructedRoute = getConstructedRoute(req); | ||
| if (constructedRoute === '*') { | ||
| return constructedRoute; | ||
| } | ||
|
|
||
| // For RegExp routes or route arrays, return the constructed route | ||
| // This handles the case where the route is defined using RegExp or an array | ||
| if (constructedRoute.includes('/') && | ||
| (constructedRoute.includes(',') || | ||
| constructedRoute.includes('\\') || | ||
| constructedRoute.includes('*') || | ||
| constructedRoute.includes('['))) { | ||
|
||
| return constructedRoute; | ||
| } | ||
|
|
||
| // Ensure route starts with '/' if it doesn't already | ||
| const normalizedRoute = constructedRoute.startsWith('/') | ||
| ? constructedRoute | ||
| : `/${constructedRoute}`; | ||
|
|
||
| // Validate that this appears to be a matched route | ||
| // A route is considered matched if: | ||
| // 1. We have a constructed route | ||
| // 2. The original URL matches or starts with our route pattern | ||
| const isValidRoute = | ||
| normalizedRoute.length > 0 && | ||
| (req.originalUrl === normalizedRoute || | ||
| req.originalUrl.startsWith(normalizedRoute) || | ||
| isRoutePattern(normalizedRoute)); | ||
|
|
||
| return isValidRoute ? normalizedRoute : undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a route contains parameter patterns (e.g., :id, :userId) | ||
| * which are valid even if they don't exactly match the original URL | ||
| */ | ||
| function isRoutePattern(route: string): boolean { | ||
| return route.includes(':') || route.includes('*'); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
_LAYERS_STORE_PROPERTYis set only instoreLayerPathfunction. Meaning that if defined it must be an array. So this const assignment could be simplified to.I see you added a test specifically for a scenario where that property is defined with a non-array value. Probably to avoid a case where another piece of code outside the instrumentation modifies the property. If you want the extra safety of
Array.isArrayit's okay but there is no need to cast types. TS resolves the type properly from the function signature.NOTE: maybe if the instrumentation uses a
Symbolinstead of a string it will ensure no other code could change the layer store