Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

s1gr1d
Copy link

@s1gr1d s1gr1d commented May 22, 2025

Which problem is this PR solving?

This is an upstream fix for getsentry/sentry-javascript#16211

Instead of adding / to non-existing routes, it sets the attribute to undefined, which makes it clear it's a non-existing route and not the index route.


A tricky thing is that the layers look the same if a non-existing route is requested or the root is requested: ['/', '/'].

  • /existing --> ['/', '/existing']
  • /non-existing --> ['/', '/']
  • / --> ['/', '/']

So in order to know if it was a 404 route, a utility function that does some additional checking was added.

@s1gr1d s1gr1d requested a review from a team as a code owner May 22, 2025 11:52
Copy link

linux-foundation-easycla bot commented May 22, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions bot requested review from JamieDanielson and pkanal May 22, 2025 11:52
@pichlermarc pichlermarc added bug Something isn't working priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect labels May 22, 2025
pichlermarc
pichlermarc previously approved these changes May 22, 2025
Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

LGTM! thank you for your contribution @s1gr1d 🙂

cc @raphael-theriault-swi, @JamieDanielson, @pkanal (component owners)

@pichlermarc
Copy link
Member

oh - looks like there are some lint errors, you can fix them by running npm run lint:fix and then pushing the resulting diff 🙂

Copy link

codecov bot commented May 22, 2025

Codecov Report

Attention: Patch coverage is 97.61905% with 1 line in your changes missing coverage. Please review.

Project coverage is 89.73%. Comparing base (2ac08ff) to head (006539c).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...opentelemetry-instrumentation-express/src/utils.ts 97.36% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2843      +/-   ##
==========================================
+ Coverage   89.69%   89.73%   +0.03%     
==========================================
  Files         185      186       +1     
  Lines        9034     9085      +51     
  Branches     1852     1875      +23     
==========================================
+ Hits         8103     8152      +49     
- Misses        931      933       +2     
Files with missing lines Coverage Δ
...try-instrumentation-express/src/instrumentation.ts 96.73% <100.00%> (-0.03%) ⬇️
...opentelemetry-instrumentation-express/src/utils.ts 97.19% <97.36%> (+0.09%) ⬆️

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pichlermarc pichlermarc dismissed their stale review May 22, 2025 14:25

failing tests

@raphael-theriault-swi raphael-theriault-swi self-requested a review May 22, 2025 17:19
@s1gr1d
Copy link
Author

s1gr1d commented May 23, 2025

I have to look into this again. I see another test is failing. It doesn't add http.route either when the route is / (but this should be added in this case).

@s1gr1d
Copy link
Author

s1gr1d commented May 23, 2025

The problem was that the layers look the same if a non-existing route is requested or the root is requested: ['/', '/'].

So in order to know if it was a 404 route, I added a utility function that does some additional checking. I made sure that the existing tests still work and added some more tests for the utility function.

Copy link
Member

@pichlermarc pichlermarc left a comment

Choose a reason for hiding this comment

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

Hi @s1gr1d, thanks for following up on this.

It seems that this PR is based on a commit that's a little over 2 months old. Would you mind rebasing it on top of main it so that we can see how this would fit in with the current state? 🙂

@s1gr1d s1gr1d force-pushed the sigrid/handle-express-404 branch from 6592b20 to 99a94d2 Compare May 23, 2025 12:52
@s1gr1d s1gr1d requested a review from pichlermarc May 23, 2025 12:53
@s1gr1d s1gr1d marked this pull request as draft May 26, 2025 13:09
@s1gr1d s1gr1d force-pushed the sigrid/handle-express-404 branch from 99a94d2 to 006539c Compare June 3, 2025 12:47
@s1gr1d s1gr1d marked this pull request as ready for review June 3, 2025 12:56
Comment on lines 271 to 275
if (constructedRoute.includes('/') &&
(constructedRoute.includes(',') ||
constructedRoute.includes('\\') ||
constructedRoute.includes('*') ||
constructedRoute.includes('['))) {
Copy link
Author

Choose a reason for hiding this comment

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

This condition is for considering regex routes (without this, the regex tests would fail). This should cover most cases.

There is a previous conversation about this: #2008 (comment)

@s1gr1d
Copy link
Author

s1gr1d commented Jun 4, 2025

@pichlermarc I rebased it and aligned the code so the "regex" tests don't fail anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:instrumentation-express priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants