Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

refactor: Updated koa instrumentation to construct specs at instrumentation #171

Merged
merged 2 commits into from
Feb 28, 2024
Merged
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
2 changes: 1 addition & 1 deletion THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ This product includes source derived from [lockfile-lint](https://github.com/lir

### newrelic

This product includes source derived from [newrelic](https://github.com/newrelic/node-newrelic) ([v11.0.0](https://github.com/newrelic/node-newrelic/tree/v11.0.0)), distributed under the [Apache-2.0 License](https://github.com/newrelic/node-newrelic/blob/v11.0.0/LICENSE):
This product includes source derived from [newrelic](https://github.com/newrelic/node-newrelic) ([v11.11.0](https://github.com/newrelic/node-newrelic/tree/v11.11.0)), distributed under the [Apache-2.0 License](https://github.com/newrelic/node-newrelic/blob/v11.11.0/LICENSE):

```
Apache License
Expand Down
29 changes: 19 additions & 10 deletions lib/instrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ module.exports = function initialize(shim, Koa) {

shim.setFramework(shim.KOA)

shim.wrapMiddlewareMounter(proto, 'use', wrapMiddleware)
shim.wrapMiddlewareMounter(
proto,
'use',
new shim.specs.MiddlewareMounterSpec({
wrapper: wrapMiddleware
})
)
shim.wrapReturn(proto, 'createContext', wrapCreateContext)

// The application is used to handle unhandled errors in the application. We
Expand Down Expand Up @@ -50,15 +56,18 @@ function wrapMiddleware(shim, middleware) {
)
}

return shim.recordMiddleware(middleware, {
type: shim.MIDDLEWARE,
promise: true,
appendPath: true,
next: shim.LAST,
req: function getReq(shim, fn, fnName, args) {
return args[0] && args[0].req
}
})
return shim.recordMiddleware(
middleware,
new shim.specs.MiddlewareSpec({
type: shim.MIDDLEWARE,
promise: true,
appendPath: true,
next: shim.LAST,
req: function getReq(shim, fn, fnName, args) {
return args[0] && args[0].req
}
})
)
}

function wrapCreateContext(shim, fn, fnName, context) {
Expand Down
22 changes: 12 additions & 10 deletions lib/route-instrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ module.exports = function instrumentRoute(shim, route) {
shim.wrap(route, method, function wrapMethod(shim, methodFn) {
return function wrappedMethod() {
const middleware = methodFn.apply(route, arguments)
return shim.recordMiddleware(middleware, {
type: shim.MIDDLEWARE,
route: arguments[0],
next: shim.LAST,
name: shim.getName(arguments[1]),
promise: true,
req: function getReq(shim, fn, fnName, args) {
return args[0] && args[0].req
}
})
return shim.recordMiddleware(
middleware,
new shim.specs.MiddlewareSpec({
route: arguments[0],
next: shim.LAST,
name: shim.getName(arguments[1]),
promise: true,
req: function getReq(shim, fn, fnName, args) {
return args[0] && args[0].req
}
})
)
}
})
})
Expand Down
116 changes: 63 additions & 53 deletions lib/router-instrumentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,69 @@
shim.wrapReturn(proto, 'routes', wrapRoutes)
shim.wrapReturn(proto, 'middleware', wrapRoutes)

shim.wrapMiddlewareMounter(proto, 'param', {
route: shim.FIRST,
wrapper: function wrapParamware(shim, paramware, fnName, route) {
return shim.recordParamware(paramware, {
name: route,
next: shim.LAST,
promise: true,
appendPath: false,
req: function getReq(shim, fn, _fnName, args) {
return args[1] && args[1].req
}
})
shim.wrapMiddlewareMounter(
proto,
'param',
new shim.specs.MiddlewareMounterSpec({
route: shim.FIRST,
wrapper: wrapParamware
})
)
}

function wrapParamware(shim, paramware, fnName, route) {
return shim.recordParamware(paramware, {
name: route,
next: shim.LAST,
promise: true,
appendPath: false,
req: function getReq(shim, fn, _fnName, args) {
return args[1] && args[1].req
}
})
}

function wrapMiddleware(shim, fn, name, layer) {
if (!isLayer(layer)) {
return
}
function wrapMiddleware(shim, fn, name, layer) {
if (!isLayer(layer)) {
return
}

Check warning on line 44 in lib/router-instrumentation.js

View check run for this annotation

Codecov / codecov/patch

lib/router-instrumentation.js#L43-L44

Added lines #L43 - L44 were not covered by tests

const spec = {
route: () => layer.path, // defer retrieval
type: shim.MIDDLEWARE,
next: shim.LAST,
promise: true,
appendPath: false,
req: function getReq(shim, func, fnName, args) {
return args[0] && args[0].req
}
const spec = new shim.specs.MiddlewareSpec({
route: () => layer.path, // defer retrieval
next: shim.LAST,
promise: true,
appendPath: false,
req: function getReq(shim, func, fnName, args) {
return args[0] && args[0].req
}
})

layer.stack = layer.stack.map(function wrapLayerMiddleware(m) {
// allowedMethods middleware can exist in a stack so we need to
// protect against re-instrumenting.
if (shim.isWrapped(m)) {
return m
}
layer.stack = layer.stack.map(function wrapLayerMiddleware(m) {
// allowedMethods middleware can exist in a stack so we need to
// protect against re-instrumenting.
if (shim.isWrapped(m)) {
return m
}

return shim.recordMiddleware(m, spec)
})
}
return shim.recordMiddleware(m, spec)
})
}

function wrapAllowedMethods(shim, fn, name, allowedMethodsMiddleware) {
const wrapped = shim.wrap(allowedMethodsMiddleware, wrapAllowedMethodsMiddleware)

return shim.recordMiddleware(wrapped, {
name: allowedMethodsMiddleware.name,
type: shim.MIDDLEWARE,
promise: true,
appendPath: false,
next: shim.LAST,
req: function getReq(shim, func, fnName, args) {
return args[0] && args[0].req
}
})
return shim.recordMiddleware(
wrapped,
new shim.specs.MiddlewareSpec({
name: allowedMethodsMiddleware.name,
promise: true,
appendPath: false,
next: shim.LAST,
req: function getReq(shim, func, fnName, args) {
return args[0] && args[0].req
}
})
)
}

function wrapAllowedMethodsMiddleware(shim, original) {
Expand All @@ -87,15 +94,18 @@
if (shim.isWrapped(dispatchMiddleware)) {
return dispatchMiddleware
}
const wrappedDispatch = shim.recordMiddleware(dispatchMiddleware, {
type: shim.ROUTER,
promise: true,
appendPath: false,
next: shim.LAST,
req: function getReq(shim, func, fnName, args) {
return args[0] && args[0].req
}
})
const wrappedDispatch = shim.recordMiddleware(
dispatchMiddleware,
new shim.specs.MiddlewareSpec({
type: shim.ROUTER,
promise: true,
appendPath: false,
next: shim.LAST,
req: function getReq(shim, func, fnName, args) {
return args[0] && args[0].req
}
})
)

// copy keys from dispatchMiddleware to wrapped version
return Object.assign(wrappedDispatch, dispatchMiddleware)
Expand Down
Loading
Loading