-
Notifications
You must be signed in to change notification settings - Fork 345
collect express endpoints #6271
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
base: master
Are you sure you want to change the base?
Changes from all commits
2c71048
ee03973
bf3ec5d
8975579
1ec7d81
aa1273e
819b4d5
4640c82
ccc72f2
315ed6d
5ca704f
bcd1d57
75bcf53
d5c5a67
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict' | ||
|
||
const tracer = require('dd-trace') | ||
tracer.init({ | ||
flushInterval: 0 | ||
}) | ||
|
||
const express = require('express') | ||
const app = express() | ||
|
||
// Basic routes | ||
app.get('/users', (_, res) => res.send('ok')) | ||
app.post('/users/', (_, res) => res.send('ok')) | ||
app.put('/users/:id', (_, res) => res.send('ok')) | ||
app.delete('/users/:id', (_, res) => res.send('ok')) | ||
app.patch('/users/:id/:name', (_, res) => res.send('ok')) | ||
app.options('/users/:id', (_, res) => res.send('ok')) | ||
|
||
// Additional methods | ||
app.trace('/trace-test', (_, res) => res.send('ok')) | ||
app.head('/head-test', (_, res) => res.send('ok')) | ||
app.connect('/connect-test', (_, res) => res.send('ok')) | ||
|
||
// Using app.route() | ||
app.route('/multi-method') | ||
.post((_, res) => res.send('ok')) | ||
.put((_, res) => res.send('ok')) | ||
.patch((_, res) => res.send('ok')) | ||
.all((_, res) => res.send('ok')) | ||
|
||
// All supported methods route | ||
app.all('/all-methods', async (_, res) => res.send('ok')) | ||
|
||
// Wildcard routes via array | ||
app.all(['/wildcard/*name'], (_, res) => res.send('ok')) | ||
|
||
// RegExp wildcard route | ||
app.all(/^\/login\/.*$/i, (req, res) => { | ||
res.send('ok') | ||
}) | ||
|
||
// Nested routes with Router | ||
const apiRouter = express.Router() | ||
apiRouter.put('/nested/:id', (_, res) => res.send('ok')) | ||
app.use('/v1', apiRouter) | ||
|
||
// Add endpoint during runtime | ||
setTimeout(() => { | ||
// Deeply nested routes | ||
const deepRouter = express.Router() | ||
deepRouter.get('/nested', (_, res) => res.send('ok')) | ||
const subRouter = express.Router() | ||
subRouter.get('/deep', (_, res) => res.send('ok')) | ||
subRouter.post('/deep/:id', (_, res) => res.send('ok')) | ||
deepRouter.use('/sub', subRouter) | ||
app.use('/api', deepRouter) | ||
app.get('/later', (_, res) => res.send('ok')) | ||
}, 1_000) | ||
|
||
const server = app.listen(0, '127.0.0.1', () => { | ||
const port = server.address().port | ||
process.send({ port }) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,10 @@ const shimmer = require('../../datadog-shimmer') | |
const { addHook, channel } = require('./helpers/instrument') | ||
const tracingChannel = require('dc-polyfill').tracingChannel | ||
|
||
const METHODS = require('http').METHODS.map(v => v.toLowerCase()) | ||
|
||
const handleChannel = channel('apm:express:request:handle') | ||
const routeAddedChannel = channel('apm:express:add:route') | ||
|
||
function wrapHandle (handle) { | ||
return function handleWithTrace (req, res) { | ||
|
@@ -57,8 +60,125 @@ function wrapResponseRender (render) { | |
} | ||
} | ||
|
||
function wrapAppAll (all) { | ||
return function wrappedAll (path) { | ||
if (routeAddedChannel.hasSubscribers) { | ||
const paths = Array.isArray(path) ? path : [path] | ||
|
||
for (const p of paths) { | ||
routeAddedChannel.publish({ | ||
method: '*', | ||
path: p instanceof RegExp ? p.toString() : p | ||
}) | ||
} | ||
} | ||
|
||
return all.apply(this, arguments) | ||
} | ||
} | ||
|
||
// Wrap app.route() to instrument Route object | ||
function wrapAppRoute (route) { | ||
return function wrappedRoute (path) { | ||
const routeObj = route.apply(this, arguments) | ||
|
||
if (routeAddedChannel.hasSubscribers && typeof path === 'string') { | ||
// Wrap the .all() first | ||
if (typeof routeObj.all === 'function') { | ||
shimmer.wrap(routeObj, 'all', (original) => function wrappedRouteAll () { | ||
routeAddedChannel.publish({ | ||
method: '*', | ||
path | ||
}) | ||
|
||
return original.apply(this, arguments) | ||
}) | ||
} | ||
|
||
// Wrap each HTTP method | ||
METHODS.forEach(method => { | ||
if (typeof routeObj[method] === 'function') { | ||
shimmer.wrap(routeObj, method, (original) => function wrapMethod () { | ||
routeAddedChannel.publish({ | ||
method, | ||
path | ||
}) | ||
|
||
return original.apply(this, arguments) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
return routeObj | ||
} | ||
} | ||
|
||
function joinPath (base, path) { | ||
if (!base || base === '/') return path || '/' | ||
if (!path || path === '/') return base | ||
return base + path | ||
} | ||
|
||
function wrapAppUse (use) { | ||
return function wrappedUse () { | ||
if (arguments.length < 2) { | ||
return use.apply(this, arguments) | ||
} | ||
|
||
// Check if second argument has a stack (likely a router) | ||
const mountPath = arguments[0] | ||
const router = arguments[1] | ||
|
||
if (typeof mountPath === 'string' && router?.stack?.length && routeAddedChannel.hasSubscribers) { | ||
collectRoutesFromRouter(router, mountPath) | ||
} | ||
|
||
return use.apply(this, arguments) | ||
} | ||
} | ||
|
||
function collectRoutesFromRouter (router, prefix) { | ||
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. If this operates on the router, doesn't it also apply to the |
||
if (!router?.stack?.length) return | ||
|
||
router.stack.forEach(layer => { | ||
if (layer.route) { | ||
// This layer has a direct route | ||
const route = layer.route | ||
const fullPath = joinPath(prefix, route.path) | ||
|
||
Object.keys(route.methods).forEach(method => { | ||
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. I think this could be done in just one step with a condition only on the method which would result in simpler code. Same for the similar code above. |
||
if (route.methods[method] && method !== '_all') { | ||
routeAddedChannel.publish({ | ||
method, | ||
path: fullPath | ||
}) | ||
} | ||
}) | ||
|
||
if (route.methods._all) { | ||
routeAddedChannel.publish({ | ||
method: '*', | ||
path: fullPath | ||
}) | ||
} | ||
} else if (layer.handle?.stack?.length) { | ||
// This layer contains a nested router | ||
// Prefer matchers (from router.js) when available to resolve the exact mount path | ||
const matchers = layer.ddMatchers | ||
const mountPath = (Array.isArray(matchers) && matchers.length) ? matchers[0].path : '' | ||
|
||
const nestedPrefix = joinPath(prefix, mountPath) | ||
collectRoutesFromRouter(layer.handle, nestedPrefix) | ||
} | ||
}) | ||
} | ||
|
||
addHook({ name: 'express', versions: ['>=4'] }, express => { | ||
shimmer.wrap(express.application, 'handle', wrapHandle) | ||
shimmer.wrap(express.application, 'all', wrapAppAll) | ||
shimmer.wrap(express.application, 'route', wrapAppRoute) | ||
shimmer.wrap(express.application, 'use', wrapAppUse) | ||
|
||
shimmer.wrap(express.response, 'json', wrapResponseJson) | ||
shimmer.wrap(express.response, 'jsonp', wrapResponseJson) | ||
|
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.
Is using
METHODS
needed here? Aren't all methods on the route HTTP methods?