-
Notifications
You must be signed in to change notification settings - Fork 345
Removing hook for express v5 router #6272
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
crysmags
wants to merge
7
commits into
master
Choose a base branch
from
crysmags/er-work
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+66
−42
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2e48abd
removing hook for express v5 router
crysmags 25c6637
separating v4 router instrumentation to its own addhook
crysmags 58f6859
add router subscription for code origin
crysmags e2e6ee4
fixinf appsec tests to include router plugin on agent load
crysmags d03b10b
updating appsec -lfi to also block requests for router middleware
crysmags 2ef48b6
removing mistake
crysmags 06991b7
asm tests now check for right amount of traces - only router middlewa…
crysmags File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -22,6 +22,16 @@ class ExpressCodeOriginForSpansPlugin extends Plugin { | |||||||
if (layerTags.has(layer)) return | ||||||||
layerTags.set(layer, entryTags(topOfStackFunc)) | ||||||||
}) | ||||||||
this.addSub('apm:router:middleware:enter', ({ req, layer }) => { | ||||||||
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.
Suggested change
|
||||||||
const tags = layerTags.get(layer) | ||||||||
if (!tags) return | ||||||||
web.getContext(req).span?.addTags(tags) | ||||||||
}) | ||||||||
|
||||||||
this.addSub('apm:router:route:added', ({ topOfStackFunc, layer }) => { | ||||||||
if (layerTags.has(layer)) return | ||||||||
layerTags.set(layer, entryTags(topOfStackFunc)) | ||||||||
}) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -40,7 +40,7 @@ describe('Plugin', () => { | |||||||||
|
||||||||||
describe('without http', () => { | ||||||||||
before(() => { | ||||||||||
return agent.load('express', { client: false }) | ||||||||||
return agent.load(['express', 'router'], [{ client: false }, {}]) | ||||||||||
}) | ||||||||||
|
||||||||||
after(() => { | ||||||||||
|
@@ -94,7 +94,7 @@ describe('Plugin', () => { | |||||||||
|
||||||||||
describe('without configuration', () => { | ||||||||||
before(() => { | ||||||||||
return agent.load(['express', 'http'], [{}, { client: false }]) | ||||||||||
return agent.load(['express', 'http', 'router'], [{}, { client: false }, {}]) | ||||||||||
}) | ||||||||||
|
||||||||||
after(() => { | ||||||||||
|
@@ -230,6 +230,9 @@ describe('Plugin', () => { | |||||||||
const spans = sort(traces[0]) | ||||||||||
const isExpress4 = semver.intersects(version, '<5.0.0') | ||||||||||
let index = 0 | ||||||||||
const whichMiddleware = semver.intersects(version, '<5.0.0') | ||||||||||
? 'express' | ||||||||||
: 'router' | ||||||||||
Comment on lines
+233
to
+235
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. We have this
Suggested change
|
||||||||||
|
||||||||||
const rootSpan = spans[index++] | ||||||||||
expect(rootSpan).to.have.property('resource', 'GET /app/user/:id') | ||||||||||
|
@@ -251,31 +254,31 @@ describe('Plugin', () => { | |||||||||
} | ||||||||||
|
||||||||||
expect(spans[index]).to.have.property('resource', 'named') | ||||||||||
expect(spans[index]).to.have.property('name', 'express.middleware') | ||||||||||
expect(spans[index]).to.have.property('name', `${whichMiddleware}.middleware`) | ||||||||||
expect(spans[index].parent_id.toString()).to.equal(rootSpan.span_id.toString()) | ||||||||||
expect(spans[index].meta).to.have.property('component', 'express') | ||||||||||
expect(spans[index].meta).to.have.property('component', whichMiddleware) | ||||||||||
index++ | ||||||||||
|
||||||||||
expect(spans[index]).to.have.property('resource', 'router') | ||||||||||
expect(spans[index]).to.have.property('name', 'express.middleware') | ||||||||||
expect(spans[index]).to.have.property('name', `${whichMiddleware}.middleware`) | ||||||||||
expect(spans[index].parent_id.toString()).to.equal(rootSpan.span_id.toString()) | ||||||||||
expect(spans[index].meta).to.have.property('component', 'express') | ||||||||||
expect(spans[index].meta).to.have.property('component', whichMiddleware) | ||||||||||
index++ | ||||||||||
|
||||||||||
if (isExpress4) { | ||||||||||
expect(spans[index].resource).to.match(/^bound\s.*$/) | ||||||||||
} else { | ||||||||||
expect(spans[index]).to.have.property('resource', 'handle') | ||||||||||
} | ||||||||||
expect(spans[index]).to.have.property('name', 'express.middleware') | ||||||||||
expect(spans[index]).to.have.property('name', `${whichMiddleware}.middleware`) | ||||||||||
expect(spans[index].parent_id.toString()).to.equal(spans[index - 1].span_id.toString()) | ||||||||||
expect(spans[index].meta).to.have.property('component', 'express') | ||||||||||
expect(spans[index].meta).to.have.property('component', whichMiddleware) | ||||||||||
index++ | ||||||||||
|
||||||||||
expect(spans[index]).to.have.property('resource', '<anonymous>') | ||||||||||
expect(spans[index]).to.have.property('name', 'express.middleware') | ||||||||||
expect(spans[index]).to.have.property('name', `${whichMiddleware}.middleware`) | ||||||||||
expect(spans[index].parent_id.toString()).to.equal(spans[index - 1].span_id.toString()) | ||||||||||
expect(spans[index].meta).to.have.property('component', 'express') | ||||||||||
expect(spans[index].meta).to.have.property('component', whichMiddleware) | ||||||||||
|
||||||||||
expect(index).to.equal(spans.length - 1) | ||||||||||
}) | ||||||||||
|
@@ -314,13 +317,16 @@ describe('Plugin', () => { | |||||||||
const spans = sort(traces[0]) | ||||||||||
|
||||||||||
const breakingSpanIndex = semver.intersects(version, '<5.0.0') ? 3 : 1 | ||||||||||
const whichMiddleware = semver.intersects(version, '<5.0.0') | ||||||||||
? 'express' | ||||||||||
: 'router' | ||||||||||
|
||||||||||
expect(spans[0]).to.have.property('resource', 'GET /user/:id') | ||||||||||
expect(spans[0]).to.have.property('name', 'express.request') | ||||||||||
expect(spans[0].meta).to.have.property('component', 'express') | ||||||||||
expect(spans[breakingSpanIndex]).to.have.property('resource', 'breaking') | ||||||||||
expect(spans[breakingSpanIndex]).to.have.property('name', 'express.middleware') | ||||||||||
expect(spans[breakingSpanIndex].meta).to.have.property('component', 'express') | ||||||||||
expect(spans[breakingSpanIndex]).to.have.property('name', `${whichMiddleware}.middleware`) | ||||||||||
expect(spans[breakingSpanIndex].meta).to.have.property('component', whichMiddleware) | ||||||||||
}) | ||||||||||
.then(done) | ||||||||||
.catch(done) | ||||||||||
|
@@ -359,12 +365,15 @@ describe('Plugin', () => { | |||||||||
.assertSomeTraces(traces => { | ||||||||||
const spans = sort(traces[0]) | ||||||||||
const errorSpanIndex = semver.intersects(version, '<5.0.0') ? 4 : 2 | ||||||||||
const whichMiddleware = semver.intersects(version, '<5.0.0') | ||||||||||
? 'express' | ||||||||||
: 'router' | ||||||||||
|
||||||||||
expect(spans[0]).to.have.property('name', 'express.request') | ||||||||||
expect(spans[errorSpanIndex]).to.have.property('name', 'express.middleware') | ||||||||||
expect(spans[errorSpanIndex]).to.have.property('name', `${whichMiddleware}.middleware`) | ||||||||||
expect(spans[errorSpanIndex].meta).to.have.property(ERROR_TYPE, error.name) | ||||||||||
expect(spans[0].meta).to.have.property('component', 'express') | ||||||||||
expect(spans[errorSpanIndex].meta).to.have.property('component', 'express') | ||||||||||
expect(spans[errorSpanIndex].meta).to.have.property('component', whichMiddleware) | ||||||||||
}) | ||||||||||
.then(done) | ||||||||||
.catch(done) | ||||||||||
|
@@ -1176,6 +1185,9 @@ describe('Plugin', () => { | |||||||||
.assertSomeTraces(traces => { | ||||||||||
const spans = sort(traces[0]) | ||||||||||
const secondErrorIndex = spans.length - 2 | ||||||||||
const whichMiddleware = semver.intersects(version, '<5.0.0') | ||||||||||
? 'express' | ||||||||||
: 'router' | ||||||||||
|
||||||||||
expect(spans[0]).to.have.property('error', 1) | ||||||||||
expect(spans[0].meta).to.have.property(ERROR_TYPE, error.name) | ||||||||||
|
@@ -1186,7 +1198,7 @@ describe('Plugin', () => { | |||||||||
expect(spans[secondErrorIndex].meta).to.have.property(ERROR_TYPE, error.name) | ||||||||||
expect(spans[secondErrorIndex].meta).to.have.property(ERROR_MESSAGE, error.message) | ||||||||||
expect(spans[secondErrorIndex].meta).to.have.property(ERROR_STACK, error.stack) | ||||||||||
expect(spans[secondErrorIndex].meta).to.have.property('component', 'express') | ||||||||||
expect(spans[secondErrorIndex].meta).to.have.property('component', whichMiddleware) | ||||||||||
}) | ||||||||||
.then(done) | ||||||||||
.catch(done) | ||||||||||
|
@@ -1448,12 +1460,12 @@ describe('Plugin', () => { | |||||||||
|
||||||||||
describe('with configuration', () => { | ||||||||||
before(() => { | ||||||||||
return agent.load(['express', 'http'], [{ | ||||||||||
return agent.load(['express', 'http', 'router'], [{ | ||||||||||
service: 'custom', | ||||||||||
validateStatus: code => code < 400, | ||||||||||
headers: ['User-Agent'], | ||||||||||
blocklist: ['/health'] | ||||||||||
}, { client: false }]) | ||||||||||
}, { client: false }, {}]) | ||||||||||
}) | ||||||||||
|
||||||||||
after(() => { | ||||||||||
|
@@ -1576,9 +1588,9 @@ describe('Plugin', () => { | |||||||||
|
||||||||||
describe('with configuration for middleware disabled', () => { | ||||||||||
before(() => { | ||||||||||
return agent.load(['express', 'http'], [{ | ||||||||||
return agent.load(['express', 'http', 'router'], [{ | ||||||||||
middleware: false | ||||||||||
}, { client: false }]) | ||||||||||
}, { client: false }, { middleware: false }]) | ||||||||||
}) | ||||||||||
|
||||||||||
after(() => { | ||||||||||
|
@@ -1594,8 +1606,8 @@ describe('Plugin', () => { | |||||||||
|
||||||||||
let span | ||||||||||
|
||||||||||
app.use((req, res, next) => { | ||||||||||
span = tracer.scope().active() | ||||||||||
app.use(async (req, res, next) => { | ||||||||||
span = await tracer.scope().active() | ||||||||||
next() | ||||||||||
}) | ||||||||||
|
||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
I see what you're trying to do here, but I believe there's an issue with this change.
By removing the
addHook
blocks that scoped the wrapping logic to specific Express versions, we're now applying the same wrapping logic toexpress.Router
regardless of whether it's Express 4 or 5. This isn't safe: in Express 5, Router is a constructor and the wrapping should happen on its prototype, while in Express 4 it's not, so wrapping it directly is necessary.Applying the same wrap to both versions lead to runtime errors, as seen in the tests.
If the goal is to avoid wrapping
express.Router
entirely in Express 5, we should retain the version-specificaddHook
blocks and simply omit the wrapping for Router in the>=5.0.0
block.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.
@CarlesDD I believe I addressed your issue, I have added in an
addHook
block for express v4 with a comment as well. This is will make it more explicit that for v4 we are wrapping the router and in v5 we will fall to the router instrumentation to wrap the calls.