-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat: Added instrumentation support for Express 5 beta (#2476)
This will be experimental until express@5.0.0 is generally available
Showing
18 changed files
with
1,425 additions
and
1,520 deletions.
There are no files selected for viewing
This file contains 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
const { makeRequest, setup } = require('./utils') | ||
const { test } = require('tap') | ||
|
||
test('should properly track async handlers', (t) => { | ||
setup(t) | ||
const { app } = t.context | ||
const mwTimeout = 20 | ||
const handlerTimeout = 25 | ||
|
||
app.use(async function (req, res, next) { | ||
await new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve() | ||
}, mwTimeout) | ||
}) | ||
next() | ||
}) | ||
app.use('/test', async function handler(req, res) { | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, handlerTimeout) | ||
}) | ||
res.send('ok') | ||
}) | ||
|
||
runTest(t, '/test', (tx) => { | ||
const [children] = tx.trace.root.children | ||
const [mw, handler] = children.children | ||
t.ok( | ||
Math.ceil(mw.getDurationInMillis()) >= mwTimeout, | ||
`should be at least ${mwTimeout} for middleware segment` | ||
) | ||
t.ok( | ||
Math.ceil(handler.getDurationInMillis()) >= handlerTimeout, | ||
`should be at least ${handlerTimeout} for handler segment` | ||
) | ||
t.end() | ||
}) | ||
}) | ||
|
||
test('should properly handle errors in async handlers', (t) => { | ||
setup(t) | ||
const { app } = t.context | ||
|
||
app.use(() => { | ||
return Promise.reject(new Error('whoops i failed')) | ||
}) | ||
app.use('/test', function handler(req, res) { | ||
t.fail('should not call handler on error') | ||
res.send('ok') | ||
}) | ||
// eslint-disable-next-line no-unused-vars | ||
app.use(function (error, req, res, next) { | ||
res.status(400).end() | ||
}) | ||
|
||
runTest(t, '/test', (tx) => { | ||
const errors = tx.agent.errors.traceAggregator.errors | ||
t.equal(errors.length, 1) | ||
const [error] = errors | ||
t.equal(error[2], 'HttpError 400', 'should return 400 from custom error handler') | ||
t.end() | ||
}) | ||
}) | ||
|
||
function runTest(t, endpoint, callback) { | ||
const { agent, app } = t.context | ||
|
||
agent.on('transactionFinished', callback) | ||
|
||
const server = app.listen(function () { | ||
makeRequest(this, endpoint, function (response) { | ||
response.resume() | ||
}) | ||
}) | ||
|
||
t.teardown(() => { | ||
server.close() | ||
}) | ||
} |
This file contains 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
const http = require('http') | ||
const helper = require('../../lib/agent_helper') | ||
const semver = require('semver') | ||
|
||
function isExpress5() { | ||
const { version } = require('express/package') | ||
// TODO: change to 5.0.0 when officially released | ||
return semver.gte(version, '5.0.0-beta.3') | ||
} | ||
|
||
function makeRequest(server, path, callback) { | ||
const port = server.address().port | ||
http.request({ port: port, path: path }, callback).end() | ||
} | ||
|
||
function setup(t, config = {}) { | ||
t.context.agent = helper.instrumentMockedAgent(config) | ||
t.context.isExpress5 = isExpress5() | ||
|
||
t.context.express = require('express') | ||
t.context.app = t.context.express() | ||
t.teardown(() => { | ||
helper.unloadAgent(t.context.agent) | ||
}) | ||
} | ||
|
||
module.exports = { | ||
isExpress5, | ||
makeRequest, | ||
setup | ||
} |