Skip to content

Commit

Permalink
fix mongoose instru not supporting deferred callback
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-id authored Feb 9, 2024
1 parent 12f4bbc commit 012a485
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions packages/datadog-instrumentations/src/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,25 @@ addHook({
})

let callbackWrapped = false
const lastArgumentIndex = arguments.length - 1

if (typeof arguments[lastArgumentIndex] === 'function') {
// is a callback, wrap it to execute finish()
shimmer.wrap(arguments, lastArgumentIndex, originalCb => {
return function () {
finish()
const wrapCallbackIfExist = (args) => {
const lastArgumentIndex = args.length - 1

return originalCb.apply(this, arguments)
}
})
if (typeof args[lastArgumentIndex] === 'function') {
// is a callback, wrap it to execute finish()
shimmer.wrap(args, lastArgumentIndex, originalCb => {
return function () {
finish()

return originalCb.apply(this, arguments)
}
})

callbackWrapped = true
callbackWrapped = true
}
}

Check failure on line 99 in packages/datadog-instrumentations/src/mongoose.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
wrapCallbackIfExist(arguments)

return asyncResource.runInAsyncScope(() => {
startCh.publish({
Expand All @@ -106,8 +111,14 @@ addHook({
if (!callbackWrapped) {
shimmer.wrap(res, 'exec', originalExec => {
return function wrappedExec () {
wrapCallbackIfExist(arguments)

const execResult = originalExec.apply(this, arguments)

if (callbackWrapped || typeof execResult?.then !== 'function') {
return execResult
}

// wrap them method, wrap resolve and reject methods
shimmer.wrap(execResult, 'then', originalThen => {
return function wrappedThen () {
Expand Down

0 comments on commit 012a485

Please sign in to comment.