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): fix express instrumentation for v5 #4877

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/datadog-instrumentations/src/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function wrapResponseRender (render) {
}
}

addHook({ name: 'express', versions: ['>=4'] }, express => {
addHook({ name: 'express', versions: ['>=4 <5.0.0'] }, express => {
shimmer.wrap(express.application, 'handle', wrapHandle)
shimmer.wrap(express.Router, 'use', wrapRouterMethod)
shimmer.wrap(express.Router, 'route', wrapRouterMethod)
Expand All @@ -69,6 +69,16 @@ addHook({ name: 'express', versions: ['>=4'] }, express => {
return express
})

addHook({ name: 'express', versions: ['>=5.0.0'] }, express => {
shimmer.wrap(express.application, 'handle', wrapHandle)

shimmer.wrap(express.response, 'json', wrapResponseJson)
shimmer.wrap(express.response, 'jsonp', wrapResponseJson)
shimmer.wrap(express.response, 'render', wrapResponseRender)

return express
})

const queryParserReadCh = channel('datadog:query:read:finish')

function publishQueryParsedAndNext (req, res, next) {
Expand Down Expand Up @@ -129,7 +139,7 @@ addHook({ name: 'express', versions: ['>=4.0.0 <4.3.0'] }, express => {
return express
})

addHook({ name: 'express', versions: ['>=4.3.0'] }, express => {
addHook({ name: 'express', versions: ['>=4.3.0 <5.0.0'] }, express => {
shimmer.wrap(express.Router, 'process_params', wrapProcessParamsMethod(2))
return express
})
26 changes: 26 additions & 0 deletions packages/datadog-instrumentations/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
const lastIndex = arguments.length - 1
const name = original._name || original.name
const req = arguments[arguments.length > 3 ? 1 : 0]
// wrapParams(req)
const next = arguments[lastIndex]

if (typeof next === 'function') {
Expand Down Expand Up @@ -167,12 +168,37 @@
return wrapMethod
}

const processParamsStartCh = channel('datadog:express:process_params:start')
function wrapParams (req) {

Check failure on line 172 in packages/datadog-instrumentations/src/router.js

View workflow job for this annotation

GitHub Actions / lint

'wrapParams' is defined but never used
let params
Object.defineProperty(req, 'params', {
get () { return params },
set (val) {
params = val
if (val && processParamsStartCh.hasSubscribers) {
const abortController = new AbortController()

processParamsStartCh.publish({
req,
res: req?.res,
abortController,
params: req?.params
})
}
}
})
}

const wrapRouterMethod = createWrapRouterMethod('router')
const wrapExpressRouterMethod = createWrapRouterMethod('express')

addHook({ name: 'router', versions: ['>=1'] }, Router => {
shimmer.wrap(Router.prototype, 'use', wrapRouterMethod)
shimmer.wrap(Router.prototype, 'route', wrapRouterMethod)

shimmer.wrap(Router.prototype, 'use', wrapExpressRouterMethod)
shimmer.wrap(Router.prototype, 'route', wrapExpressRouterMethod)

return Router
})

Expand Down
Loading
Loading