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 koa router double patched on every request #873

Merged
merged 3 commits into from
Feb 24, 2020
Merged
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
65 changes: 20 additions & 45 deletions packages/datadog-plugin-koa/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,14 @@ function createWrapRegister (tracer, config) {
}
}

function createWrapRoutes (tracer, config) {
return function wrapRoutes (routes) {
return function routesWithTrace () {
const dispatch = routes.apply(this, arguments)
const dispatchWithTrace = function (ctx, next) {
if (!ctx.router) {
let router

Object.defineProperty(ctx, 'router', {
set (value) {
router = value

for (const layer of router.stack) {
wrapStack(layer)
}
},

get () {
return router
}
})
}

return dispatch.apply(this, arguments)
}
function createWrapRouterUse (tracer, config) {
return function wrapUse (use) {
return function useWithTrace () {
const router = use.apply(this, arguments)

dispatchWithTrace.router = dispatch.router
router.stack.forEach(wrapStack)

return dispatchWithTrace
return router
}
}
}
Expand All @@ -104,16 +83,22 @@ function wrapStack (layer) {
layer.stack = layer.stack.map(middleware => {
if (typeof middleware !== 'function') return middleware

middleware = middleware._dd_original || middleware

const wrappedMiddleware = wrapMiddleware(middleware)

return function (ctx, next) {
const handler = function (ctx, next) {
if (!ctx || !web.active(ctx.req)) return middleware.apply(this, arguments)

web.exitRoute(ctx.req)
web.enterRoute(ctx.req, layer.path)

return wrappedMiddleware.apply(this, arguments)
}

handler._dd_original = middleware

return handler
})
}

Expand Down Expand Up @@ -164,34 +149,24 @@ module.exports = [
name: '@koa/router',
versions: ['>=8'],
patch (Router, tracer, config) {
this.wrap(Router.prototype, 'routes', createWrapRoutes(tracer, config))
this.wrap(Router.prototype, 'middleware', createWrapRoutes(tracer, config))
},
unpatch (Router) {
this.unwrap(Router.prototype, 'routes')
this.unwrap(Router.prototype, 'middleware')
}
},
{
name: 'koa-router',
versions: ['>7'],
patch (Router, tracer, config) {
this.wrap(Router.prototype, 'routes', createWrapRoutes(tracer, config))
this.wrap(Router.prototype, 'middleware', createWrapRoutes(tracer, config))
this.wrap(Router.prototype, 'register', createWrapRegister(tracer, config))
this.wrap(Router.prototype, 'use', createWrapRouterUse(tracer, config))
},
unpatch (Router) {
this.unwrap(Router.prototype, 'routes')
this.unwrap(Router.prototype, 'middleware')
this.unwrap(Router.prototype, 'register')
this.unwrap(Router.prototype, 'use')
}
},
{
name: 'koa-router',
versions: ['7'],
versions: ['>=7'],
patch (Router, tracer, config) {
this.wrap(Router.prototype, 'register', createWrapRegister(tracer, config))
this.wrap(Router.prototype, 'use', createWrapRouterUse(tracer, config))
},
unpatch (Router) {
this.unwrap(Router.prototype, 'register')
this.unwrap(Router.prototype, 'use')
}
}
]