Skip to content
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

fix(tracing-internal): Fix express middleware path param parsing #8668

Merged
merged 23 commits into from
Oct 16, 2023
Merged

fix(tracing-internal): Fix express middleware path param parsing #8668

merged 23 commits into from
Oct 16, 2023

Conversation

LubomirIgonda1
Copy link
Contributor

This PR fix usecase when path params are define in express middleware (.use) not in express endpoint route (.get, post, .etc).
Also posible releated for #3155

Solution does not fix case when layer.path is array or regexp.

Example:
Create endpoint with path param define in middleware layer route as

app.use( '/api', router.use( '/group/:pathParam', router.get('/endpoint', (req, res) => { return res.json() }) ) )

then send request on URL:

request URL: /api/group/123/endpoint

Old behavior

sentry build req._reconstructedRoute as:
/api/group/123/endpoint

New behavior

sentry build req._reconstructedRoute as:
/api/group/:pathParam/endpoint

Param _reconstructedRoute is used also for transaction name what is used as group attribute in sentry performance tracing section

@AbhiPrasad AbhiPrasad requested a review from Lms24 July 28, 2023 13:33
@AbhiPrasad
Copy link
Member

Hey @LubomirIgonda1, thanks for the PR! Could you add an integration test to https://github.com/getsentry/sentry-javascript/tree/develop/packages/node-integration-tests/suites/express to validate that this works?

You can also run yarn fix in the package to fix the lint errors. Thanks!

@LubomirIgonda1
Copy link
Contributor Author

@AbhiPrasad Hi I added test for middle layer route path. Let me know if it is OK. thx

@AbhiPrasad
Copy link
Member

Seems like there is some test failures

FAIL suites/express/multiple-routers/common-prefix/test.ts
  ✕ should construct correct urls with multiple routers. (836 ms)

Could you take a look?

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Lubomirlgonda1, thanks for opening this PR! It generally looks good to me and I think we should merge it once all tests pass but I still have some questions.

packages/tracing-internal/src/node/integrations/express.ts Outdated Show resolved Hide resolved
*
* @returns string in layer.route.path structure 'router/:pathParam' or undefined
*/
const extractOriginalRoute = ({ path, regexp, keys }: Layer): string | undefined => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure I understand what's going on here. Would you mind explaining when we actually run into the situation that we need to extract the route from the regex? Is it just for the case you mentioned in the PR description or could this happen otherwise, too?

I'm assuming the integration test you added already covers this function but if there's another situation where we run into this function being called (that you know of), would you mind adding another integration test for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I know the case is only when the middleware contains path param as I already mentioned in merge request. Should I add test for the function ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add test for the function ?

Not strictly required from my end but if you have the time, I'd appreciate a test for the function.

Copy link
Member

@AbhiPrasad AbhiPrasad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience sticking with this for so long @LubomirIgonda1!

@LubomirIgonda1
Copy link
Contributor Author

Hi I am used RegExp /d flag for getting match.indices but this feature is available from node 16. That is the reason why tests for node <16 fails. Can you let me know how shold I fix this case ? thank you

@mydea
Copy link
Member

mydea commented Sep 27, 2023

Hi I am used RegExp /d flag for getting match.indices but this feature is available from node 16. That is the reason why tests for node <16 fails. Can you let me know how shold I fix this case ? thank you

Sorry for the late feedback, kind of got lost in the meanwhile - we'd def. need a solution that works in all our supported node versions (8+ as of now) 😬 can you rewrite the regex to be Node 8+ compatible? Is there a way to do this without the d flag? 🤔

@LubomirIgonda1
Copy link
Contributor Author

Hi I am used RegExp /d flag for getting match.indices but this feature is available from node 16. That is the reason why tests for node <16 fails. Can you let me know how shold I fix this case ? thank you

Sorry for the late feedback, kind of got lost in the meanwhile - we'd def. need a solution that works in all our supported node versions (8+ as of now) 😬 can you rewrite the regex to be Node 8+ compatible? Is there a way to do this without the d flag? 🤔

Hi I used this package https://www.npmjs.com/package/regexp-match-indices as nodejs polyfill for regex d flag let me know if it is OK

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me know if it is OK

If possible, we'd like to avoid adding dependencies to our SDKs. Is there a way we can solve this without adding a dependency?

@LubomirIgonda1
Copy link
Contributor Author

let me know if it is OK

If possible, we'd like to avoid adding dependencies to our SDKs. Is there a way we can solve this without adding a dependency?

@Lms24 I don't think so. In this situation I see only two options. First is accept official polyfill as a new dependency or apply this fix only for nodejs 16+ with native regex d flag implementation and previous versions of node just let with current implementation.

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LubomirIgonda1 thanks for sticking with us!

or apply this fix only for nodejs 16+ with native regex d flag implementation and previous versions of node just let with current implementation

Talked with the team and we prefer this approach. We're currently trying to get rid of all 3rd party dependencies in the Node SDK (#9199) by vendoring them in. However, this polyfill would be quite big and I think it's totally fine for us to apply this fix only to Node 16+.

Let's try/catch the native /d regex flag and bail if it is not supported. This will require a Node version check in the tests but that's fine.

packages/tracing-internal/src/node/integrations/express.ts Outdated Show resolved Hide resolved
*
* @returns string in layer.route.path structure 'router/:pathParam' or undefined
*/
const extractOriginalRoute = ({ path, regexp, keys }: Layer): string | undefined => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add test for the function ?

Not strictly required from my end but if you have the time, I'd appreciate a test for the function.

@LubomirIgonda1
Copy link
Contributor Author

@Lms24 Hi i updated core logic to handle node 16 regex d flag feature in code and tests as well. Let me know if I missed something. thx

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sticking with us! Gonna merge this soon and we'll include it in the next release.

@Lms24 Lms24 merged commit 755ba73 into getsentry:develop Oct 16, 2023
70 checks passed
@AbhiPrasad
Copy link
Member

thanks for contributing @LubomirIgonda1!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants