diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index b61a5cd0f1..46798d4f52 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -17,7 +17,6 @@ code, the source code can be found at [https://github.com/newrelic/node-newrelic * [@grpc/grpc-js](#grpcgrpc-js) * [@grpc/proto-loader](#grpcproto-loader) * [@newrelic/aws-sdk](#newrelicaws-sdk) -* [@newrelic/koa](#newrelickoa) * [@newrelic/ritm](#newrelicritm) * [@newrelic/security-agent](#newrelicsecurity-agent) * [@tyriar/fibonacci-heap](#tyriarfibonacci-heap) @@ -33,6 +32,7 @@ code, the source code can be found at [https://github.com/newrelic/node-newrelic **[devDependencies](#devDependencies)** +* [@koa/router](#koarouter) * [@newrelic/eslint-config](#newreliceslint-config) * [@newrelic/newrelic-oss-cli](#newrelicnewrelic-oss-cli) * [@newrelic/test-utilities](#newrelictest-utilities) @@ -56,6 +56,9 @@ code, the source code can be found at [https://github.com/newrelic/node-newrelic * [got](#got) * [husky](#husky) * [jsdoc](#jsdoc) +* [koa-route](#koa-route) +* [koa-router](#koa-router) +* [koa](#koa) * [lint-staged](#lint-staged) * [lockfile-lint](#lockfile-lint) * [memcached](#memcached) @@ -66,6 +69,7 @@ code, the source code can be found at [https://github.com/newrelic/node-newrelic * [rimraf](#rimraf) * [should](#should) * [sinon](#sinon) +* [superagent](#superagent) * [tap](#tap) * [temp](#temp) * [when](#when) @@ -709,215 +713,6 @@ This product includes source derived from [@newrelic/aws-sdk](https://github.com limitations under the License. ``` -### @newrelic/koa - -This product includes source derived from [@newrelic/koa](https://github.com/newrelic/node-newrelic-koa) ([v9.1.0](https://github.com/newrelic/node-newrelic-koa/tree/v9.1.0)), distributed under the [Apache-2.0 License](https://github.com/newrelic/node-newrelic-koa/blob/v9.1.0/LICENSE): - -``` -Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -``` - ### @newrelic/ritm This product includes source derived from [@newrelic/ritm](https://github.com/newrelic-forks/require-in-the-middle) ([v7.2.0](https://github.com/newrelic-forks/require-in-the-middle/tree/v7.2.0)), distributed under the [MIT License](https://github.com/newrelic-forks/require-in-the-middle/blob/v7.2.0/LICENSE): @@ -1295,6 +1090,35 @@ SOFTWARE. ## devDependencies +### @koa/router + +This product includes source derived from [@koa/router](https://github.com/koajs/router) ([v12.0.1](https://github.com/koajs/router/tree/v12.0.1)), distributed under the [MIT License](https://github.com/koajs/router/blob/v12.0.1/LICENSE): + +``` +The MIT License (MIT) + +Copyright (c) 2015 Alexander C. Mingoia and @koajs contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +``` + ### @newrelic/eslint-config This product includes source derived from [@newrelic/eslint-config](https://github.com/newrelic/eslint-config-newrelic) ([v0.3.0](https://github.com/newrelic/eslint-config-newrelic/tree/v0.3.0)), distributed under the [Apache-2.0 License](https://github.com/newrelic/eslint-config-newrelic/blob/v0.3.0/LICENSE): @@ -2705,6 +2529,81 @@ https://github.com/jmblog/color-themes-for-google-code-prettify ``` +### koa-route + +This product includes source derived from [koa-route](https://github.com/koajs/route) ([v4.0.1](https://github.com/koajs/route/tree/v4.0.1)), distributed under the [MIT License](https://github.com/koajs/route/blob/v4.0.1/Readme.md): + +``` +MIT License + +Copyright (c) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +``` + +### koa-router + +This product includes source derived from [koa-router](https://github.com/koajs/router) ([v12.0.1](https://github.com/koajs/router/tree/v12.0.1)), distributed under the [MIT License](https://github.com/koajs/router/blob/v12.0.1/LICENSE): + +``` +The MIT License (MIT) + +Copyright (c) 2015 Alexander C. Mingoia and @koajs contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +``` + +### koa + +This product includes source derived from [koa](https://github.com/koajs/koa) ([v2.15.3](https://github.com/koajs/koa/tree/v2.15.3)), distributed under the [MIT License](https://github.com/koajs/koa/blob/v2.15.3/LICENSE): + +``` +(The MIT License) + +Copyright (c) 2019 Koa contributors + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +``` + ### lint-staged This product includes source derived from [lint-staged](https://github.com/okonet/lint-staged) ([v11.2.6](https://github.com/okonet/lint-staged/tree/v11.2.6)), distributed under the [MIT License](https://github.com/okonet/lint-staged/blob/v11.2.6/LICENSE): @@ -3146,6 +3045,36 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ``` +### superagent + +This product includes source derived from [superagent](https://github.com/ladjs/superagent) ([v8.1.2](https://github.com/ladjs/superagent/tree/v8.1.2)), distributed under the [MIT License](https://github.com/ladjs/superagent/blob/v8.1.2/LICENSE): + +``` +(The MIT License) + +Copyright (c) 2014-2016 TJ Holowaychuk + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +``` + ### tap This product includes source derived from [tap](https://github.com/tapjs/node-tap) ([v16.3.10](https://github.com/tapjs/node-tap/tree/v16.3.10)), distributed under the [ISC License](https://github.com/tapjs/node-tap/blob/v16.3.10/LICENSE): diff --git a/lib/instrumentation/grpc-js/grpc.js b/lib/instrumentation/grpc-js/grpc.js index e57879a4e2..e27a07d6be 100644 --- a/lib/instrumentation/grpc-js/grpc.js +++ b/lib/instrumentation/grpc-js/grpc.js @@ -7,7 +7,7 @@ const recordExternal = require('../../metrics/recorders/http_external') const recordHttp = require('../../metrics/recorders/http') -const specs = require('../../shim/specs') +const { TransactionSpec } = require('../../shim/specs') const { DESTINATIONS } = require('../../config/attribute-filter') const DESTINATION = DESTINATIONS.TRANS_EVENT | DESTINATIONS.ERROR_EVENT const semver = require('semver') @@ -149,7 +149,7 @@ function wrapRegister(shim, original) { args[1] = shim.bindCreateTransaction( instrumentedHandler, - new specs.TransactionSpec({ type: shim.WEB }) + new TransactionSpec({ type: shim.WEB }) ) return original.apply(this, args) diff --git a/lib/instrumentation/http-methods.js b/lib/instrumentation/http-methods.js new file mode 100644 index 0000000000..280c5dcde8 --- /dev/null +++ b/lib/instrumentation/http-methods.js @@ -0,0 +1,9 @@ +/* + * Copyright 2021 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' +const http = require('http') +const methodsLower = http.METHODS.map((method) => method.toLowerCase()) +module.exports.METHODS = methodsLower diff --git a/lib/instrumentation/koa/instrumentation.js b/lib/instrumentation/koa/instrumentation.js new file mode 100644 index 0000000000..1eadd3819b --- /dev/null +++ b/lib/instrumentation/koa/instrumentation.js @@ -0,0 +1,197 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' +const symbols = require('../../symbols') +const { MiddlewareSpec, MiddlewareMounterSpec } = require('../../shim/specs') + +module.exports = function initialize(shim, Koa) { + // Koa's exports are different depending on using CJS or MJS - https://github.com/koajs/koa/issues/1513 + const proto = Koa.prototype || Koa.default?.prototype + + if (!shim || !Koa || !proto || Object.keys(proto).length > 1) { + shim.logger.debug( + 'Koa instrumentation function called with incorrect arguments, not instrumenting.' + ) + return + } + + shim.setFramework(shim.KOA) + + shim.wrapMiddlewareMounter( + proto, + 'use', + new MiddlewareMounterSpec({ + wrapper: wrapMiddleware + }) + ) + shim.wrapReturn(proto, 'createContext', wrapCreateContext) + + // The application is used to handle unhandled errors in the application. We + // want to notice those. + shim.wrap(proto, 'emit', function wrapper(shim, original) { + return function wrappedEmit(evt, err, ctx) { + if (evt === 'error' && ctx) { + shim.noticeError(ctx.req, err) + } + return original.apply(this, arguments) + } + }) +} + +function wrapMiddleware(shim, middleware) { + // Skip middleware that are already wrapped. + if (shim.isWrapped(middleware)) { + return middleware + } + + if (middleware.router) { + shim.logger.info( + [ + 'Found uninstrumented router property on Koa middleware.', + 'This may indicate either an unsupported routing library is being used,', + 'or a particular version of a supported library is not fully instrumented.' + ].join(' ') + ) + } + + return shim.recordMiddleware( + middleware, + new MiddlewareSpec({ + type: shim.MIDDLEWARE, + promise: true, + appendPath: true, + next: shim.LAST, + req: function getReq(shim, fn, fnName, args) { + return args[0] && args[0].req + } + }) + ) +} + +/** + * Many of the properties on the `context` object are just aliases for the same + * property on the `request` or `response` objects. We take advantage of this + * by just intercepting the `request` or `response` property and don't touch + * the `context` property. + * See: https://github.com/koajs/koa/blob/master/lib/context.js#L186-L241 + * + * @param {Shim} shim instance of shim + * @param {function} _fn createContext function + * @param {string} _fnName name of function + * @param {object} context koa ctx object + */ +function wrapCreateContext(shim, _fn, _fnName, context) { + wrapResponseBody(shim, context) + wrapMatchedRoute(shim, context) + wrapResponseStatus(shim, context) +} + +function wrapResponseBody(shim, context) { + // The `context.body` and `context.response.body` properties are how users set + // the response contents. It is roughly equivalent to `res.send()` in Express. + // Under the hood, these set the `_body` property on the `context.response`. + context[symbols.koaBody] = context.response.body + context[symbols.koaBodySet] = false + + Object.defineProperty(context.response, '_body', { + get: () => context[symbols.koaBody], + set: function setBody(val) { + if (!context[symbols.koaRouter]) { + shim.savePossibleTransactionName(context.req) + } + context[symbols.koaBody] = val + context[symbols.koaBodySet] = true + } + }) +} + +function wrapMatchedRoute(shim, context) { + context[symbols.koaMatchedRoute] = null + context[symbols.koaRouter] = false + + Object.defineProperty(context, '_matchedRoute', { + get: () => context[symbols.koaMatchedRoute], + set: (val) => { + const match = getLayerForTransactionName(context) + + // match should never be undefined given _matchedRoute was set + if (match) { + const currentSegment = shim.getActiveSegment() + + // Segment/Transaction may be null, see: + // - https://github.com/newrelic/node-newrelic-koa/issues/32 + // - https://github.com/newrelic/node-newrelic-koa/issues/33 + if (currentSegment) { + const transaction = currentSegment.transaction + + if (context[symbols.koaMatchedRoute]) { + transaction.nameState.popPath() + } + + transaction.nameState.appendPath(match.path) + transaction.nameState.markPath() + } + } + + context[symbols.koaMatchedRoute] = val + // still true if somehow match is undefined because we are + // using koa-router naming and don't want to allow default naming + context[symbols.koaRouter] = true + } + }) +} + +function wrapResponseStatus(shim, context) { + // Sometimes people just set `context.status` or `context.response.status` + // without setting a body. When this happens we'll want to use that as the + // response point to name the transaction. `context.status` is just an alias + // for `context.response.status` so we only wrap the latter. + const statusDescriptor = getInheritedPropertyDescriptor(context.response, 'status') + if (!statusDescriptor) { + shim.logger.debug('Failed to find status descriptor on context.response') + return + } else if (!statusDescriptor.get || !statusDescriptor.set) { + shim.logger.debug(statusDescriptor, 'Status descriptor missing getter/setter pair') + return + } + + Object.defineProperty(context.response, 'status', { + get: () => statusDescriptor.get.call(context.response), + set: function setStatus(val) { + if (!context[symbols.koaBodySet] && !context[symbols.koaRouter]) { + shim.savePossibleTransactionName(context.req) + } + return statusDescriptor.set.call(this, val) + } + }) +} + +function getLayerForTransactionName(context) { + // Context.matched might be null + // See https://github.com/newrelic/node-newrelic-koa/pull/29 + if (!context.matched) { + return null + } + for (let i = context.matched.length - 1; i >= 0; i--) { + const layer = context.matched[i] + if (layer.opts.end) { + return layer + } + } + + return null +} + +function getInheritedPropertyDescriptor(obj, property) { + let proto = obj + let descriptor = null + do { + descriptor = Object.getOwnPropertyDescriptor(proto, property) + proto = Object.getPrototypeOf(proto) + } while (!descriptor && proto) + + return descriptor +} diff --git a/lib/instrumentation/koa/nr-hooks.js b/lib/instrumentation/koa/nr-hooks.js new file mode 100644 index 0000000000..2ab22b5a93 --- /dev/null +++ b/lib/instrumentation/koa/nr-hooks.js @@ -0,0 +1,35 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const InstrumentationDescriptor = require('../../instrumentation-descriptor') + +module.exports = [ + { + type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK, + moduleName: 'koa', + shimName: 'koa', + onRequire: require('./instrumentation') + }, + { + type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK, + moduleName: 'koa-router', + shimName: 'koa', + onRequire: require('./router-instrumentation') + }, + { + type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK, + moduleName: '@koa/router', + shimName: 'koa', + onRequire: require('./router-instrumentation') + }, + { + type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK, + moduleName: 'koa-route', + shimName: 'koa', + onRequire: require('./route-instrumentation') + } +] diff --git a/lib/instrumentation/koa/route-instrumentation.js b/lib/instrumentation/koa/route-instrumentation.js new file mode 100644 index 0000000000..e0e6ccaeb4 --- /dev/null +++ b/lib/instrumentation/koa/route-instrumentation.js @@ -0,0 +1,33 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const { METHODS } = require('../http-methods') +const { MiddlewareSpec } = require('../../shim/specs') + +module.exports = function instrumentRoute(shim, route) { + shim.setFramework(shim.KOA) + + METHODS.forEach(function wrap(method) { + shim.wrap(route, method, function wrapMethod(shim, methodFn) { + return function wrappedMethod() { + const middleware = methodFn.apply(route, arguments) + return shim.recordMiddleware( + middleware, + new 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 + } + }) + ) + } + }) + }) +} diff --git a/lib/instrumentation/koa/router-instrumentation.js b/lib/instrumentation/koa/router-instrumentation.js new file mode 100644 index 0000000000..829313970d --- /dev/null +++ b/lib/instrumentation/koa/router-instrumentation.js @@ -0,0 +1,120 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' +const symbols = require('../../symbols') +const { MiddlewareSpec, MiddlewareMounterSpec } = require('../../shim/specs') + +module.exports = function instrumentRouter(shim, Router) { + shim.setFramework(shim.KOA) + + const proto = Router.prototype + + shim.wrapReturn(proto, 'register', wrapMiddleware) + shim.wrapReturn(proto, 'allowedMethods', wrapAllowedMethods) + shim.wrapReturn(proto, 'routes', wrapRoutes) + shim.wrapReturn(proto, 'middleware', wrapRoutes) + + shim.wrapMiddlewareMounter( + proto, + 'param', + new MiddlewareMounterSpec({ + route: shim.FIRST, + wrapper: wrapParamware + }) + ) +} + +function wrapParamware(shim, paramware, fnName, route) { + return shim.recordParamware( + paramware, + new MiddlewareSpec({ + 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 + } + + const spec = new 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 + } + + return shim.recordMiddleware(m, spec) + }) +} + +function wrapAllowedMethods(shim, fn, name, allowedMethodsMiddleware) { + const wrapped = shim.wrap(allowedMethodsMiddleware, wrapAllowedMethodsMiddleware) + + return shim.recordMiddleware( + wrapped, + new 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) { + return function setRouteHandledOnContextWrapper() { + const [ctx] = shim.argsToArray.apply(shim, arguments) + ctx[symbols.koaRouter] = true + + return original.apply(this, arguments) + } +} + +function wrapRoutes(shim, fn, name, dispatchMiddleware) { + if (shim.isWrapped(dispatchMiddleware)) { + return dispatchMiddleware + } + const wrappedDispatch = shim.recordMiddleware( + dispatchMiddleware, + new 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) +} + +function isLayer(obj) { + return !!(obj.paramNames && obj.path) +} diff --git a/lib/instrumentation/superagent.js b/lib/instrumentation/superagent.js index 0d4560668f..e4f4d47227 100644 --- a/lib/instrumentation/superagent.js +++ b/lib/instrumentation/superagent.js @@ -5,8 +5,7 @@ 'use strict' -const http = require('http') -const METHODS = http.METHODS.map((method) => method.toLowerCase()) +const { METHODS } = require('./http-methods') module.exports = function instrument(agent, superagent, moduleName, shim) { shim.wrapExport(superagent, function wrapRequest(shim, request) { diff --git a/lib/instrumentations.js b/lib/instrumentations.js index 3149b53d22..aa4aab4703 100644 --- a/lib/instrumentations.js +++ b/lib/instrumentations.js @@ -29,7 +29,7 @@ module.exports = function instrumentations() { 'fastify': { type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK }, 'generic-pool': { type: InstrumentationDescriptor.TYPE_GENERIC }, 'ioredis': { type: InstrumentationDescriptor.TYPE_DATASTORE }, - 'koa': { module: '@newrelic/koa' }, + 'koa': { module: './instrumentation/koa' }, 'langchain': { module: './instrumentation/langchain' }, 'memcached': { type: InstrumentationDescriptor.TYPE_DATASTORE }, 'mongodb': { type: InstrumentationDescriptor.TYPE_DATASTORE }, diff --git a/lib/symbols.js b/lib/symbols.js index 8d48933cf6..fdac740724 100644 --- a/lib/symbols.js +++ b/lib/symbols.js @@ -11,6 +11,10 @@ module.exports = { databaseName: Symbol('databaseName'), disableDT: Symbol('Disable distributed tracing'), // description for backwards compatibility executorContext: Symbol('executorContext'), + koaBody: Symbol('body'), + koaBodySet: Symbol('bodySet'), + koaRouter: Symbol('koaRouter'), + koaMatchedRoute: Symbol('matchedRoute'), name: Symbol('name'), onceExecuted: Symbol('onceExecuted'), offTheRecord: Symbol('offTheRecord'), diff --git a/package-lock.json b/package-lock.json index 97bf2b0f9f..c3d58acfec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,6 @@ "@grpc/grpc-js": "^1.9.4", "@grpc/proto-loader": "^0.7.5", "@newrelic/aws-sdk": "^7.3.0", - "@newrelic/koa": "^9.1.0", "@newrelic/ritm": "^7.2.0", "@newrelic/security-agent": "^1.1.1", "@tyriar/fibonacci-heap": "^2.0.7", @@ -30,6 +29,7 @@ "newrelic-naming-rules": "bin/test-naming-rules.js" }, "devDependencies": { + "@koa/router": "^12.0.1", "@newrelic/eslint-config": "^0.3.0", "@newrelic/newrelic-oss-cli": "^0.1.2", "@newrelic/test-utilities": "^8.5.0", @@ -53,6 +53,9 @@ "got": "^11.8.5", "husky": "^6.0.0", "jsdoc": "^4.0.0", + "koa": "^2.15.3", + "koa-route": "^4.0.1", + "koa-router": "^12.0.1", "lint-staged": "^11.0.0", "lockfile-lint": "^4.9.6", "memcached": ">=0.2.8", @@ -830,6 +833,22 @@ "node": ">=v12.0.0" } }, + "node_modules/@koa/router": { + "version": "12.0.1", + "resolved": "https://registry.npmjs.org/@koa/router/-/router-12.0.1.tgz", + "integrity": "sha512-ribfPYfHb+Uw3b27Eiw6NPqjhIhTpVFzEWLwyc/1Xp+DCdwRRyIlAUODX+9bPARF6aQtUu1+/PHzdNvRzcs/+Q==", + "dev": true, + "dependencies": { + "debug": "^4.3.4", + "http-errors": "^2.0.0", + "koa-compose": "^4.1.0", + "methods": "^1.1.2", + "path-to-regexp": "^6.2.1" + }, + "engines": { + "node": ">= 12" + } + }, "node_modules/@newrelic/aws-sdk": { "version": "7.3.0", "resolved": "https://registry.npmjs.org/@newrelic/aws-sdk/-/aws-sdk-7.3.0.tgz", @@ -856,14 +875,6 @@ "prettier": "^2.3.2" } }, - "node_modules/@newrelic/koa": { - "version": "9.1.0", - "resolved": "https://registry.npmjs.org/@newrelic/koa/-/koa-9.1.0.tgz", - "integrity": "sha512-huLV/11IZ1CByVlNzU79bUV1p/SHpglFNPT1DJV5NfcfW+czZ0VIWH9gJd8PK1azaZ1Gy2+HV+nZ1mFuoIANnA==", - "engines": { - "node": ">=16.0.0" - } - }, "node_modules/@newrelic/native-metrics": { "version": "10.0.1", "resolved": "https://registry.npmjs.org/@newrelic/native-metrics/-/native-metrics-10.0.1.tgz", @@ -4633,6 +4644,19 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/cache-content-type": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/cache-content-type/-/cache-content-type-1.0.1.tgz", + "integrity": "sha512-IKufZ1o4Ut42YUrZSo8+qnMTrFuKkvyoLXUywKz9GJ5BrhOFGhLdkx9sG4KAnVvbY6kEcSFjLQul+DVmBm2bgA==", + "dev": true, + "dependencies": { + "mime-types": "^2.1.18", + "ylru": "^1.2.0" + }, + "engines": { + "node": ">= 6.0.0" + } + }, "node_modules/cacheable-lookup": { "version": "5.0.4", "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz", @@ -5153,6 +5177,16 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/co": { + "version": "4.6.0", + "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", + "integrity": "sha512-QVb0dM5HvG+uaxitm8wONl7jltx8dqhfU33DcqtOZcLSVIKSDDLDi7+0LbAKiyI8hD9u42m2YxXSkMGWThaecQ==", + "dev": true, + "engines": { + "iojs": ">= 1.0.0", + "node": ">= 0.12.0" + } + }, "node_modules/color-convert": { "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", @@ -5398,6 +5432,19 @@ "integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==", "dev": true }, + "node_modules/cookies": { + "version": "0.9.1", + "resolved": "https://registry.npmjs.org/cookies/-/cookies-0.9.1.tgz", + "integrity": "sha512-TG2hpqe4ELx54QER/S3HQ9SRVnQnGBtKUz5bLQWtYAQ+o6GpgMs6sYUvaiJjVxb+UXwhRhAEP3m7LbsIZ77Hmw==", + "dev": true, + "dependencies": { + "depd": "~2.0.0", + "keygrip": "~1.1.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/core-util-is": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", @@ -5564,6 +5611,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/deep-equal": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", + "integrity": "sha512-bHtC0iYvWhyaTzvV3CZgPeZQqCOBGyGsVV7v4eevpdkLHfiSrXUdBG+qAuSz4RI70sszvjQ1QSZ98An1yNwpSw==", + "dev": true + }, "node_modules/deep-is": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz", @@ -5642,6 +5695,12 @@ "node": ">=0.4.0" } }, + "node_modules/delegates": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", + "integrity": "sha512-bd2L678uiWATM6m5Z1VzNCErI3jiGzt6HGY8OVICs40JQq/HALfbyNJmp0UDakEY4pMMaN0Ly5om/B1VI/+xfQ==", + "dev": true + }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -7491,6 +7550,53 @@ "node": ">=14" } }, + "node_modules/http-assert": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/http-assert/-/http-assert-1.5.0.tgz", + "integrity": "sha512-uPpH7OKX4H25hBmU6G1jWNaqJGpTXxey+YOUizJUAgu0AjLUeC8D73hTrhvDS5D+GJN1DN1+hhc/eF/wpxtp0w==", + "dev": true, + "dependencies": { + "deep-equal": "~1.0.1", + "http-errors": "~1.8.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/http-assert/node_modules/depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==", + "dev": true, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/http-assert/node_modules/http-errors": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.8.1.tgz", + "integrity": "sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==", + "dev": true, + "dependencies": { + "depd": "~1.1.2", + "inherits": "2.0.4", + "setprototypeof": "1.2.0", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.1" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/http-assert/node_modules/statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==", + "dev": true, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/http-basic": { "version": "8.1.3", "resolved": "https://registry.npmjs.org/http-basic/-/http-basic-8.1.3.tgz", @@ -7971,6 +8077,21 @@ "node": ">=8" } }, + "node_modules/is-generator-function": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.0.10.tgz", + "integrity": "sha512-jsEjy9l3yiXEQ+PsXdmBwEPcOxaXWLspKdplFUVI9vq1iZgIekeC0L167qeu86czQaxed3q/Uzuw0swL0irL8A==", + "dev": true, + "dependencies": { + "has-tostringtag": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/is-glob": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", @@ -8686,6 +8807,18 @@ "safe-buffer": "^5.0.1" } }, + "node_modules/keygrip": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/keygrip/-/keygrip-1.1.0.tgz", + "integrity": "sha512-iYSchDJ+liQ8iwbSI2QqsQOvqv58eJCEanyJPJi+Khyu8smkcKSFUCbPwzFcL7YVtZ6eONjqRX/38caJ7QjRAQ==", + "dev": true, + "dependencies": { + "tsscmp": "1.0.6" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -8722,6 +8855,120 @@ "graceful-fs": "^4.1.11" } }, + "node_modules/koa": { + "version": "2.15.3", + "resolved": "https://registry.npmjs.org/koa/-/koa-2.15.3.tgz", + "integrity": "sha512-j/8tY9j5t+GVMLeioLaxweJiKUayFhlGqNTzf2ZGwL0ZCQijd2RLHK0SLW5Tsko8YyyqCZC2cojIb0/s62qTAg==", + "dev": true, + "dependencies": { + "accepts": "^1.3.5", + "cache-content-type": "^1.0.0", + "content-disposition": "~0.5.2", + "content-type": "^1.0.4", + "cookies": "~0.9.0", + "debug": "^4.3.2", + "delegates": "^1.0.0", + "depd": "^2.0.0", + "destroy": "^1.0.4", + "encodeurl": "^1.0.2", + "escape-html": "^1.0.3", + "fresh": "~0.5.2", + "http-assert": "^1.3.0", + "http-errors": "^1.6.3", + "is-generator-function": "^1.0.7", + "koa-compose": "^4.1.0", + "koa-convert": "^2.0.0", + "on-finished": "^2.3.0", + "only": "~0.0.2", + "parseurl": "^1.3.2", + "statuses": "^1.5.0", + "type-is": "^1.6.16", + "vary": "^1.1.2" + }, + "engines": { + "node": "^4.8.4 || ^6.10.1 || ^7.10.1 || >= 8.1.4" + } + }, + "node_modules/koa-compose": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/koa-compose/-/koa-compose-4.1.0.tgz", + "integrity": "sha512-8ODW8TrDuMYvXRwra/Kh7/rJo9BtOfPc6qO8eAfC80CnCvSjSl0bkRM24X6/XBBEyj0v1nRUQ1LyOy3dbqOWXw==", + "dev": true + }, + "node_modules/koa-convert": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/koa-convert/-/koa-convert-2.0.0.tgz", + "integrity": "sha512-asOvN6bFlSnxewce2e/DK3p4tltyfC4VM7ZwuTuepI7dEQVcvpyFuBcEARu1+Hxg8DIwytce2n7jrZtRlPrARA==", + "dev": true, + "dependencies": { + "co": "^4.6.0", + "koa-compose": "^4.1.0" + }, + "engines": { + "node": ">= 10" + } + }, + "node_modules/koa-route": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/koa-route/-/koa-route-4.0.1.tgz", + "integrity": "sha512-ytLrdDPF/qTMh20BxZCNpIUY329SoGu84xjGYeNsp/jkGT3OpZfkuK646sDScVJQ9XdsLXJVMml1dXMA5EIuxQ==", + "dev": true, + "dependencies": { + "debug": "*", + "methods": "~1.1.0", + "path-to-regexp": "^6.2.1" + } + }, + "node_modules/koa-router": { + "version": "12.0.1", + "resolved": "https://registry.npmjs.org/koa-router/-/koa-router-12.0.1.tgz", + "integrity": "sha512-gaDdj3GtzoLoeosacd50kBBTnnh3B9AYxDThQUo4sfUyXdOhY6ku1qyZKW88tQCRgc3Sw6ChXYXWZwwgjOxE0w==", + "dev": true, + "dependencies": { + "debug": "^4.3.4", + "http-errors": "^2.0.0", + "koa-compose": "^4.1.0", + "methods": "^1.1.2", + "path-to-regexp": "^6.2.1" + }, + "engines": { + "node": ">= 12" + } + }, + "node_modules/koa/node_modules/http-errors": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.8.1.tgz", + "integrity": "sha512-Kpk9Sm7NmI+RHhnj6OIWDI1d6fIoFAtFt9RLaTMRlg/8w49juAStsrBgp0Dp4OdxdVbRIeKhtCUvoi/RuAhO4g==", + "dev": true, + "dependencies": { + "depd": "~1.1.2", + "inherits": "2.0.4", + "setprototypeof": "1.2.0", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.1" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/koa/node_modules/http-errors/node_modules/depd": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", + "integrity": "sha512-7emPTl6Dpo6JRXOXjLRxck+FlLRX5847cLKEn00PLAgc3g2hTZZgr+e4c2v6QpSmLeFP3n5yUo7ft6avBK/5jQ==", + "dev": true, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/koa/node_modules/statuses": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", + "integrity": "sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==", + "dev": true, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/leven": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/leven/-/leven-2.1.0.tgz", @@ -10334,6 +10581,12 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/only": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/only/-/only-0.0.2.tgz", + "integrity": "sha512-Fvw+Jemq5fjjyWz6CpKx6w9s7xxqo3+JCyM0WXWeCSOboZ8ABkyvP8ID4CZuChA/wxSx+XSJmdOm8rGVyJ1hdQ==", + "dev": true + }, "node_modules/opener": { "version": "1.5.2", "resolved": "https://registry.npmjs.org/opener/-/opener-1.5.2.tgz", @@ -15402,6 +15655,15 @@ "node": ">=4.0.0" } }, + "node_modules/ylru": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/ylru/-/ylru-1.4.0.tgz", + "integrity": "sha512-2OQsPNEmBCvXuFlIni/a+Rn+R2pHW9INm0BxXJ4hVDA8TirqMj+J/Rp9ItLatT/5pZqWwefVrTQcHpixsxnVlA==", + "dev": true, + "engines": { + "node": ">= 4.0.0" + } + }, "node_modules/yocto-queue": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", diff --git a/package.json b/package.json index 42b9ab6ce6..9e4eb86a34 100644 --- a/package.json +++ b/package.json @@ -194,7 +194,6 @@ "@grpc/grpc-js": "^1.9.4", "@grpc/proto-loader": "^0.7.5", "@newrelic/aws-sdk": "^7.3.0", - "@newrelic/koa": "^9.1.0", "@newrelic/ritm": "^7.2.0", "@newrelic/security-agent": "^1.1.1", "@tyriar/fibonacci-heap": "^2.0.7", @@ -214,6 +213,7 @@ "@prisma/prisma-fmt-wasm": "^4.17.0-16.27eb2449f178cd9fe1a4b892d732cc4795f75085" }, "devDependencies": { + "@koa/router": "^12.0.1", "@newrelic/eslint-config": "^0.3.0", "@newrelic/newrelic-oss-cli": "^0.1.2", "@newrelic/test-utilities": "^8.5.0", @@ -237,6 +237,9 @@ "got": "^11.8.5", "husky": "^6.0.0", "jsdoc": "^4.0.0", + "koa": "^2.15.3", + "koa-route": "^4.0.1", + "koa-router": "^12.0.1", "lint-staged": "^11.0.0", "lockfile-lint": "^4.9.6", "memcached": ">=0.2.8", diff --git a/test/lib/agent_helper.js b/test/lib/agent_helper.js index fb4ddbb3d9..7cbac195fd 100644 --- a/test/lib/agent_helper.js +++ b/test/lib/agent_helper.js @@ -653,3 +653,12 @@ helper.isSupportedVersion = function isSupportedVersion(version) { helper.destroyProxyAgent = function destroyProxyAgent() { require('../../lib/collector/http-agents').proxyAgent().destroy() } + +/** + * Gets a shim instance for a package. + * @param {object} pkg exported obj that is instrumented + * @returns The existing or newly created shim. + */ +helper.getShim = function getShim(pkg) { + return pkg?.[symbols.shim] +} diff --git a/test/unit/instrumentation/koa/instrumentation.test.js b/test/unit/instrumentation/koa/instrumentation.test.js new file mode 100644 index 0000000000..51516c3a84 --- /dev/null +++ b/test/unit/instrumentation/koa/instrumentation.test.js @@ -0,0 +1,94 @@ +/* + * Copyright 2022 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const tap = require('tap') +const sinon = require('sinon') +const initialize = require('../../../../lib/instrumentation/koa/instrumentation') + +tap.test('Koa instrumentation', (t) => { + t.beforeEach((t) => { + t.context.shimMock = { + KOA: 'koa', + MIDDLEWARE: 'middleware', + logger: { + debug: sinon.stub(), + info: sinon.stub() + }, + specs: { + MiddlewareMounterSpec: sinon.stub(), + MiddlewareSpec: sinon.stub() + }, + setFramework: sinon.stub(), + wrapMiddlewareMounter: sinon.stub(), + wrapReturn: sinon.stub(), + wrap: sinon.stub(), + savePossibleTransactionName: sinon.stub() + } + + t.context.KoaMock = class { + constructor() { + this.use = sinon.stub() + this.createContext = sinon.stub() + this.emit = sinon.stub() + } + } + }) + + t.test('should work with Koa MJS export', async (t) => { + const { shimMock, KoaMock } = t.context + + initialize(shimMock, { default: KoaMock }) + t.equal(shimMock.logger.debug.callCount, 0, 'should not have called debug') + t.ok(shimMock.setFramework.calledOnceWith('koa'), 'should set the framework') + t.ok(shimMock.wrapMiddlewareMounter.calledOnceWith(KoaMock.prototype, 'use'), 'should wrap use') + t.ok( + shimMock.wrapReturn.calledOnceWith(KoaMock.prototype, 'createContext'), + 'should wrap createContext' + ) + t.ok(shimMock.wrap.calledOnceWith(KoaMock.prototype, 'emit'), 'should wrap emit') + }) + + t.test('should log when unable to find the prototype MJS Export', async (t) => { + const { shimMock } = t.context + + initialize(shimMock, { default: {} }) + t.ok( + shimMock.logger.debug.calledOnceWith( + 'Koa instrumentation function called with incorrect arguments, not instrumenting.' + ), + 'should have called debug' + ) + }) + + t.test('should work with Koa CJS export', async (t) => { + const { shimMock, KoaMock } = t.context + + initialize(shimMock, KoaMock) + t.equal(shimMock.logger.debug.callCount, 0, 'should not have called debug') + t.ok(shimMock.setFramework.calledOnceWith('koa'), 'should set the framework') + t.ok(shimMock.wrapMiddlewareMounter.calledOnceWith(KoaMock.prototype, 'use'), 'should wrap use') + t.ok( + shimMock.wrapReturn.calledOnceWith(KoaMock.prototype, 'createContext'), + 'should wrap createContext' + ) + t.ok(shimMock.wrap.calledOnceWith(KoaMock.prototype, 'emit'), 'should wrap emit') + }) + + t.test('should log when unable to find the prototype CJS Export', async (t) => { + const { shimMock } = t.context + + initialize(shimMock, {}) + t.ok( + shimMock.logger.debug.calledOnceWith( + 'Koa instrumentation function called with incorrect arguments, not instrumenting.' + ), + 'should have called debug' + ) + }) + + t.end() +}) diff --git a/test/unit/instrumentation/koa/koa.test.js b/test/unit/instrumentation/koa/koa.test.js new file mode 100644 index 0000000000..47bb083be1 --- /dev/null +++ b/test/unit/instrumentation/koa/koa.test.js @@ -0,0 +1,44 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const tap = require('tap') +const helper = require('../../../lib/agent_helper') +const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor') + +tap.beforeEach((t) => { + t.context.agent = helper.instrumentMockedAgent({ + moduleName: 'koa', + type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK, + onRequire: require('../../../../lib/instrumentation/koa/instrumentation'), + shimName: 'koa' + }) + + t.context.Koa = require('koa') + t.context.shim = helper.getShim(t.context.Koa) +}) + +tap.afterEach((t) => { + helper.unloadAgent(t.context.agent) + Object.keys(require.cache).forEach((key) => { + if (key.includes('koa')) { + delete require.cache[key] + } + }) +}) + +tap.test('Koa instrumentation', async (t) => { + const wrapped = ['createContext', 'use', 'emit'] + const notWrapped = ['handleRequest', 'listen', 'toJSON', 'inspect', 'callback', 'onerror'] + const { Koa, shim } = t.context + + wrapped.forEach(function (method) { + t.ok(shim.isWrapped(Koa.prototype[method]), method + ' is wrapped, as expected') + }) + notWrapped.forEach(function (method) { + t.not(shim.isWrapped(Koa.prototype[method]), method + ' is not wrapped, as expected') + }) +}) diff --git a/test/unit/instrumentation/koa/newrelic.js b/test/unit/instrumentation/koa/newrelic.js new file mode 100644 index 0000000000..5bfe53711f --- /dev/null +++ b/test/unit/instrumentation/koa/newrelic.js @@ -0,0 +1,25 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +exports.config = { + app_name: ['My Application'], + license_key: 'license key here', + logging: { + level: 'trace', + filepath: '../../../newrelic_agent.log' + }, + utilization: { + detect_aws: false, + detect_pcf: false, + detect_azure: false, + detect_gcp: false, + detect_docker: false + }, + transaction_tracer: { + enabled: true + } +} diff --git a/test/unit/instrumentation/koa/route.test.js b/test/unit/instrumentation/koa/route.test.js new file mode 100644 index 0000000000..25f47d5d06 --- /dev/null +++ b/test/unit/instrumentation/koa/route.test.js @@ -0,0 +1,40 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const tap = require('tap') +const { METHODS } = require('../../../../lib/instrumentation/http-methods') +const helper = require('../../../lib/agent_helper') +const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor') + +tap.beforeEach((t) => { + t.context.agent = helper.instrumentMockedAgent({ + moduleName: 'koa-route', + type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK, + onRequire: require('../../../../lib/instrumentation/koa/route-instrumentation'), + shimName: 'koa' + }) + + t.context.KoaRoute = require('koa-route') + t.context.shim = helper.getShim(t.context.KoaRoute) +}) + +tap.afterEach((t) => { + helper.unloadAgent(t.context.agent) + Object.keys(require.cache).forEach((key) => { + if (key.includes('koa-route')) { + delete require.cache[key] + } + }) +}) + +tap.test('methods', function (t) { + const { KoaRoute: route, shim } = t.context + METHODS.forEach(function checkWrapped(method) { + t.ok(shim.isWrapped(route[method]), method + ' should be wrapped') + }) + t.end() +}) diff --git a/test/unit/instrumentation/koa/router.test.js b/test/unit/instrumentation/koa/router.test.js new file mode 100644 index 0000000000..dd34269ae0 --- /dev/null +++ b/test/unit/instrumentation/koa/router.test.js @@ -0,0 +1,137 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const tap = require('tap') + +const instrumentation = require('../../../../lib/instrumentation/koa/router-instrumentation') +const { METHODS } = require('../../../../lib/instrumentation/http-methods') +const helper = require('../../../lib/agent_helper') +const InstrumentationDescriptor = require('../../../../lib/instrumentation-descriptor') +const WRAPPED_METHODS = ['param', 'register', 'routes', 'middleware', 'allowedMethods'] +const UNWRAPPED_METHODS = METHODS.concat([ + 'use', + 'prefix', + 'all', + 'redirect', + 'route', + 'url', + 'match' +]) +const UNWRAPPED_STATIC_METHODS = ['url'] + +// Trying to loop over the list of supported router mods results in: +// Error('Only one agent at a time! This one was created at:'). +// +// So we unroll that loop. + +tap.test('koa-router', (t) => { + const koaRouterMod = 'koa-router' + + t.beforeEach((t) => { + t.context.agent = helper.instrumentMockedAgent({ + moduleName: koaRouterMod, + type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK, + onRequire: instrumentation, + shimName: 'koa' + }) + + t.context.mod = require(koaRouterMod) + t.context.shim = helper.getShim(t.context.mod) + }) + + t.afterEach((t) => { + helper.unloadAgent(t.context.agent) + Object.keys(require.cache).forEach((key) => { + if (key.includes(koaRouterMod)) { + delete require.cache[key] + } + }) + }) + + t.test('mounting paramware', async (t) => { + const { mod: Router, shim } = t.context + const router = new Router() + router.param('second', function () {}) + t.ok(shim.isWrapped(router.params.second), 'param function should be wrapped') + t.end() + }) + + t.test('methods', async (t) => { + const { mod: Router, shim } = t.context + WRAPPED_METHODS.forEach(function checkWrapped(method) { + t.ok( + shim.isWrapped(Router.prototype[method]), + method + ' should be a wrapped method on the prototype' + ) + }) + UNWRAPPED_METHODS.forEach(function checkUnwrapped(method) { + t.not( + shim.isWrapped(Router.prototype[method]), + method + ' should be a unwrapped method on the prototype' + ) + }) + UNWRAPPED_STATIC_METHODS.forEach(function checkUnwrappedStatic(method) { + t.not(shim.isWrapped(Router[method]), method + ' should be an unwrapped static method') + }) + }) + + t.end() +}) + +tap.test('koa-router', (t) => { + const koaRouterMod = '@koa/router' + + t.beforeEach((t) => { + t.context.agent = helper.instrumentMockedAgent({ + moduleName: koaRouterMod, + type: InstrumentationDescriptor.TYPE_WEB_FRAMEWORK, + onRequire: instrumentation, + shimName: 'koa' + }) + + t.context.mod = require(koaRouterMod) + t.context.shim = helper.getShim(t.context.mod) + }) + + t.afterEach((t) => { + helper.unloadAgent(t.context.agent) + Object.keys(require.cache).forEach((key) => { + if (key.includes(koaRouterMod)) { + delete require.cache[key] + } + }) + }) + + t.test('mounting paramware', async (t) => { + const { mod: Router, shim } = t.context + const router = new Router() + router.param('second', function () {}) + t.ok(shim.isWrapped(router.params.second), 'param function should be wrapped') + t.end() + }) + + t.test('methods', async (t) => { + const { mod: Router, shim } = t.context + WRAPPED_METHODS.forEach(function checkWrapped(method) { + t.ok( + shim.isWrapped(Router.prototype[method]), + method + ' should be a wrapped method on the prototype' + ) + }) + UNWRAPPED_METHODS.forEach(function checkUnwrapped(method) { + t.not( + shim.isWrapped(Router.prototype[method]), + method + ' should be a unwrapped method on the prototype' + ) + }) + UNWRAPPED_STATIC_METHODS.forEach(function checkUnwrappedStatic(method) { + t.not(shim.isWrapped(Router[method]), method + ' should be an unwrapped static method') + }) + }) + + t.end() +}) diff --git a/test/versioned-external/external-repos.js b/test/versioned-external/external-repos.js index 70a16b034a..f6c2565958 100644 --- a/test/versioned-external/external-repos.js +++ b/test/versioned-external/external-repos.js @@ -17,11 +17,6 @@ const repos = [ repository: 'https://github.com/newrelic/node-newrelic-aws-sdk.git', branch: 'main' }, - { - name: 'koa', - repository: 'https://github.com/newrelic/node-newrelic-koa.git', - branch: 'fix-paramware' - }, { name: 'next', repository: 'https://github.com/newrelic/newrelic-node-nextjs.git', diff --git a/test/versioned/koa/code-level-metrics.tap.js b/test/versioned/koa/code-level-metrics.tap.js new file mode 100644 index 0000000000..889c3fdbb4 --- /dev/null +++ b/test/versioned/koa/code-level-metrics.tap.js @@ -0,0 +1,262 @@ +/* + * Copyright 2022 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const tap = require('tap') +const helper = require('../../lib/agent_helper') +const http = require('http') +let koaRouterAvailable +let atKoaRouterAvailable + +try { + require('./node_modules/koa-router/package.json') + koaRouterAvailable = true +} catch (err) { + koaRouterAvailable = false +} + +try { + require('./node_modules/@koa/router/package.json') + atKoaRouterAvailable = true +} catch (err) { + atKoaRouterAvailable = false +} + +async function setupApp({ useKoaRouter, useAtKoaRouter, isCLMEnabled }) { + const agent = helper.instrumentMockedAgent({ code_level_metrics: { enabled: isCLMEnabled } }) + let router + + if (useKoaRouter) { + const Router = require('koa-router') + router = new Router() + } + + if (useAtKoaRouter) { + const Router = require('@koa/router') + router = new Router() + } + + const Koa = require('koa') + const app = new Koa() + const server = await startServer(app) + + return { agent, app, router, server } +} + +async function makeRequest(params) { + return new Promise((resolve, reject) => { + const req = http.request(params, (res) => { + if (res.statusCode < 200 || res.statusCode >= 300) { + reject(new Error(`Status Code: ${res.statusCode}`)) + return + } + + const data = [] + + res.on('data', (chunk) => { + data.push(chunk) + }) + + res.on('end', () => resolve(Buffer.concat(data).toString())) + }) + + req.on('error', (err) => { + reject(err) + }) + + req.end() + }) +} + +async function startServer(app) { + return new Promise((resolve) => { + const server = app.listen(0, () => resolve(server)) + }) +} + +async function teardownApp(server, agent) { + return new Promise((resolve) => { + if (agent) { + helper.unloadAgent(agent) + } + + if (server) { + server.close(resolve) + } else { + resolve() + } + }) +} + +tap.test('Vanilla koa, no router', (t) => { + t.autoend() + + let agent + let app + let server + ;[true, false].forEach((isCLMEnabled) => { + t.test(`should ${isCLMEnabled ? 'add' : 'not add'} CLM attributes`, async (t) => { + ;({ agent, app, server } = await setupApp({ isCLMEnabled })) + + app.use(function one(_, next) { + next() + }) + + app.use(function two(ctx) { + ctx.body = 'done' + }) + + agent.on('transactionFinished', (transaction) => { + const baseSegment = transaction.trace.root.children[0] + t.clmAttrs({ + segments: [ + { + segment: baseSegment.children[0], + name: 'one', + filepath: 'code-level-metrics.tap.js' + }, + { + segment: baseSegment.children[0].children[0], + name: 'two', + filepath: 'code-level-metrics.tap.js' + } + ], + enabled: isCLMEnabled, + test: t + }) + }) + + const response = await makeRequest({ port: server.address().port }) + + t.equal(response, 'done', 'should return the correct data') + + t.teardown(async () => { + await teardownApp(server, agent) + }) + }) + }) +}) + +tap.test('Using koa-router', { skip: !koaRouterAvailable }, (t) => { + t.autoend() + + let agent + let app + let server + let router + ;[true, false].forEach((isCLMEnabled) => { + t.test(`should ${isCLMEnabled ? 'add' : 'not add'} CLM attributes`, async (t) => { + ;({ agent, app, server, router } = await setupApp({ isCLMEnabled, useKoaRouter: true })) + + const Router = require('koa-router') + const nestedRouter = new Router() + + nestedRouter.get('/second', function secondMiddleware(ctx) { + ctx.body = 'winner winner, chicken dinner' + }) + + router.use(function appLevelMiddleware(ctx, next) { + ctx.body = 'nope, not here' + return next() + }) + router.use('/:first', nestedRouter.routes()) + app.use(router.routes()) + + agent.on('transactionFinished', (transaction) => { + const baseSegment = transaction.trace.root.children[0] + + t.clmAttrs({ + segments: [ + { + segment: baseSegment.children[0], + name: 'dispatch', + filepath: 'koa-router/lib/router.js' + }, + { + segment: baseSegment.children[0].children[0], + name: 'appLevelMiddleware', + filepath: 'code-level-metrics.tap.js' + }, + { + segment: baseSegment.children[0].children[0].children[0], + name: 'secondMiddleware', + filepath: 'code-level-metrics.tap.js' + } + ], + enabled: isCLMEnabled, + test: t + }) + }) + + const response = await makeRequest({ port: server.address().port, path: '/123/second' }) + t.equal(response, 'winner winner, chicken dinner', 'should return the correct data') + t.teardown(async () => { + await teardownApp(server, agent) + }) + }) + }) +}) + +tap.test('Using @koa/router', { skip: !atKoaRouterAvailable }, (t) => { + t.autoend() + + let agent + let app + let server + let router + ;[true, false].forEach((isCLMEnabled) => { + t.test(`should ${isCLMEnabled ? 'add' : 'not add'} CLM attributes`, async (t) => { + ;({ agent, app, server, router } = await setupApp({ isCLMEnabled, useAtKoaRouter: true })) + + const Router = require('@koa/router') + const nestedRouter = new Router() + + nestedRouter.get('/second', function secondMiddleware(ctx) { + ctx.body = 'winner winner, chicken dinner' + }) + + router.use(function appLevelMiddleware(ctx, next) { + ctx.body = 'nope, not here' + return next() + }) + router.use('/:first', nestedRouter.routes()) + app.use(router.routes()) + + agent.on('transactionFinished', (transaction) => { + const baseSegment = transaction.trace.root.children[0] + + t.clmAttrs({ + segments: [ + { + segment: baseSegment.children[0], + name: 'dispatch', + filepath: '@koa/router/lib/router.js' + }, + { + segment: baseSegment.children[0].children[0], + name: 'appLevelMiddleware', + filepath: 'code-level-metrics.tap.js' + }, + { + segment: baseSegment.children[0].children[0].children[0], + name: 'secondMiddleware', + filepath: 'code-level-metrics.tap.js' + } + ], + enabled: isCLMEnabled, + test: t + }) + }) + + const response = await makeRequest({ port: server.address().port, path: '/123/second' }) + t.equal(response, 'winner winner, chicken dinner', 'should return the correct data') + + t.teardown(async () => { + await teardownApp(server, agent) + }) + }) + }) +}) diff --git a/test/versioned/koa/koa-route.tap.js b/test/versioned/koa/koa-route.tap.js new file mode 100644 index 0000000000..9048776692 --- /dev/null +++ b/test/versioned/koa/koa-route.tap.js @@ -0,0 +1,197 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const tap = require('tap') +const helper = require('../../lib/agent_helper') +require('../../lib/metrics_helper') +const { run } = require('./utils') + +tap.test('koa-route instrumentation', function (t) { + t.beforeEach(function (t) { + t.context.agent = helper.instrumentMockedAgent() + const Koa = require('koa') + t.context.app = new Koa() + t.context.route = require('koa-route') + }) + + t.afterEach(function (t) { + t.context.server.close() + helper.unloadAgent(t.context.agent) + }) + + t.test('should name and produce segments for koa-route middleware', function (t) { + const { agent, app, route } = t.context + const first = route.get('/resource', function firstMiddleware(ctx) { + ctx.body = 'hello' + }) + app.use(first) + agent.on('transactionFinished', function (tx) { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//resource', + ['Nodejs/Middleware/Koa/firstMiddleware//resource'] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//resource', + 'transaction should be named after the middleware responsible for responding' + ) + t.end() + }) + run({ path: '/resource', context: t.context }) + }) + + t.test('should name the transaction after the last responder', function (t) { + const { agent, app, route } = t.context + const first = route.get('/:first', function firstMiddleware(ctx, param, next) { + ctx.body = 'first' + return next() + }) + const second = route.get('/:second', function secondMiddleware(ctx) { + ctx.body = 'second' + }) + app.use(first) + app.use(second) + agent.on('transactionFinished', function (tx) { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + [ + 'Nodejs/Middleware/Koa/firstMiddleware//:first', + ['Nodejs/Middleware/Koa/secondMiddleware//:second'] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + 'transaction should be named after the middleware responsible for responding' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name the transaction properly when responding after next', function (t) { + const { agent, app, route } = t.context + const first = route.get('/:first', function firstMiddleware(ctx, param, next) { + return next().then(function respond() { + ctx.body = 'first' + }) + }) + const second = route.get('/:second', function secondMiddleware(ctx) { + ctx.body = 'second' + }) + app.use(first) + app.use(second) + agent.on('transactionFinished', function (tx) { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + [ + 'Nodejs/Middleware/Koa/firstMiddleware//:first', + ['Nodejs/Middleware/Koa/secondMiddleware//:second'] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + 'transaction should be named after the middleware responsible for responding' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should work with early responding', function (t) { + const { agent, app, route } = t.context + const first = route.get('/:first', function firstMiddleware(ctx) { + ctx.body = 'first' + return Promise.resolve() + }) + const second = route.get('/:second', function secondMiddleware(ctx) { + ctx.body = 'second' + }) + app.use(first) + app.use(second) + agent.on('transactionFinished', function (tx) { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + ['Nodejs/Middleware/Koa/firstMiddleware//:first'] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + 'transaction should be named after the middleware responsible for responding' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name the transaction after the source of the error that occurred', function (t) { + const { agent, app, route } = t.context + const first = route.get('/:first', function firstMiddleware(ctx, param, next) { + return next() + }) + const second = route.get('/:second', function secondMiddleware() { + throw new Error('some error') + }) + app.use(first) + app.use(second) + agent.on('transactionFinished', function (tx) { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + [ + 'Nodejs/Middleware/Koa/firstMiddleware//:first', + ['Nodejs/Middleware/Koa/secondMiddleware//:second'] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + 'transaction should be named after the middleware responsible for responding' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should work properly when used along with non-route middleware', function (t) { + const { agent, app, route } = t.context + const first = function firstMiddleware(ctx, next) { + return next() + } + const second = route.get('/resource', function secondMiddleware(ctx, next) { + ctx.body = 'hello' + return next() + }) + const third = function thirdMiddleware(ctx, next) { + return next() + } + app.use(first) + app.use(second) + app.use(third) + agent.on('transactionFinished', function (tx) { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//resource', + [ + 'Nodejs/Middleware/Koa/firstMiddleware', + [ + 'Nodejs/Middleware/Koa/secondMiddleware//resource', + ['Nodejs/Middleware/Koa/thirdMiddleware'] + ] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//resource', + 'transaction should be named after the middleware responsible for responding' + ) + t.end() + }) + run({ path: '/resource', context: t.context }) + }) + + t.end() +}) diff --git a/test/versioned/koa/koa-router.tap.js b/test/versioned/koa/koa-router.tap.js new file mode 100644 index 0000000000..bf57137cb9 --- /dev/null +++ b/test/versioned/koa/koa-router.tap.js @@ -0,0 +1,7 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' +require('./router-common')('koa-router') diff --git a/test/versioned/koa/koa.tap.js b/test/versioned/koa/koa.tap.js new file mode 100644 index 0000000000..9f2844fcce --- /dev/null +++ b/test/versioned/koa/koa.tap.js @@ -0,0 +1,408 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const tap = require('tap') +const http = require('http') +const helper = require('../../lib/agent_helper') +require('../../lib/metrics_helper') + +tap.test('Koa instrumentation', (t) => { + t.autoend() + + t.beforeEach(() => { + t.context.agent = helper.instrumentMockedAgent() + const Koa = require('koa') + t.context.app = new Koa() + t.context.testShim = helper.getShim(Koa) + }) + + t.afterEach((t) => { + t.context.server.close() + helper.unloadAgent(t.context.agent) + }) + + t.test('Should name after koa framework and verb when body set', (t) => { + const { agent, app } = t.context + + app.use(function one(ctx, next) { + return next().then(() => { + // do nothing + }) + }) + + app.use(function two(ctx) { + ctx.body = 'done' + }) + + agent.on('transactionFinished', (tx) => { + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//', + 'should have name without post-response name info' + ) + }) + + run(t) + }) + + t.test('Should name (not found) when no work is performed', (t) => { + const { agent, app } = t.context + + app.use(function one(ctx, next) { + return next().then(() => { + // do nothing + }) + }) + + app.use(function two() { + // do nothing + }) + + agent.on('transactionFinished', (tx) => { + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET/(not found)', + 'should name after status code message' + ) + }) + + run(t, 'Not Found') + }) + + t.test('names the transaction after the middleware that sets the body', (t) => { + const { agent, app } = t.context + + app.use(function one(ctx, next) { + const tx = agent.getTransaction() + return next().then(() => tx.nameState.appendPath('one-end')) + }) + + app.use(function two(ctx) { + const tx = agent.getTransaction() + tx.nameState.appendPath('two') + ctx.body = 'done' + }) + + agent.on('transactionFinished', (tx) => { + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//two', + 'should have name without post-response name info' + ) + }) + + run(t) + }) + + t.test('names the transaction after the last middleware that sets the body', (t) => { + const { agent, app } = t.context + + app.use(function one(ctx, next) { + const tx = agent.getTransaction() + return next().then(() => tx.nameState.appendPath('one-end')) + }) + + app.use(function two(ctx, next) { + const tx = agent.getTransaction() + tx.nameState.appendPath('two') + ctx.body = 'not actually done' + return next() + }) + + app.use(function three(ctx) { + const tx = agent.getTransaction() + tx.nameState.appendPath('three') + ctx.body = 'done' + }) + + agent.on('transactionFinished', (tx) => { + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//three', + 'should have name without post-response name info' + ) + }) + + run(t) + }) + + t.test('names the transaction off the status setting middleware', (t) => { + const { agent, app } = t.context + + app.use(function one(ctx, next) { + const tx = agent.getTransaction() + return next().then(() => tx.nameState.appendPath('one-end')) + }) + + app.use(function two(ctx) { + const tx = agent.getTransaction() + tx.nameState.appendPath('two') + ctx.status = 202 + }) + + agent.on('transactionFinished', (tx) => { + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//two', + 'should have name without post-response name info' + ) + }) + + run(t, 'Accepted', (err, res) => { + t.error(err) + t.equal(res.statusCode, 202, 'should not interfere with status code setting') + t.end() + }) + }) + + t.test('names the transaction when body set even if status set after', (t) => { + const { agent, app } = t.context + + app.use(function one(ctx, next) { + const tx = agent.getTransaction() + return next().then(() => tx.nameState.appendPath('one-end')) + }) + + app.use(function two(ctx) { + const tx = agent.getTransaction() + tx.nameState.appendPath('two') + ctx.body = 'done' + + tx.nameState.appendPath('setting-status') + ctx.status = 202 + }) + + agent.on('transactionFinished', (tx) => { + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//two', + 'should have name without post-response name info' + ) + }) + + run(t, (err, res) => { + t.error(err) + t.equal(res.statusCode, 202, 'should not interfere with status code setting') + t.end() + }) + }) + + t.test('produces transaction trace with multiple middleware', (t) => { + const { agent, app } = t.context + + app.use(function one(ctx, next) { + return next() + }) + app.use(function two(ctx) { + ctx.response.body = 'done' + }) + + agent.on('transactionFinished', (tx) => { + checkSegments(t, tx) + }) + + run(t) + }) + + t.test('correctly records actions interspersed among middleware', (t) => { + const { agent, app, testShim } = t.context + + app.use(function one(ctx, next) { + testShim.createSegment('testSegment') + return next().then(function () { + testShim.createSegment('nestedSegment') + }) + }) + app.use(function two(ctx, next) { + return new Promise(function (resolve) { + setTimeout(resolve, 10) + }).then(next) + }) + app.use(function three(ctx) { + ctx.body = 'done' + }) + + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//', + [ + 'Nodejs/Middleware/Koa/one', + [ + 'Truncated/testSegment', + 'Nodejs/Middleware/Koa/two', + ['timers.setTimeout', ['Callback: '], 'Nodejs/Middleware/Koa/three'], + 'Truncated/nestedSegment' + ] + ] + ]) + }) + + run(t) + }) + + t.test('maintains transaction state between middleware', (t) => { + const { agent, app } = t.context + let tx + + app.use(async function one(ctx, next) { + tx = agent.getTransaction() + + await next() + + t.ok(tx) + }) + + app.use(async function two(ctx, next) { + t.equal(tx.id, agent.getTransaction().id, 'two has transaction context') + await next() + }) + + app.use(function three(ctx, next) { + t.equal(tx.id, agent.getTransaction().id, 'three has transaction context') + return new Promise((resolve) => { + setImmediate(() => { + next().then(() => { + t.equal( + tx.id, + agent.getTransaction().id, + 'still have context after in-context timer hop' + ) + resolve() + }) + }) + }) + }) + + app.use(function four(ctx) { + t.equal(tx.id, agent.getTransaction().id, 'four has transaction context') + ctx.body = 'done' + }) + + agent.on('transactionFinished', function (txn) { + t.assertSegments(tx.trace.root, [ + txn.name, + [ + 'Nodejs/Middleware/Koa/one', + [ + 'Nodejs/Middleware/Koa/two', + ['Nodejs/Middleware/Koa/three', ['Nodejs/Middleware/Koa/four']] + ] + ] + ]) + }) + + run(t) + }) + + t.test('errors handled within middleware are not recorded', (t) => { + const { agent, app } = t.context + + app.use(function one(ctx, next) { + return next().catch(function (err) { + t.equal(err.message, 'middleware error', 'caught expected error') + ctx.status = 200 + ctx.body = 'handled error' + }) + }) + app.use(function two(ctx) { + throw new Error('middleware error') + ctx.body = 'done' + }) + + agent.on('transactionFinished', (tx) => { + const errors = agent.errors.traceAggregator.errors + t.equal(errors.length, 0, 'no errors are recorded') + checkSegments(t, tx) + }) + + run(t, 'handled error') + }) + + t.test('errors not handled by middleware are recorded', (t) => { + const { agent, app } = t.context + + app.use(function one(ctx, next) { + return next().catch(function (err) { + t.equal(err.message, 'middleware error', 'caught expected error') + ctx.status = 500 + ctx.body = 'error is not actually handled' + }) + }) + app.use(function two() { + throw new Error('middleware error') + }) + + agent.on('transactionFinished', (tx) => { + const errors = agent.errors.traceAggregator.errors + t.equal(errors.length, 1, 'recorded expected number of errors') + const error = errors[0][2] + t.equal(error, 'middleware error', 'recorded expected error') + checkSegments(t, tx) + }) + run(t, 'error is not actually handled') + }) + + t.test('errors caught by default error listener are recorded', (t) => { + const { agent, app } = t.context + + app.use(function one(ctx, next) { + return next() + }) + app.use(function two() { + throw new Error('middleware error') + }) + app.on('error', function (err) { + t.equal(err.message, 'middleware error', 'caught expected error') + }) + + agent.on('transactionFinished', (tx) => { + const errors = agent.errors.traceAggregator.errors + t.equal(errors.length, 1, 'recorded expected number of errors') + const error = errors[0][2] + t.equal(error, 'middleware error', 'recorded expected error') + checkSegments(t, tx) + }) + run(t, 'Internal Server Error') + }) + + function run(t, expected, cb) { + if (typeof expected !== 'string') { + // run(t [, cb]) + cb = expected + expected = 'done' + } + + t.context.server = t.context.app.listen(0, () => { + http.get({ port: t.context.server.address().port }, (res) => { + let body = '' + res.on('data', (data) => (body += data.toString('utf8'))) + res.on('error', (err) => cb && cb(err)) + res.on('end', () => { + if (expected) { + t.equal(body, expected, 'should send expected response') + } + + if (!cb) { + t.end() + return + } + + cb(null, res) + }) + }) + }) + } +}) + +function checkSegments(t, tx) { + t.assertSegments(tx.trace.root, [ + // Until koa-router is instrumented and transaction naming is addressed, + // names will be inconsistent depending on whether there is an error. + tx.name, + ['Nodejs/Middleware/Koa/one', ['Nodejs/Middleware/Koa/two']] + ]) +} diff --git a/test/versioned/koa/newrelic.js b/test/versioned/koa/newrelic.js new file mode 100644 index 0000000000..5bfe53711f --- /dev/null +++ b/test/versioned/koa/newrelic.js @@ -0,0 +1,25 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +exports.config = { + app_name: ['My Application'], + license_key: 'license key here', + logging: { + level: 'trace', + filepath: '../../../newrelic_agent.log' + }, + utilization: { + detect_aws: false, + detect_pcf: false, + detect_azure: false, + detect_gcp: false, + detect_docker: false + }, + transaction_tracer: { + enabled: true + } +} diff --git a/test/versioned/koa/package.json b/test/versioned/koa/package.json new file mode 100644 index 0000000000..df17b4e4ec --- /dev/null +++ b/test/versioned/koa/package.json @@ -0,0 +1,84 @@ +{ + "name": "koa-tests", + "targets": [ + "koa", + "koa-router", + "@koa/router" + ], + "version": "0.0.0", + "private": true, + "tests": [ + { + "engines": { + "node": ">=16" + }, + "dependencies": { + "koa": { + "versions": ">=2.0.0", + "samples": 5 + } + }, + "files": [ + "koa.tap.js", + "code-level-metrics.tap.js" + ] + }, + { + "engines": { + "node": ">=16" + }, + "dependencies": { + "koa": { + "versions": ">=2.0.0", + "samples": 5 + }, + "koa-router": { + "versions": ">=7.1.0", + "samples": 5 + } + }, + "files": [ + "koa-router.tap.js", + "code-level-metrics.tap.js" + ] + }, + { + "engines": { + "node": ">=16" + }, + "dependencies": { + "koa": { + "versions": ">=2.0.0", + "samples": 5 + }, + "@koa/router": { + "versions": ">=8.0.0", + "samples": 5 + } + }, + "files": [ + "scoped-koa-router.tap.js", + "code-level-metrics.tap.js" + ] + }, + { + "engines": { + "node": ">=16" + }, + "dependencies": { + "koa": { + "versions": ">=2.0.0", + "samples": 5 + }, + "koa-route": { + "versions": ">=3.0.0", + "samples": 5 + } + }, + "files": [ + "koa-route.tap.js" + ] + } + ], + "dependencies": {} +} diff --git a/test/versioned/koa/router-common.js b/test/versioned/koa/router-common.js new file mode 100644 index 0000000000..50492b76ea --- /dev/null +++ b/test/versioned/koa/router-common.js @@ -0,0 +1,887 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +module.exports = (pkg) => { + const tap = require('tap') + require('../../lib/metrics_helper') + const helper = require('../../lib/agent_helper') + const semver = require('semver') + const { run } = require('./utils') + + tap.test(`${pkg} instrumentation`, (t) => { + const { version: pkgVersion } = require(`${pkg}/package.json`) + const paramMiddlewareName = 'Nodejs/Middleware/Koa/middleware//:first' + + /** + * Helper to decide how to name nested route segments + * This diverged in 8.0.2 and we decided not to fix. + * Instead of pinning the routers to a very old version we unleashed + * and handle the differences. + * + * See original issue: https://github.com/newrelic/node-newrelic-koa/issues/35 + */ + function getNestedSpanName(mwName) { + let spanName = `Nodejs/Middleware/Koa/${mwName}/` + if (semver.gte(pkgVersion, '8.0.2')) { + spanName += '/:second' + } else { + spanName += '/:first/:second' + } + return spanName + } + + function testSetup(t) { + t.context.agent = helper.instrumentMockedAgent() + + const Koa = require('koa') + t.context.app = new Koa() + const Router = require(pkg) + t.context.router = new Router() + t.context.Router = Router + } + + function tearDown(t) { + t.context.server.close() + helper.unloadAgent(t.context.agent) + } + + t.test('with single router', (t) => { + t.beforeEach(testSetup) + t.afterEach(tearDown) + t.autoend() + + t.test('should name and produce segments for matched path', (t) => { + const { agent, router, app } = t.context + router.get( + '/:first', + function firstMiddleware(ctx, next) { + next().then(() => { + ctx.body = 'first' + }) + }, + function secondMiddleware(ctx) { + ctx.body = 'second' + } + ) + + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + [ + 'Koa/Router: /', + [ + 'Nodejs/Middleware/Koa/firstMiddleware//:first', + ['Nodejs/Middleware/Koa/secondMiddleware//:first'] + ] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + 'transaction should be named after the matched path' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name after matched path using middleware() alias', (t) => { + const { agent, router, app } = t.context + router.get('/:first', function firstMiddleware(ctx) { + ctx.body = 'first' + }) + app.use(router.middleware()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/firstMiddleware//:first']] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + 'transaction should be named after the matched path' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should handle transaction state loss', (t) => { + const { agent, router, app } = t.context + let savedCtx = null + router.get('/:any', (ctx) => { + savedCtx = ctx + }) + app.use(router.middleware()) + agent.on('transactionFinished', () => { + t.doesNotThrow(() => (savedCtx._matchedRoute = 'test')) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name and produce segments for matched regex path', (t) => { + const { agent, router, app } = t.context + router.get(/.*rst$/, function firstMiddleware(ctx) { + ctx.body = 'first' + }) + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//.*rst$', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/firstMiddleware//.*rst$/']] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//.*rst$', + 'transaction should be named after the matched regex pattern' + ) + t.end() + }) + run({ path: '/first', context: t.context }) + }) + + t.test('should name and produce segments for matched wildcard path', (t) => { + const { agent, router, app } = t.context + router.get('/:first/(.*)', function firstMiddleware(ctx) { + ctx.body = 'first' + }) + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first/(.*)', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/firstMiddleware//:first/(.*)']] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first/(.*)', + 'transaction should be named after the matched regex path' + ) + t.end() + }) + run({ path: '/123/456', context: t.context }) + }) + + t.test('should name and produce segments with router paramware', (t) => { + const { agent, router, app } = t.context + router.param('first', function firstParamware(id, ctx, next) { + ctx.body = 'first' + return next() + }) + router.get('/:first', function firstMiddleware(ctx, next) { + return next() + }) + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + [ + 'Koa/Router: /', + [ + paramMiddlewareName, + [ + 'Nodejs/Middleware/Koa/firstParamware//[param handler :first]', + ['Nodejs/Middleware/Koa/firstMiddleware//:first'] + ] + ] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + 'transaction should be named after the matched path' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name transaction after matched path with erroring parameware', (t) => { + const { agent, router, app } = t.context + router.param('first', function firstParamware() { + throw new Error('wrong param') + }) + router.get('/:first', function firstMiddleware() {}) + + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + [ + 'Koa/Router: /', + [ + paramMiddlewareName, + ['Nodejs/Middleware/Koa/firstParamware//[param handler :first]'] + ] + ] + ]) + const errors = agent.errors.eventAggregator + t.equal(errors.length, 1, 'the error has been recorded') + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + 'transaction should be named after the matched path' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name the transaction after the last matched path (layer)', (t) => { + const { agent, router, app } = t.context + router.get('/:first', function firstMiddleware(ctx, next) { + ctx.body = 'first' + return next().then(function someMoreContent() { + ctx.body = 'first' + }) + }) + router.get('/:second', function secondMiddleware(ctx) { + ctx.body += ' second' + }) + + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + [ + 'Koa/Router: /', + [ + 'Nodejs/Middleware/Koa/firstMiddleware//:first', + ['Nodejs/Middleware/Koa/secondMiddleware//:second'] + ] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + 'transaction should be named after the matched path' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('tx name should not be named after error handling middleware', (t) => { + const { agent, router, app } = t.context + app.use(function errorHandler(ctx, next) { + return next().catch((err) => { + ctx.body = { err: err.message } + }) + }) + + router.get('/:first', function firstMiddleware(ctx) { + ctx.throw(400, '☃') + }) + + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + [ + 'Nodejs/Middleware/Koa/errorHandler', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/firstMiddleware//:first']] + ] + ]) + const errors = agent.errors.eventAggregator + t.equal(errors.length, 0, 'should not record error') + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + 'transaction should be named after the matched layer path' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('transaction name should not be affected by unhandled error', (t) => { + const { agent, router, app } = t.context + app.use(function errorHandler(ctx, next) { + return next() + }) + + router.get('/:first', function firstMiddleware(ctx) { + ctx.throw(400, '☃') + }) + + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + [ + 'Nodejs/Middleware/Koa/errorHandler', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/firstMiddleware//:first']] + ] + ]) + const errors = agent.errors.eventAggregator + t.equal(errors.length, 1, 'error should be recorded') + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + 'transaction should be named after the matched layer path' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name tx after route declarations with supported http methods', (t) => { + const { agent, router, app } = t.context + // This will register the same middleware (i.e. secondMiddleware) + // under both the /:first and /:second routes. Use does not register middleware + // w/ supported methods they cannot handle routes. + router.use(['/:first', '/:second'], function secondMiddleware(ctx, next) { + ctx.body += ' second' + return next() + }) + router.get('/:second', function terminalMiddleware(ctx) { + ctx.body = ' second' + }) + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + [ + 'Koa/Router: /', + [ + 'Nodejs/Middleware/Koa/secondMiddleware//:first', + [ + 'Nodejs/Middleware/Koa/secondMiddleware//:second', + ['Nodejs/Middleware/Koa/terminalMiddleware//:second'] + ] + ] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + 'transaction should be named after the last matched path' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('names transaction (not found) with array of paths and no handler', (t) => { + const { agent, router, app } = t.context + // This will register the same middleware (i.e. secondMiddleware) + // under both the /:first and /:second routes. + router.use(['/:first', '/:second'], function secondMiddleware(ctx, next) { + ctx.body += ' second' + return next() + }) + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET/(not found)', + ['Koa/Router: /'] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET/(not found)', + 'transaction should be named (not found)' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test( + 'names tx (not found) when no matching route and base middleware does not set body', + (t) => { + const { agent, router, app } = t.context + app.use(function baseMiddleware(ctx, next) { + next() + }) + + // This will register the same middleware (i.e. secondMiddleware) + // under both the /:first and /:second routes. + router.get('/first', function secondMiddleware(ctx) { + ctx.body = 'first' + }) + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET/(not found)', + ['Nodejs/Middleware/Koa/baseMiddleware', ['Koa/Router: /']] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET/(not found)', + 'transaction should be named (not found)' + ) + t.end() + }) + run({ path: '/', context: t.context }) + } + ) + }) + + t.test('using multipler routers', (t) => { + t.beforeEach(testSetup) + t.afterEach(tearDown) + t.autoend() + + t.test('should name transaction after last route for identical matches', (t) => { + const { agent, router, app } = t.context + const Router = require(pkg) + const router2 = new Router() + router.get('/:first', function firstMiddleware(ctx, next) { + ctx.body = 'first' + return next() + }) + + router2.get('/:second', function secondMiddleware(ctx) { + ctx.body += ' second' + }) + app.use(router.routes()) + app.use(router2.routes()) + agent.on('transactionFinished', (tx) => { + // NOTE: due to an implementation detail in koa-compose, + // sequential middleware will show up as nested. This is due to + // the dispatch function blocking its returned promise on the + // resolution of a recursively returned promise. + // https://github.com/koajs/compose/blob/e754ca3c13e9248b3f453d98ea0b618e09578e2d/index.js#L42-L44 + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + [ + 'Koa/Router: /', + [ + 'Nodejs/Middleware/Koa/firstMiddleware//:first', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/secondMiddleware//:second']] + ] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + 'transaction should be named after the most specific matched path' + ) + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name tx after last matched route even if body not set', (t) => { + const { agent, router, app } = t.context + const Router = require(pkg) + const router2 = new Router() + router.get('/first', function firstMiddleware(ctx, next) { + ctx.body = 'first' + return next() + }) + + router2.get('/:second', function secondMiddleware() {}) + app.use(router.routes()) + app.use(router2.routes()) + agent.on('transactionFinished', (tx) => { + // NOTE: due to an implementation detail in koa-compose, + // sequential middleware will show up as nested. This is due to + // the dispatch function blocking its returned promise on the + // resolution of a recursively returned promise. + // https://github.com/koajs/compose/blob/e754ca3c13e9248b3f453d98ea0b618e09578e2d/index.js#L42-L44 + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + [ + 'Koa/Router: /', + [ + 'Nodejs/Middleware/Koa/firstMiddleware//first', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/secondMiddleware//:second']] + ] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:second', + 'transaction should be named after the last matched path' + ) + t.end() + }) + run({ path: '/first', context: t.context }) + }) + }) + + t.test('using nested or prefixed routers', (t) => { + t.beforeEach(testSetup) + t.afterEach(tearDown) + t.autoend() + + t.test('should name after most last matched path', (t) => { + const { agent, router, Router, app } = t.context + const router2 = new Router() + router2.get('/:second', function secondMiddleware(ctx) { + ctx.body = ' second' + }) + router.use('/:first', router2.routes()) + app.use(router.routes()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first/:second', + ['Koa/Router: /', [getNestedSpanName('secondMiddleware')]] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first/:second', + 'transaction should be named after the last matched path' + ) + t.end() + }) + run({ path: '/123/456/', context: t.context }) + }) + + t.test('app-level middleware should not rename tx from matched path', (t) => { + const { agent, router, Router, app } = t.context + app.use(function appLevelMiddleware(ctx, next) { + return next().then(() => { + ctx.body = 'do not want this to set the name' + }) + }) + + const nestedRouter = new Router() + nestedRouter.get('/:second', function terminalMiddleware(ctx) { + ctx.body = 'this is a test' + }) + nestedRouter.get('/second', function secondMiddleware(ctx) { + ctx.body = 'want this to set the name' + }) + router.use('/:first', nestedRouter.routes()) + app.use(router.routes()) + + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first/second', + [ + 'Nodejs/Middleware/Koa/appLevelMiddleware', + ['Koa/Router: /', [getNestedSpanName('terminalMiddleware')]] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first/second', + 'should be named after last matched route' + ) + t.end() + }) + run({ path: '/123/second', context: t.context }) + }) + + t.test('app-level middleware should not rename tx from matched prefix path', (t) => { + const { agent, router, app } = t.context + app.use(function appLevelMiddleware(ctx, next) { + return next().then(() => { + ctx.body = 'do not want this to set the name' + }) + }) + + router.get('/:second', function terminalMiddleware(ctx) { + ctx.body = 'this is a test' + }) + router.get('/second', function secondMiddleware(ctx) { + ctx.body = 'want this to set the name' + }) + router.prefix('/:first') + app.use(router.routes()) + + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first/second', + [ + 'Nodejs/Middleware/Koa/appLevelMiddleware', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/terminalMiddleware//:first/:second']] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first/second', + 'should be named after the last matched path' + ) + t.end() + }) + run({ path: '/123/second', context: t.context }) + }) + }) + + t.test('using allowedMethods', (t) => { + t.autoend() + + t.test('with throw: true', (t) => { + t.beforeEach(testSetup) + t.afterEach(tearDown) + t.autoend() + + t.test('should name transaction after status `method now allowed` message', (t) => { + const { agent, router, app } = t.context + router.post('/:first', function firstMiddleware() {}) + app.use(router.routes()) + app.use(router.allowedMethods({ throw: true })) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET/(method not allowed)', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/allowedMethods']] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET/(method not allowed)', + 'transaction should be named after corresponding status code message' + ) + const errors = agent.errors.eventAggregator + t.equal(errors.length, 1, 'the error has been recorded') + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name transaction after status `not implemented` message', (t) => { + const { agent, Router, app } = t.context + const router = new Router({ methods: ['POST'] }) + router.post('/:first', function firstMiddleware() {}) + app.use(router.routes()) + app.use(router.allowedMethods({ throw: true })) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET/(not implemented)', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/allowedMethods']] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET/(not implemented)', + 'transaction should be named after corresponding status code message' + ) + + const errors = agent.errors.eventAggregator + t.equal(errors.length, 1, 'the error has been recorded') + t.end() + }) + run({ context: t.context }) + }) + + t.test('error handler normalizes tx name if body is reset without status', (t) => { + const { agent, router, Router, app } = t.context + app.use(function errorHandler(ctx, next) { + return next().catch(() => { + // resetting the body without manually persisting ctx.status + // results in status 200 + ctx.body = { msg: 'error is handled' } + }) + }) + + const nestedRouter = new Router() + nestedRouter.post('/:second', function terminalMiddleware(ctx) { + ctx.body = 'would want this to set name if verb were correct' + }) + router.use('/:first', nestedRouter.routes(), nestedRouter.allowedMethods()) + app.use(router.routes()) + app.use(router.allowedMethods({ throw: true })) + + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/NormalizedUri/*', + [ + 'Nodejs/Middleware/Koa/errorHandler', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/allowedMethods']] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/NormalizedUri/*', + 'should have normalized transaction name' + ) + const errors = agent.errors.eventAggregator + t.equal(errors.length, 0, 'error should not be recorded') + t.end() + }) + run({ path: '/123/456', context: t.context }) + }) + + t.test( + 'should name tx after status message when base middleware does not set body', + (t) => { + const { agent, router, Router, app } = t.context + // Because allowedMethods throws & no user catching, it is considered + // unhandled and will push the base route back on + app.use(function baseMiddleware(ctx, next) { + return next() + // does not set ctx.body or ctx.status + }) + + const nestedRouter = new Router() + nestedRouter.post('/:second', function terminalMiddleware(ctx) { + ctx.body = 'would want this to set name if verb were correct' + }) + router.use('/:first', nestedRouter.routes(), nestedRouter.allowedMethods()) + app.use(router.routes()) + app.use(router.allowedMethods({ throw: true })) + + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET/(method not allowed)', + [ + 'Nodejs/Middleware/Koa/baseMiddleware', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/allowedMethods']] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET/(method not allowed)', + 'should name after returned status code' + ) + const errors = agent.errors.eventAggregator + t.equal(errors.length, 1, 'should notice thrown error') + + t.end() + }) + run({ path: '/123/456', context: t.context }) + } + ) + }) + + t.test('with throw: false', (t) => { + t.beforeEach(testSetup) + t.afterEach(tearDown) + t.autoend() + + t.test('should name transaction after status `method now allowed` message', (t) => { + const { agent, router, app } = t.context + router.post('/:first', function firstMiddleware() {}) + app.use(router.routes()) + app.use(router.allowedMethods()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET/(method not allowed)', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/allowedMethods']] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET/(method not allowed)', + 'transaction should be named after corresponding status code message' + ) + // Agent will automatically create error for 405 status code. + const errors = agent.errors.eventAggregator + t.equal(errors.length, 1, 'the error has been recorded') + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name transaction after status `not implemented` message', (t) => { + const { agent, app, Router } = t.context + const router = new Router({ methods: ['POST'] }) + router.post('/:first', function firstMiddleware() {}) + app.use(router.routes()) + app.use(router.allowedMethods()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET/(not implemented)', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/allowedMethods']] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET/(not implemented)', + 'transaction should be named after corresponding status code message' + ) + // Agent will automatically create error for 501 status code. + const errors = agent.errors.eventAggregator + t.equal(errors.length, 1, 'the error has been recorded') + t.end() + }) + run({ context: t.context }) + }) + + t.test('should name tx after `method not allowed` with prefixed router', (t) => { + const { agent, router, app } = t.context + app.use(function appLevelMiddleware(ctx, next) { + return next().then(() => { + ctx.body = 'should not set the name' + }) + }) + router.post('/second', function secondMiddleware(ctx) { + ctx.body = 'should not set the name' + }) + router.prefix('/:first') + app.use(router.routes()) + app.use(router.allowedMethods()) + + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET/(method not allowed)', + [ + 'Nodejs/Middleware/Koa/appLevelMiddleware', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/allowedMethods']] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET/(method not allowed)', + 'transaction should be named after corresponding status code message' + ) + t.end() + }) + run({ path: '/123/second', context: t.context }) + }) + + t.test('should name tx after `not implemented` with prefixed router', (t) => { + const { agent, app, Router } = t.context + const router = new Router({ methods: ['POST'] }) + + app.use(function appLevelMiddleware(ctx, next) { + return next().then(() => { + ctx.body = 'should not set the name' + }) + }) + router.post('/second', function secondMiddleware(ctx) { + ctx.body = 'should not set the name' + }) + router.prefix('/:first') + app.use(router.routes()) + app.use(router.allowedMethods()) + + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET/(not implemented)', + [ + 'Nodejs/Middleware/Koa/appLevelMiddleware', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/allowedMethods']] + ] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET/(not implemented)', + 'transaction should be named after corresponding status code message' + ) + t.end() + }) + run({ path: '/123/first', context: t.context }) + }) + + t.test('should name and produce segments for existing matched path', (t) => { + const { agent, app, Router } = t.context + const router = new Router({ methods: ['GET'] }) + router.get('/:first', function firstMiddleware(ctx) { + ctx.body = 'first' + }) + app.use(router.routes()) + app.use(router.allowedMethods()) + agent.on('transactionFinished', (tx) => { + t.assertSegments(tx.trace.root, [ + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + ['Koa/Router: /', ['Nodejs/Middleware/Koa/firstMiddleware//:first']] + ]) + t.equal( + tx.name, + 'WebTransaction/WebFrameworkUri/Koa/GET//:first', + 'transaction should be named after the matched path' + ) + t.end() + }) + run({ context: t.context }) + }) + }) + }) + t.end() + }) +} diff --git a/test/versioned/koa/scoped-koa-router.tap.js b/test/versioned/koa/scoped-koa-router.tap.js new file mode 100644 index 0000000000..fad2ac5ff2 --- /dev/null +++ b/test/versioned/koa/scoped-koa-router.tap.js @@ -0,0 +1,7 @@ +/* + * Copyright 2020 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' +require('./router-common')('@koa/router') diff --git a/test/versioned/koa/utils.js b/test/versioned/koa/utils.js new file mode 100644 index 0000000000..c367aca4af --- /dev/null +++ b/test/versioned/koa/utils.js @@ -0,0 +1,19 @@ +/* + * Copyright 2024 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' +const utils = module.exports +const http = require('http') + +utils.run = function run({ path = '/123', context }) { + context.server = context.app.listen(0, function () { + http + .get({ + port: context.server.address().port, + path + }) + .end() + }) +} diff --git a/third_party_manifest.json b/third_party_manifest.json index 3b922b6895..72d60bca56 100644 --- a/third_party_manifest.json +++ b/third_party_manifest.json @@ -1,5 +1,5 @@ { - "lastUpdated": "Mon Apr 15 2024 12:30:33 GMT-0400 (Eastern Daylight Time)", + "lastUpdated": "Tue Apr 16 2024 15:55:51 GMT-0400 (Eastern Daylight Time)", "projectName": "New Relic Node Agent", "projectUrl": "https://github.com/newrelic/node-newrelic", "includeOptDeps": true, @@ -81,19 +81,6 @@ "publisher": "New Relic Node.js agent team", "email": "nodejs@newrelic.com" }, - "@newrelic/koa@9.1.0": { - "name": "@newrelic/koa", - "version": "9.1.0", - "range": "^9.1.0", - "licenses": "Apache-2.0", - "repoUrl": "https://github.com/newrelic/node-newrelic-koa", - "versionedRepoUrl": "https://github.com/newrelic/node-newrelic-koa/tree/v9.1.0", - "licenseFile": "node_modules/@newrelic/koa/LICENSE", - "licenseUrl": "https://github.com/newrelic/node-newrelic-koa/blob/v9.1.0/LICENSE", - "licenseTextSource": "file", - "publisher": "New Relic Node.js agent team", - "email": "nodejs@newrelic.com" - }, "@newrelic/ritm@7.2.0": { "name": "@newrelic/ritm", "version": "7.2.0", @@ -252,6 +239,19 @@ } }, "devDependencies": { + "@koa/router@12.0.1": { + "name": "@koa/router", + "version": "12.0.1", + "range": "^12.0.1", + "licenses": "MIT", + "repoUrl": "https://github.com/koajs/router", + "versionedRepoUrl": "https://github.com/koajs/router/tree/v12.0.1", + "licenseFile": "node_modules/@koa/router/LICENSE", + "licenseUrl": "https://github.com/koajs/router/blob/v12.0.1/LICENSE", + "licenseTextSource": "file", + "publisher": "Alex Mingoia", + "email": "talk@alexmingoia.com" + }, "@newrelic/eslint-config@0.3.0": { "name": "@newrelic/eslint-config", "version": "0.3.0", @@ -544,6 +544,41 @@ "publisher": "Michael Mathews", "email": "micmath@gmail.com" }, + "koa-route@4.0.1": { + "name": "koa-route", + "version": "4.0.1", + "range": "^4.0.1", + "licenses": "MIT", + "repoUrl": "https://github.com/koajs/route", + "versionedRepoUrl": "https://github.com/koajs/route/tree/v4.0.1", + "licenseFile": "node_modules/koa-route/Readme.md", + "licenseUrl": "https://github.com/koajs/route/blob/v4.0.1/Readme.md", + "licenseTextSource": "spdx" + }, + "koa-router@12.0.1": { + "name": "koa-router", + "version": "12.0.1", + "range": "^12.0.1", + "licenses": "MIT", + "repoUrl": "https://github.com/koajs/router", + "versionedRepoUrl": "https://github.com/koajs/router/tree/v12.0.1", + "licenseFile": "node_modules/koa-router/LICENSE", + "licenseUrl": "https://github.com/koajs/router/blob/v12.0.1/LICENSE", + "licenseTextSource": "file", + "publisher": "Alex Mingoia", + "email": "talk@alexmingoia.com" + }, + "koa@2.15.3": { + "name": "koa", + "version": "2.15.3", + "range": "^2.15.3", + "licenses": "MIT", + "repoUrl": "https://github.com/koajs/koa", + "versionedRepoUrl": "https://github.com/koajs/koa/tree/v2.15.3", + "licenseFile": "node_modules/koa/LICENSE", + "licenseUrl": "https://github.com/koajs/koa/blob/v2.15.3/LICENSE", + "licenseTextSource": "file" + }, "lint-staged@11.2.6": { "name": "lint-staged", "version": "11.2.6", @@ -675,6 +710,19 @@ "licenseTextSource": "file", "publisher": "Christian Johansen" }, + "superagent@8.1.2": { + "name": "superagent", + "version": "8.1.2", + "range": "^8.1.2", + "licenses": "MIT", + "repoUrl": "https://github.com/ladjs/superagent", + "versionedRepoUrl": "https://github.com/ladjs/superagent/tree/v8.1.2", + "licenseFile": "node_modules/superagent/LICENSE", + "licenseUrl": "https://github.com/ladjs/superagent/blob/v8.1.2/LICENSE", + "licenseTextSource": "file", + "publisher": "TJ Holowaychuk", + "email": "tj@vision-media.ca" + }, "tap@16.3.10": { "name": "tap", "version": "16.3.10",