From bc40b6ae2e73f267b5d80a263d7ab1d7db94e85f Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Thu, 13 Jan 2022 23:30:01 +0900 Subject: [PATCH 01/14] [#104] make koa example --- demo/koa-example/index.js | 17 +++++++++++++++++ demo/koa-example/package.json | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 demo/koa-example/index.js create mode 100644 demo/koa-example/package.json diff --git a/demo/koa-example/index.js b/demo/koa-example/index.js new file mode 100644 index 00000000..2750da1c --- /dev/null +++ b/demo/koa-example/index.js @@ -0,0 +1,17 @@ +'use strict' + +const Koa = require('koa') +const Router = require('@koa/router') + +const app = new Koa() +const router = new Router() + +router.get('/', (ctx, next) => { + ctx.body = 'Hello World' +}) + +app + .use(router.routes()) + .use(router.allowedMethods()) + +app.listen(1234) \ No newline at end of file diff --git a/demo/koa-example/package.json b/demo/koa-example/package.json new file mode 100644 index 00000000..69a074bd --- /dev/null +++ b/demo/koa-example/package.json @@ -0,0 +1,16 @@ +{ + "name": "koa-example", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "@koa/router": "^10.0.0", + "koa": "^2.13.1" + } +} From d14ab986b650a4af732bfd52a700cf04006b9af9 Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Mon, 17 Jan 2022 23:43:50 +0900 Subject: [PATCH 02/14] [#104] koa support code level visibility --- test/instrumentation/module/koa.test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/instrumentation/module/koa.test.js b/test/instrumentation/module/koa.test.js index cd02d967..e50676a0 100644 --- a/test/instrumentation/module/koa.test.js +++ b/test/instrumentation/module/koa.test.js @@ -33,6 +33,10 @@ test(`${testName1} Should record request in basic route koa.test.js`, function ( router.get(PATH, async (ctx, next) => { ctx.body = 'ok. get' + process.nextTick(() => { + + const trace = agent.traceContext.currentTraceObject() + }) }) router.post(PATH, async (ctx, next) => { ctx.body = 'ok. post' From c963e5403655a77c3346e4983a3fc8aa1e35faec Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Tue, 18 Jan 2022 15:55:14 +0900 Subject: [PATCH 03/14] [#104] remove no used module hook --- lib/instrumentation/module-hook.js | 5 +- lib/instrumentation/module/koa-router.js | 22 +----- lib/instrumentation/module/koa.js | 90 +----------------------- test/instrumentation/module/koa.test.js | 2 +- 4 files changed, 7 insertions(+), 112 deletions(-) diff --git a/lib/instrumentation/module-hook.js b/lib/instrumentation/module-hook.js index 0111aaba..747b146c 100644 --- a/lib/instrumentation/module-hook.js +++ b/lib/instrumentation/module-hook.js @@ -15,7 +15,6 @@ const MODULES = [ 'express', 'http', 'https', - // 'koa', 'koa-router', 'redis', 'ioredis', @@ -67,8 +66,8 @@ class ModuleHook { } catch (error) { log.error('error occurred', error) } - } - + } + if (!version) { version = process.versions.node } diff --git a/lib/instrumentation/module/koa-router.js b/lib/instrumentation/module/koa-router.js index b164daec..8e3f4ec6 100644 --- a/lib/instrumentation/module/koa-router.js +++ b/lib/instrumentation/module/koa-router.js @@ -13,36 +13,20 @@ const log = require('../../utils/logger') const MethodDescriptor = require('../../context/method-descriptor') const apiMetaService = require('../../context/api-meta-service') -module.exports = function(agent, version, router) { +module.exports = function (agent, version, router) { if (!semver.satisfies(version, '>=5.2.0 <8')) { log.debug('koa-router version %s not supported - aborting...', version) return router } - const methodDescriptorMap = new Map() - - const MODULE_NAME = 'koa' - const OBJECT_NAME = 'router' - - function getMethodDescriptor (methodName) { - return methodDescriptorMap.get(methodName)|| setMethodDescriptor(methodName) - } - - function setMethodDescriptor(methodName) { - const descriptor = MethodDescriptor.create(MODULE_NAME, OBJECT_NAME, methodName) - apiMetaService.cacheApi(descriptor) - methodDescriptorMap.set(methodName, descriptor) - return descriptor - } - - shimmer.wrap(router.prototype, 'register', function(original) { + shimmer.wrap(router.prototype, 'register', function (original) { return function (path, methods, middleware, opts) { const layer = original.apply(this, arguments) if (layer.stack) { layer.stack.forEach((fn, index) => { if (typeof fn === 'function') { let result - layer.stack[index] = async function(ctx, next) { + layer.stack[index] = async function (ctx, next) { const name = fn.name || 'AnonymousFunction' const trace = agent.traceContext.currentTraceObject() let spanEventRecorder = null diff --git a/lib/instrumentation/module/koa.js b/lib/instrumentation/module/koa.js index 18c3a3e2..16c0fa58 100644 --- a/lib/instrumentation/module/koa.js +++ b/lib/instrumentation/module/koa.js @@ -11,94 +11,6 @@ const shimmer = require('shimmer') const ServiceTypeCode = require('../../constant/service-type').ServiceTypeCode const log = require('../../utils/logger') -module.exports = function(agent, version, koa) { - - // todo. emit 체크 - shimmer.wrap(koa.prototype, 'emit', function(original) { - return function (evt, err, ctx) { - const trace = agent.traceContext.currentTraceObject() - let spanEventRecorder = null - if (evt === 'error' & ctx) { - spanEventRecorder = trace.traceBlockBegin() - spanEventRecorder.recordServiceType(ServiceTypeCode.koa) - spanEventRecorder.recordApiDesc('koa') - spanEventRecorder.recordException(err, true) - trace.traceBlockEnd(spanEventRecorder) - return original.apply(this, arguments) - } - if (trace) { - spanEventRecorder = trace.traceBlockBegin() - spanEventRecorder.recordServiceType(ServiceTypeCode.koa) - spanEventRecorder.recordApiDesc('koa') // Todo. check this - } - const result = original.apply(this, arguments) - if (trace) { - trace.traceBlockEnd(spanEventRecorder) - } - return result - } - }) - shimmer.wrap(koa.prototype, 'use', function(original) { - return function wrapMiddleware(middleware) { - if (typeof middleware === 'function') { - const cb = arguments[0] - const name = middleware.name || 'AnonymousFunction' - // todo. 기본로직을 제외 하는 방법 검토할 것 - // if (cb.toString().trim().match(/^async/)) {} - arguments[0] = async function(ctx, next) { - let result - const trace = agent.traceContext.currentTraceObject() - let spanEventRecorder = null - try { - if (trace) { - spanEventRecorder = trace.traceBlockBegin() - spanEventRecorder.recordServiceType(ServiceTypeCode.koa) - spanEventRecorder.recordApiDesc(`middleware [${name}]`) - } - result = await cb.apply(this, arguments) - } catch (e) { - if (!e._pinpointCheck) { - e._pinpointCheck = true - spanEventRecorder.recordServiceType(ServiceTypeCode.koa) - spanEventRecorder.recordApiDesc(`middleware [${name}]`) - spanEventRecorder.recordException(e, true) - } - throw e - } finally { - if (trace) { - trace.traceBlockEnd(spanEventRecorder) - } - } - return result - } - - } - return original.apply(this, arguments) - } - }) - // Test 3. createContext - // shimmer.wrap(koa.prototype, 'createContext', function(original) { - // return function wrapCreateContext(req, res) { - // const result = original.apply(this, arguments) - // - // shimmer.wrap(result, 'onerror', function(ori) { - // return function (err) { - // const trace = agent.traceContext.currentTraceObject() - // if (trace) { - // const spanEventRecorder = trace.traceBlockBegin() - // spanEventRecorder.recordServiceType(ServiceTypeCode.express) - // spanEventRecorder.recordApiDesc('tes') - // spanEventRecorder.recordException(err, true) - // trace.traceBlockEnd(spanEventRecorder) - // } - // return ori.apply(this, arguments) - // } - // }) - // - // - // - // return result - // } - // }) +module.exports = function (agent, version, koa) { return koa } diff --git a/test/instrumentation/module/koa.test.js b/test/instrumentation/module/koa.test.js index e50676a0..74ec6fd2 100644 --- a/test/instrumentation/module/koa.test.js +++ b/test/instrumentation/module/koa.test.js @@ -33,9 +33,9 @@ test(`${testName1} Should record request in basic route koa.test.js`, function ( router.get(PATH, async (ctx, next) => { ctx.body = 'ok. get' + const trace = agent.traceContext.currentTraceObject() process.nextTick(() => { - const trace = agent.traceContext.currentTraceObject() }) }) router.post(PATH, async (ctx, next) => { From 4b7259186c257cf39a353ed1eac476004bac9637 Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Tue, 18 Jan 2022 23:55:31 +0900 Subject: [PATCH 04/14] [#104] koa support code level visibility --- lib/instrumentation/module/koa-router.js | 11 +++++++++++ test/instrumentation/module/koa.test.js | 3 --- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/instrumentation/module/koa-router.js b/lib/instrumentation/module/koa-router.js index 8e3f4ec6..906180ff 100644 --- a/lib/instrumentation/module/koa-router.js +++ b/lib/instrumentation/module/koa-router.js @@ -59,5 +59,16 @@ module.exports = function (agent, version, router) { } }) + shimmer.wrap(router.prototype, 'match', function (original) { + return function (_, method) { + var matched = original.apply(this, arguments) + if (typeof method !== 'string') { + log.debug('unexpected method type in koa-router prototype.match: %s', typeof method) + return matched + } + return matched + } + }) + return router } diff --git a/test/instrumentation/module/koa.test.js b/test/instrumentation/module/koa.test.js index 74ec6fd2..e1336e48 100644 --- a/test/instrumentation/module/koa.test.js +++ b/test/instrumentation/module/koa.test.js @@ -34,9 +34,6 @@ test(`${testName1} Should record request in basic route koa.test.js`, function ( router.get(PATH, async (ctx, next) => { ctx.body = 'ok. get' const trace = agent.traceContext.currentTraceObject() - process.nextTick(() => { - - }) }) router.post(PATH, async (ctx, next) => { ctx.body = 'ok. post' From cea53087a8c2d674f388ab790018da9ddf683779 Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Wed, 19 Jan 2022 23:41:13 +0900 Subject: [PATCH 05/14] [#104] koa wrap guard --- lib/instrumentation/module/koa-router.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/instrumentation/module/koa-router.js b/lib/instrumentation/module/koa-router.js index 906180ff..a32db813 100644 --- a/lib/instrumentation/module/koa-router.js +++ b/lib/instrumentation/module/koa-router.js @@ -10,8 +10,7 @@ const semver = require('semver') const shimmer = require('shimmer') const ServiceTypeCode = require('../../constant/service-type').ServiceTypeCode const log = require('../../utils/logger') -const MethodDescriptor = require('../../context/method-descriptor') -const apiMetaService = require('../../context/api-meta-service') +const layerPatchedSymbol = Symbol('PinpointKoaLayerPatched') module.exports = function (agent, version, router) { if (!semver.satisfies(version, '>=5.2.0 <8')) { @@ -22,6 +21,12 @@ module.exports = function (agent, version, router) { shimmer.wrap(router.prototype, 'register', function (original) { return function (path, methods, middleware, opts) { const layer = original.apply(this, arguments) + + if (layer[layerPatchedSymbol]) { + return layer + } + + layer[layerPatchedSymbol] = true if (layer.stack) { layer.stack.forEach((fn, index) => { if (typeof fn === 'function') { From 72b2afd223be4ebe7110ca89c080bd689dc7ff52 Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Wed, 19 Jan 2022 23:46:30 +0900 Subject: [PATCH 06/14] [#104] koa module parameter descriptor https://github.com/koajs/router/blob/master/API.md router.get('/', (ctx, next) => { // ctx.router available }); --- lib/context/koa-method-descriptor-builder.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lib/context/koa-method-descriptor-builder.js diff --git a/lib/context/koa-method-descriptor-builder.js b/lib/context/koa-method-descriptor-builder.js new file mode 100644 index 00000000..20aeb051 --- /dev/null +++ b/lib/context/koa-method-descriptor-builder.js @@ -0,0 +1,18 @@ +/** + * Pinpoint Node.js Agent + * Copyright 2022-present NAVER Corp. + * Apache License v2.0 + */ + +'use strict' + +class KoaMethodDescriptorBuilder { + static make(builder) { + if (!builder) { + return + } + return builder.setParameterDescriptor('(ctx, next)') + } +} + +module.exports = KoaMethodDescriptorBuilder \ No newline at end of file From bccb6ee3bc4ddd04b17fa47b8f349ea82e72c3cc Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Mon, 24 Jan 2022 14:48:06 +0900 Subject: [PATCH 07/14] [#104] remove no needs files --- lib/instrumentation/module/koa-route.js | 36 ------------------------- 1 file changed, 36 deletions(-) delete mode 100644 lib/instrumentation/module/koa-route.js diff --git a/lib/instrumentation/module/koa-route.js b/lib/instrumentation/module/koa-route.js deleted file mode 100644 index 6ac00c61..00000000 --- a/lib/instrumentation/module/koa-route.js +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Pinpoint Node.js Agent - * Copyright 2020-present NAVER Corp. - * Apache License v2.0 - */ - -'use strict' - -const shimmer = require('shimmer') -const ServiceTypeCode = require('../../constant/service-type').ServiceTypeCode -const methods = require('methods') - -module.exports = function(agent, version, route) { - // todo. route 쓸지 검토 - methods.forEach(function (method) { - shimmer.wrap(route, method, function (shimmer, methodFn) { - return function () { - const trace = agent.traceContext.currentTraceObject() - let spanEventRecorder - if (trace) { - spanEventRecorder = trace.traceBlockBegin() - spanEventRecorder.recordServiceType(ServiceTypeCode.koa) - spanEventRecorder.recordApi('koa.route.get') - } - const result = methodFn.apply(route, arguments) - if (trace) { - trace.traceBlockEnd(spanEventRecorder) - } - - return result - } - }) - }) - - return route -} From 68866b92c452a8a975e93c4fc9bec0cb6503a655 Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Wed, 26 Jan 2022 18:36:45 +0900 Subject: [PATCH 08/14] [#104] koa module https://github.com/koajs/router/blob/1aead99e0e0fdb8666e9c6fa2f52b0463c622025/lib/layer.js#L25 --- lib/instrumentation/module/koa-router.js | 72 ++++++++++++++---------- test/instrumentation/module/koa.test.js | 5 +- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/lib/instrumentation/module/koa-router.js b/lib/instrumentation/module/koa-router.js index a32db813..ee45ef1c 100644 --- a/lib/instrumentation/module/koa-router.js +++ b/lib/instrumentation/module/koa-router.js @@ -11,6 +11,9 @@ const shimmer = require('shimmer') const ServiceTypeCode = require('../../constant/service-type').ServiceTypeCode const log = require('../../utils/logger') const layerPatchedSymbol = Symbol('PinpointKoaLayerPatched') +const { makeMethodDescriptorBuilder } = require('../../context/make-method-descriptor-builder') +const KoaMethodDescriptorBuilder = require('../../context/koa-method-descriptor-builder') +const apiMetaService = require('../../context/api-meta-service') module.exports = function (agent, version, router) { if (!semver.satisfies(version, '>=5.2.0 <8')) { @@ -27,38 +30,45 @@ module.exports = function (agent, version, router) { } layer[layerPatchedSymbol] = true - if (layer.stack) { - layer.stack.forEach((fn, index) => { - if (typeof fn === 'function') { - let result - layer.stack[index] = async function (ctx, next) { - const name = fn.name || 'AnonymousFunction' - const trace = agent.traceContext.currentTraceObject() - let spanEventRecorder = null - try { - if (trace) { - spanEventRecorder = trace.traceBlockBegin() - spanEventRecorder.recordServiceType(ServiceTypeCode.koa) - spanEventRecorder.recordApiDesc(`koa.router.${ctx.method.toLocaleLowerCase()} [${name}]`) - } - result = await fn.apply(this, arguments) - } catch (e) { - if (!e._pinpointCheck) { - e._pinpointCheck = true - spanEventRecorder.recordServiceType(ServiceTypeCode.koa) - spanEventRecorder.recordApiDesc(`koa.router.${ctx.method.toLocaleLowerCase()} [${name}]`) - spanEventRecorder.recordException(e, true) - } - throw e - } finally { - if (trace) { - trace.traceBlockEnd(spanEventRecorder) - } - } - return result - } + + if (!Array.isArray(layer.stack) || layer.stack.length < 1) { + return layer + } + + const handlerIndex = layer.stack.length - 1 + const fn = layer.stack[handlerIndex] + if (typeof fn !== 'function') { + return layer + } + + const builder = KoaMethodDescriptorBuilder.make(makeMethodDescriptorBuilder('koa', 3, 4)) + const methodDescriptor = apiMetaService.cacheApiWithBuilder(builder) + layer.stack[handlerIndex] = async function (ctx, next) { + const name = fn.name || 'AnonymousFunction' + const trace = agent.traceContext.currentTraceObject() + let spanEventRecorder = null + let result + try { + if (trace) { + spanEventRecorder = trace.traceBlockBegin() + spanEventRecorder.recordServiceType(ServiceTypeCode.koa) + spanEventRecorder.recordApiDesc(`koa.router.${ctx.method.toLocaleLowerCase()} [${name}]`) + } + result = await fn.apply(this, arguments) + } catch (e) { + if (!e._pinpointCheck) { + e._pinpointCheck = true + spanEventRecorder.recordServiceType(ServiceTypeCode.koa) + spanEventRecorder.recordApiDesc(`koa.router.${ctx.method.toLocaleLowerCase()} [${name}]`) + spanEventRecorder.recordException(e, true) + } + throw e + } finally { + if (trace) { + trace.traceBlockEnd(spanEventRecorder) } - }) + } + return result } return layer } diff --git a/test/instrumentation/module/koa.test.js b/test/instrumentation/module/koa.test.js index e1336e48..5cc0e08e 100644 --- a/test/instrumentation/module/koa.test.js +++ b/test/instrumentation/module/koa.test.js @@ -7,11 +7,8 @@ const test = require('tape') const axios = require('axios') -const { log, fixture, util, enableDataSending } = require('../../test-helper') - - +const { log } = require('../../test-helper') const agent = require('../../support/agent-singleton-mock') - const Koa = require('koa') const Router = require('koa-router') From e91162a643c87fa942c0b44a08bdc44bf1623e72 Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Thu, 27 Jan 2022 23:37:53 +0900 Subject: [PATCH 09/14] [#104] koa moudle callstack parsing --- lib/instrumentation/module/koa-router.js | 2 +- .../make-method-descriptor-builder.test.js | 46 ++++++++++++++----- .../context/method-descriptor-builder.test.js | 2 +- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/lib/instrumentation/module/koa-router.js b/lib/instrumentation/module/koa-router.js index ee45ef1c..f9b98e96 100644 --- a/lib/instrumentation/module/koa-router.js +++ b/lib/instrumentation/module/koa-router.js @@ -41,7 +41,7 @@ module.exports = function (agent, version, router) { return layer } - const builder = KoaMethodDescriptorBuilder.make(makeMethodDescriptorBuilder('koa', 3, 4)) + const builder = KoaMethodDescriptorBuilder.make(makeMethodDescriptorBuilder('koa', 2, 3)) const methodDescriptor = apiMetaService.cacheApiWithBuilder(builder) layer.stack[handlerIndex] = async function (ctx, next) { const name = fn.name || 'AnonymousFunction' diff --git a/test/context/make-method-descriptor-builder.test.js b/test/context/make-method-descriptor-builder.test.js index ea769dc2..616aeeca 100644 --- a/test/context/make-method-descriptor-builder.test.js +++ b/test/context/make-method-descriptor-builder.test.js @@ -9,20 +9,20 @@ const { captureNamedGroup } = require('../../lib/context/make-method-descriptor- const MethodDescriptorBuilder = require('../../lib/context/method-descriptor-builder') const actualCallStack = `Error -at patchLayer (/Users/feelform/workspace/pinpoint/pinpoint-node-agent/lib/instrumentation/module/express.js:83:65) -at Function.route (/Users/feelform/workspace/pinpoint/pinpoint-node-agent/lib/instrumentation/module/express.js:73:9) -at Function.app. [as get] (/Users/feelform/workspace/pinpoint/pinpoint-node-agent/node_modules/express/lib/application.js:481:30) -at /Users/feelform/workspace/pinpoint/pinpoint-node-agent/test/utils/ant-path-matcher.test.js:206:13 +at patchLayer (/Users/test/workspace/pinpoint/pinpoint-node-agent/lib/instrumentation/module/express.js:83:65) +at Function.route (/Users/test/workspace/pinpoint/pinpoint-node-agent/lib/instrumentation/module/express.js:73:9) +at Function.app. [as get] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/express/lib/application.js:481:30) +at /Users/test/workspace/pinpoint/pinpoint-node-agent/test/utils/ant-path-matcher.test.js:206:13 at new Promise () -at outgoingRequest (/Users/feelform/workspace/pinpoint/pinpoint-node-agent/test/utils/ant-path-matcher.test.js:194:12) -at Test. (/Users/feelform/workspace/pinpoint/pinpoint-node-agent/test/utils/ant-path-matcher.test.js:181:11) -at Test.bound [as _cb] (/Users/feelform/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/test.js:80:32) -at Test.run (/Users/feelform/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/test.js:96:10) -at Test.bound [as run] (/Users/feelform/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/test.js:80:32) -at Immediate.next [as _onImmediate] (/Users/feelform/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/results.js:83:19) +at outgoingRequest (/Users/test/workspace/pinpoint/pinpoint-node-agent/test/utils/ant-path-matcher.test.js:194:12) +at Test. (/Users/test/workspace/pinpoint/pinpoint-node-agent/test/utils/ant-path-matcher.test.js:181:11) +at Test.bound [as _cb] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/test.js:80:32) +at Test.run (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/test.js:96:10) +at Test.bound [as run] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/test.js:80:32) +at Immediate.next [as _onImmediate] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/results.js:83:19) at processImmediate (internal/timers.js:456:21)` -test('makeMethodDescriptorBuilder', (t) => { +test('express makeMethodDescriptorBuilder', (t) => { let actual = captureNamedGroup('at new functionName (internal/modules/cjs/loader.js:699:10)') t.equal(actual.type, undefined, 'className') t.equal(actual.fileName, 'loader.js', 'fileName') @@ -56,7 +56,7 @@ test('makeMethodDescriptorBuilder', (t) => { t.end() }) -test('makeMethodDescriptorBuilder exception case', (t) => { +test('express makeMethodDescriptorBuilder exception case', (t) => { const stacks = actualCallStack.split(/\r?\n/) let actual = captureNamedGroup(stacks[5]) //at new Promise () t.equal(actual.type, undefined, 'className') @@ -70,4 +70,26 @@ test('makeMethodDescriptorBuilder exception case', (t) => { t.equal(actualMethodDescriptor.getLineNumber(), undefined, 'lineNumber actualMethodDescriptor') t.equal(actualMethodDescriptor.getLocation(), '', 'location actualMethodDescriptor') t.end() +}) + +const actualKoaCallStack = `Error +at Router.register (/Users/test/workspace/pinpoint/pinpoint-node-agent/lib/instrumentation/module/koa-router.js:44:55) +at Router. [as get] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/koa-router/lib/router.js:202:10) +at Test. (/Users/test/workspace/pinpoint/pinpoint-node-agent/test/instrumentation/module/koa.test.js:31:10) +at Test.bound [as _cb] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/test.js:80:32) +at Test.run (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/test.js:96:10) +at Test.bound [as run] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/test.js:80:32) +at Immediate.next [as _onImmediate] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/results.js:83:19) +at processImmediate (internal/timers.js:464:21) +at process.callbackTrampoline (internal/async_hooks.js:130:17)` +test('koa makeMethodDescriptorBuilder', (t) => { + const stacks = actualKoaCallStack.split(/\r?\n/) + let actual = captureNamedGroup(stacks[2]) //at Router. [as get] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/koa-router/lib/router.js:202:10) + t.equal(actual.type, 'Router', 'className') + t.equal(actual.fileName, 'router.js', 'fileName') + t.equal(actual.functionName, '', 'functionName') + t.equal(actual.lineNumber, '202', 'lineNumber') + t.true(actual.location.endsWith('koa-router/lib/'), 'location') + t.equal(actual.methodName, 'get', 'methodName') + t.end() }) \ No newline at end of file diff --git a/test/context/method-descriptor-builder.test.js b/test/context/method-descriptor-builder.test.js index ac9ba3f0..32321435 100644 --- a/test/context/method-descriptor-builder.test.js +++ b/test/context/method-descriptor-builder.test.js @@ -7,7 +7,7 @@ const test = require('tape') const MethodDescriptorBuilder = require('../../lib/context/method-descriptor-builder') -test('callstack', (t) => { +test('express callstack', (t) => { const regex = /at (?\w+(?=\.))?\.?(?[^\s]+)(?: \[as (?\w+)\])? \(.+\/(?[^:/]+):(?[0-9]+):(?[0-9]+)\)$/ let stack = 'at Function.app. [as get] (/Users/feelform/workspace/pinpoint/pinpoint-node-agent/node_modules/express/lib/application.js:481:30)' From a3ec75a2c5d2f324ee418a62a5f2ef06d9c07e6b Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Thu, 3 Feb 2022 17:22:35 +0900 Subject: [PATCH 10/14] [#104] Support call stack `Router. [as get]` with `Function.app. [as get]` --- lib/context/method-descriptor-builder.js | 3 ++ .../make-method-descriptor-builder.test.js | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/context/method-descriptor-builder.js b/lib/context/method-descriptor-builder.js index e3c8fd41..a6295cd2 100644 --- a/lib/context/method-descriptor-builder.js +++ b/lib/context/method-descriptor-builder.js @@ -214,6 +214,9 @@ function makeFunctionName(namedGroup) { if (computedGroups && computedGroups.length > 0 && namedGroup.methodName) { return computedGroups.groups.functionName + namedGroup.methodName } + if (functionName === '' && typeof namedGroup.type === 'string' && typeof namedGroup.methodName === 'string') { + return namedGroup.methodName + } return namedGroup.functionName } diff --git a/test/context/make-method-descriptor-builder.test.js b/test/context/make-method-descriptor-builder.test.js index 616aeeca..5b1aa2e0 100644 --- a/test/context/make-method-descriptor-builder.test.js +++ b/test/context/make-method-descriptor-builder.test.js @@ -7,6 +7,8 @@ const test = require('tape') const { captureNamedGroup } = require('../../lib/context/make-method-descriptor-builder') const MethodDescriptorBuilder = require('../../lib/context/method-descriptor-builder') +const KoaMethodDescriptorBuilder = require('../../lib/context/koa-method-descriptor-builder') +const ExpressMethodDescriptorBuilder = require('../../lib/context/express-method-descriptor-builder') const actualCallStack = `Error at patchLayer (/Users/test/workspace/pinpoint/pinpoint-node-agent/lib/instrumentation/module/express.js:83:65) @@ -53,6 +55,24 @@ test('express makeMethodDescriptorBuilder', (t) => { actualMethodDescriptor = MethodDescriptorBuilder.make(undefined, actual).build() t.equal(actualMethodDescriptor.getApiDescriptor(), 'FunctionName()') + const stacks = actualCallStack.split(/\r?\n/) + actual = captureNamedGroup(stacks[3]) //at Function.app. [as get] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/express/lib/application.js:481:30) + t.equal(actual.type, 'Function', 'className') + t.equal(actual.fileName, 'application.js', 'fileName') + t.equal(actual.functionName, 'app.', 'functionName') + t.equal(actual.lineNumber, '481', 'lineNumber') + t.true(actual.location.endsWith('express/lib/'), 'location') + t.equal(actual.methodName, 'get', 'methodName') + + const methodDescriptor = ExpressMethodDescriptorBuilder.make(MethodDescriptorBuilder.make('express', actual)).build() + t.equal(methodDescriptor.apiDescriptor, 'express.Function.app.get(path, callback)', 'apiDescriptor') + t.equal(methodDescriptor.className, 'Function', 'className') + t.equal(methodDescriptor.fullName, 'express.app.get(path, callback)', 'fileName') + t.equal(methodDescriptor.lineNumber, 481, 'lineNumber') + t.true(methodDescriptor.location.endsWith('express/lib/application.js'), 'location') + t.equal(methodDescriptor.methodName, 'get', 'methodName') + t.equal(methodDescriptor.objectPath, 'app.get', 'objectPath') + t.end() }) @@ -82,6 +102,7 @@ at Test.bound [as run] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_ at Immediate.next [as _onImmediate] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/tape/lib/results.js:83:19) at processImmediate (internal/timers.js:464:21) at process.callbackTrampoline (internal/async_hooks.js:130:17)` + test('koa makeMethodDescriptorBuilder', (t) => { const stacks = actualKoaCallStack.split(/\r?\n/) let actual = captureNamedGroup(stacks[2]) //at Router. [as get] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/koa-router/lib/router.js:202:10) @@ -91,5 +112,15 @@ test('koa makeMethodDescriptorBuilder', (t) => { t.equal(actual.lineNumber, '202', 'lineNumber') t.true(actual.location.endsWith('koa-router/lib/'), 'location') t.equal(actual.methodName, 'get', 'methodName') + + const methodDescriptor = KoaMethodDescriptorBuilder.make(MethodDescriptorBuilder.make('koa', actual)).build() + t.equal(methodDescriptor.apiDescriptor, 'koa.Router.get(ctx, next)', 'apiDescriptor') + t.equal(methodDescriptor.className, 'Router', 'className') + t.equal(methodDescriptor.fullName, 'koa.get(ctx, next)', 'fullName') + t.equal(methodDescriptor.lineNumber, 202, 'lineNumber') + t.true(methodDescriptor.location.endsWith('koa-router/lib/router.js'), 'location') + t.equal(methodDescriptor.methodName, 'get', 'methodName') + t.equal(methodDescriptor.objectPath, 'get', 'objectPath') + t.end() }) \ No newline at end of file From de6b611e16b031091d61b32599dd55861ee34179 Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Thu, 10 Feb 2022 22:52:56 +0900 Subject: [PATCH 11/14] [#104] koa module MethodDescriptorBuilder --- test/instrumentation/module/koa.test.js | 14 +++++++++++++- test/support/agent-singleton-mock.js | 10 ++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/test/instrumentation/module/koa.test.js b/test/instrumentation/module/koa.test.js index 5cc0e08e..d191b348 100644 --- a/test/instrumentation/module/koa.test.js +++ b/test/instrumentation/module/koa.test.js @@ -12,6 +12,9 @@ const agent = require('../../support/agent-singleton-mock') const Koa = require('koa') const Router = require('koa-router') +const apiMetaService = require('../../../lib/context/api-meta-service') +const MethodDescriptorBuilder = require('../../../lib/context/method-descriptor-builder') + const TEST_ENV = { host: 'localhost', port: 5006, @@ -20,6 +23,7 @@ const getServerUrl = (path) => `http://${TEST_ENV.host}:${TEST_ENV.port}${path}` const testName1 = 'koa-router1' test(`${testName1} Should record request in basic route koa.test.js`, function (t) { + agent.bindHttp() const testName = testName1 t.plan(3) @@ -30,7 +34,15 @@ test(`${testName1} Should record request in basic route koa.test.js`, function ( router.get(PATH, async (ctx, next) => { ctx.body = 'ok. get' - const trace = agent.traceContext.currentTraceObject() + + agent.callbackTraceClose((trace) => { + let actualBuilder = new MethodDescriptorBuilder('koa', 'get') + .setParameterDescriptor('(ctx, next)') + .setLineNumber(35) + .setFileName('koa.test.js') + const actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) + let spanEvent = trace.storage.storage[0] + }) }) router.post(PATH, async (ctx, next) => { ctx.body = 'ok. post' diff --git a/test/support/agent-singleton-mock.js b/test/support/agent-singleton-mock.js index 6a851904..8ac613da 100644 --- a/test/support/agent-singleton-mock.js +++ b/test/support/agent-singleton-mock.js @@ -77,9 +77,15 @@ class MockAgent extends Agent { shimmer.unwrap(http, 'request') } - resetAgent(callback) { - this.pinpointClient = new MockPinpointClient(this.config, this.agentInfo, this.dataSender) + callbackTraceClose(callback) { + const trace = this.traceContext.currentTraceObject() + const origin = trace.close + trace.close = () => { + callback(trace) + origin.apply(trace, arguments) + } } + } const agent = new MockAgent(fixture.config) From e6a3c5a12998a0ee68eb69b6825b6355e9c67c3c Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Fri, 11 Feb 2022 15:02:19 +0900 Subject: [PATCH 12/14] [#104] koa support code level visibility --- lib/instrumentation/module/koa-router.js | 15 +++++++++++++-- test/instrumentation/module/koa.test.js | 22 +++++++++++++++++----- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/instrumentation/module/koa-router.js b/lib/instrumentation/module/koa-router.js index f9b98e96..c57da0c7 100644 --- a/lib/instrumentation/module/koa-router.js +++ b/lib/instrumentation/module/koa-router.js @@ -52,14 +52,14 @@ module.exports = function (agent, version, router) { if (trace) { spanEventRecorder = trace.traceBlockBegin() spanEventRecorder.recordServiceType(ServiceTypeCode.koa) - spanEventRecorder.recordApiDesc(`koa.router.${ctx.method.toLocaleLowerCase()} [${name}]`) + recordAPI(methodDescriptor, path, spanEventRecorder, ctx, name) } result = await fn.apply(this, arguments) } catch (e) { if (!e._pinpointCheck) { e._pinpointCheck = true spanEventRecorder.recordServiceType(ServiceTypeCode.koa) - spanEventRecorder.recordApiDesc(`koa.router.${ctx.method.toLocaleLowerCase()} [${name}]`) + recordAPI(methodDescriptor, path, spanEventRecorder, ctx, name) spanEventRecorder.recordException(e, true) } throw e @@ -87,3 +87,14 @@ module.exports = function (agent, version, router) { return router } + +function recordAPI(methodDescriptor, path, spanEventRecorder, ctx, name) { + if (methodDescriptor && typeof path === 'string') { + spanEventRecorder.recordApiWithParameters(methodDescriptor, [path]) + } else if (methodDescriptor) { + spanEventRecorder.recordApi(methodDescriptor) + } else { + spanEventRecorder.recordApiDesc(`koa.router.${ctx.method.toLocaleLowerCase()} [${name}]`) + } +} + diff --git a/test/instrumentation/module/koa.test.js b/test/instrumentation/module/koa.test.js index d191b348..ba6b98ee 100644 --- a/test/instrumentation/module/koa.test.js +++ b/test/instrumentation/module/koa.test.js @@ -11,7 +11,7 @@ const { log } = require('../../test-helper') const agent = require('../../support/agent-singleton-mock') const Koa = require('koa') const Router = require('koa-router') - +const DefaultAnnotationKey = require('../../../lib/constant/annotation-key').DefaultAnnotationKey const apiMetaService = require('../../../lib/context/api-meta-service') const MethodDescriptorBuilder = require('../../../lib/context/method-descriptor-builder') @@ -25,9 +25,6 @@ const testName1 = 'koa-router1' test(`${testName1} Should record request in basic route koa.test.js`, function (t) { agent.bindHttp() const testName = testName1 - - t.plan(3) - const PATH = `/${testName}` const app = new Koa() const router = new Router() @@ -36,12 +33,26 @@ test(`${testName1} Should record request in basic route koa.test.js`, function ( ctx.body = 'ok. get' agent.callbackTraceClose((trace) => { + t.equal(trace.span.annotations[0].key, DefaultAnnotationKey.HTTP_STATUS_CODE.name, 'HTTP param key match') + t.equal(trace.span.annotations[0].value.intValue, 200, 'response status is 200') + let actualBuilder = new MethodDescriptorBuilder('koa', 'get') .setParameterDescriptor('(ctx, next)') - .setLineNumber(35) + .setLineNumber(32) .setFileName('koa.test.js') const actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) let spanEvent = trace.storage.storage[0] + t.equal(actualMethodDescriptor.apiId, spanEvent.apiId, 'apiId') + t.equal(spanEvent.annotations[0].key, -1, 'parameter') + t.equal(spanEvent.annotations[0].value.stringValue, '/koa-router1', 'parameter value matching') + t.true(actualMethodDescriptor.apiDescriptor.startsWith('koa.Router.get(ctx, next)'), 'apiDescriptor') + t.equal(actualMethodDescriptor.className, 'Router', 'className') + t.equal(actualMethodDescriptor.fullName, 'koa.get(ctx, next)', 'fullName') + t.equal(actualMethodDescriptor.lineNumber, 32, 'lineNumber') + t.equal(actualMethodDescriptor.methodName, 'get', 'methodName') + t.equal(actualMethodDescriptor.moduleName, 'koa', 'moduleName') + t.equal(actualMethodDescriptor.objectPath, 'get', 'objectPath') + t.true(actualMethodDescriptor.location.length > 0, 'location') }) }) router.post(PATH, async (ctx, next) => { @@ -61,6 +72,7 @@ test(`${testName1} Should record request in basic route koa.test.js`, function ( log.debug(traceMap.size) t.ok(traceMap.size > 0) + t.end() server.close() }) }) From bc2c4457b62adbc312094ac700e4c919801481a1 Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Mon, 14 Feb 2022 16:40:17 +0900 Subject: [PATCH 13/14] [#104] koa and express module status code 200 verification --- test/instrumentation/module/express.test.js | 48 +++++++++++---------- test/instrumentation/module/koa.test.js | 2 +- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/test/instrumentation/module/express.test.js b/test/instrumentation/module/express.test.js index b932d92d..c5745794 100644 --- a/test/instrumentation/module/express.test.js +++ b/test/instrumentation/module/express.test.js @@ -31,12 +31,13 @@ test(`${testName1} Should record request in basic route`, function (t) { const app = new express() app.get(PATH, async (req, res) => { - process.nextTick(() => { - res.send('ok get') + res.send('ok get') - const trace = agent.traceContext.currentTraceObject() + agent.callbackTraceClose((trace) => { t.equal(trace.span.annotations[0].key, DefaultAnnotationKey.HTTP_PARAM.name, 'HTTP param key match') t.equal(trace.span.annotations[0].value.stringValue, 'api=test&test1=test', 'HTTP param value match') + t.equal(trace.span.annotations[1].key, DefaultAnnotationKey.HTTP_STATUS_CODE.name, 'HTTP status code') + t.equal(trace.span.annotations[1].value.intValue, 200, 'response status is 200') let actualBuilder = new MethodDescriptorBuilder('express', 'app.get') .setParameterDescriptor('(path, callback)') @@ -58,6 +59,7 @@ test(`${testName1} Should record request in basic route`, function (t) { }) }) + app.post(PATH, (req, res) => { process.nextTick(() => { res.send('ok post') @@ -67,7 +69,7 @@ test(`${testName1} Should record request in basic route`, function (t) { let actualBuilder = new MethodDescriptorBuilder('express', 'app.post') .setParameterDescriptor('(path, callback)') - .setLineNumber(61) + .setLineNumber(63) .setFileName('express.test.js') const actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) let spanEvent = trace.storage.storage[0] @@ -77,7 +79,7 @@ test(`${testName1} Should record request in basic route`, function (t) { t.true(actualMethodDescriptor.apiDescriptor.startsWith('express.Function.app.post(path, callback)'), 'apiDescriptor') t.equal(actualMethodDescriptor.className, 'Function', 'className') t.equal(actualMethodDescriptor.fullName, 'express.app.post(path, callback)', 'fullName') - t.equal(actualMethodDescriptor.lineNumber, 61, 'lineNumber') + t.equal(actualMethodDescriptor.lineNumber, 63, 'lineNumber') t.equal(actualMethodDescriptor.methodName, 'post', 'methodName') t.equal(actualMethodDescriptor.moduleName, 'express', 'moduleName') t.equal(actualMethodDescriptor.objectPath, 'app.post', 'objectPath') @@ -92,7 +94,7 @@ test(`${testName1} Should record request in basic route`, function (t) { const trace = agent.traceContext.currentTraceObject() let actualBuilder = new MethodDescriptorBuilder('express', 'app.get') .setParameterDescriptor('(path, callback)') - .setLineNumber(88) + .setLineNumber(90) .setFileName('express.test.js') const actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) let spanEvent = trace.storage.storage[0] @@ -102,7 +104,7 @@ test(`${testName1} Should record request in basic route`, function (t) { t.true(actualMethodDescriptor.apiDescriptor.startsWith('express.Function.app.get(path, callback)'), 'apiDescriptor') t.equal(actualMethodDescriptor.className, 'Function', 'className') t.equal(actualMethodDescriptor.fullName, 'express.app.get(path, callback)', 'fullName') - t.equal(actualMethodDescriptor.lineNumber, 88, 'lineNumber') + t.equal(actualMethodDescriptor.lineNumber, 90, 'lineNumber') t.equal(actualMethodDescriptor.methodName, 'get', 'methodName') t.equal(actualMethodDescriptor.moduleName, 'express', 'moduleName') t.equal(actualMethodDescriptor.objectPath, 'app.get', 'objectPath') @@ -180,7 +182,7 @@ test(`${testName1} Should record request in basic route`, function (t) { function throwHandleTest(trace, t) { let actualBuilder = new MethodDescriptorBuilder('express', 'app.get') .setParameterDescriptor('(path, callback)') - .setLineNumber(117) + .setLineNumber(119) .setFileName('express.test.js') const actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) let spanEvent = trace.storage.storage[1] @@ -190,7 +192,7 @@ function throwHandleTest(trace, t) { t.true(actualMethodDescriptor.apiDescriptor.startsWith('express.Function.app.get(path, callback)'), 'apiDescriptor') t.equal(actualMethodDescriptor.className, 'Function', 'className') t.equal(actualMethodDescriptor.fullName, 'express.app.get(path, callback)', 'fullName') - t.equal(actualMethodDescriptor.lineNumber, 117, 'lineNumber') + t.equal(actualMethodDescriptor.lineNumber, 119, 'lineNumber') t.equal(actualMethodDescriptor.methodName, 'get', 'methodName') t.equal(actualMethodDescriptor.moduleName, 'express', 'moduleName') t.equal(actualMethodDescriptor.objectPath, 'app.get', 'objectPath') @@ -200,7 +202,7 @@ function throwHandleTest(trace, t) { actualBuilder = new MethodDescriptorBuilder('express', 'use') .setParameterDescriptor('(err, req, res, next)') - .setLineNumber(130) + .setLineNumber(132) .setFileName('express.test.js') const actualErrorMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) spanEvent = trace.storage.storage[0] @@ -208,7 +210,7 @@ function throwHandleTest(trace, t) { t.true(actualErrorMethodDescriptor.apiDescriptor.startsWith('express.Function.use(err, req, res, next)'), 'apiDescriptor') t.equal(actualErrorMethodDescriptor.className, 'Function', 'className') t.equal(actualErrorMethodDescriptor.fullName, 'express.use(err, req, res, next)', 'fullName') - t.equal(actualErrorMethodDescriptor.lineNumber, 130, 'lineNumber') + t.equal(actualErrorMethodDescriptor.lineNumber, 132, 'lineNumber') t.equal(actualErrorMethodDescriptor.methodName, 'use', 'methodName') t.equal(actualErrorMethodDescriptor.moduleName, 'express', 'moduleName') t.equal(actualErrorMethodDescriptor.objectPath, 'use', 'objectPath') @@ -216,13 +218,13 @@ function throwHandleTest(trace, t) { t.equal(spanEvent.sequence, 1, 'sequence') t.equal(spanEvent.depth, 2, 'spanEvent.depth') t.equal(spanEvent.exceptionInfo.intValue, 1, 'error value') - t.true(spanEvent.exceptionInfo.stringValue.endsWith('express.test.js:120:11'), 'error case') + t.true(spanEvent.exceptionInfo.stringValue.endsWith('express.test.js:122:11'), 'error case') } function nextErrorHandleTest(trace, t) { let actualBuilder = new MethodDescriptorBuilder('express', 'app.get') .setParameterDescriptor('(path, callback)') - .setLineNumber(124) + .setLineNumber(126) .setFileName('express.test.js') const actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) let spanEvent = trace.storage.storage[1] @@ -232,7 +234,7 @@ function nextErrorHandleTest(trace, t) { t.true(actualMethodDescriptor.apiDescriptor.startsWith('express.Function.app.get(path, callback)'), 'apiDescriptor') t.equal(actualMethodDescriptor.className, 'Function', 'className') t.equal(actualMethodDescriptor.fullName, 'express.app.get(path, callback)', 'fullName') - t.equal(actualMethodDescriptor.lineNumber, 124, 'lineNumber') + t.equal(actualMethodDescriptor.lineNumber, 126, 'lineNumber') t.equal(actualMethodDescriptor.methodName, 'get', 'methodName') t.equal(actualMethodDescriptor.moduleName, 'express', 'moduleName') t.equal(actualMethodDescriptor.objectPath, 'app.get', 'objectPath') @@ -242,7 +244,7 @@ function nextErrorHandleTest(trace, t) { actualBuilder = new MethodDescriptorBuilder('express', 'use') .setParameterDescriptor('(err, req, res, next)') - .setLineNumber(130) + .setLineNumber(132) .setFileName('express.test.js') const actualErrorMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) spanEvent = trace.storage.storage[0] @@ -250,7 +252,7 @@ function nextErrorHandleTest(trace, t) { t.true(actualErrorMethodDescriptor.apiDescriptor.startsWith('express.Function.use(err, req, res, next)'), 'apiDescriptor') t.equal(actualErrorMethodDescriptor.className, 'Function', 'className') t.equal(actualErrorMethodDescriptor.fullName, 'express.use(err, req, res, next)', 'fullName') - t.equal(actualErrorMethodDescriptor.lineNumber, 130, 'lineNumber') + t.equal(actualErrorMethodDescriptor.lineNumber, 132, 'lineNumber') t.equal(actualErrorMethodDescriptor.methodName, 'use', 'methodName') t.equal(actualErrorMethodDescriptor.moduleName, 'express', 'moduleName') t.equal(actualErrorMethodDescriptor.objectPath, 'use', 'objectPath') @@ -258,7 +260,7 @@ function nextErrorHandleTest(trace, t) { t.equal(spanEvent.sequence, 1, 'sequence') t.equal(spanEvent.depth, 2, 'spanEvent.depth') t.equal(spanEvent.exceptionInfo.intValue, 1, 'error value') - t.true(spanEvent.exceptionInfo.stringValue.endsWith('express.test.js:127:10'), 'error case') + t.true(spanEvent.exceptionInfo.stringValue.endsWith('express.test.js:129:10'), 'error case') } const testName2 = 'express2' @@ -392,7 +394,7 @@ test(`${testName5} Should record middleware`, function (t) { const trace = agent.traceContext.currentTraceObject() let actualBuilder = new MethodDescriptorBuilder('express', 'app.get') .setParameterDescriptor('(path, callback)') - .setLineNumber(455) + .setLineNumber(457) .setFileName('express.test.js') const actualMethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) let spanEvent = trace.storage.storage[2] @@ -402,7 +404,7 @@ test(`${testName5} Should record middleware`, function (t) { t.true(actualMethodDescriptor.apiDescriptor.startsWith('express.Function.app.get(path, callback)'), 'apiDescriptor') t.equal(actualMethodDescriptor.className, 'Function', 'className') t.equal(actualMethodDescriptor.fullName, 'express.app.get(path, callback)', 'fullName') - t.equal(actualMethodDescriptor.lineNumber, 455, 'lineNumber') + t.equal(actualMethodDescriptor.lineNumber, 457, 'lineNumber') t.equal(actualMethodDescriptor.methodName, 'get', 'methodName') t.equal(actualMethodDescriptor.moduleName, 'express', 'moduleName') t.equal(actualMethodDescriptor.objectPath, 'app.get', 'objectPath') @@ -412,7 +414,7 @@ test(`${testName5} Should record middleware`, function (t) { actualBuilder = new MethodDescriptorBuilder('express', 'use') .setParameterDescriptor('(req, res, next)') - .setLineNumber(388) + .setLineNumber(390) .setFileName('express.test.js') const actualMiddleware1MethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) spanEvent = trace.storage.storage[1] @@ -420,7 +422,7 @@ test(`${testName5} Should record middleware`, function (t) { t.true(actualMiddleware1MethodDescriptor.apiDescriptor.startsWith('express.Function.use(req, res, next)'), 'apiDescriptor') t.equal(actualMiddleware1MethodDescriptor.className, 'Function', 'className') t.equal(actualMiddleware1MethodDescriptor.fullName, 'express.use(req, res, next)', 'fullName') - t.equal(actualMiddleware1MethodDescriptor.lineNumber, 388, 'lineNumber') + t.equal(actualMiddleware1MethodDescriptor.lineNumber, 390, 'lineNumber') t.equal(actualMiddleware1MethodDescriptor.methodName, 'use', 'methodName') t.equal(actualMiddleware1MethodDescriptor.moduleName, 'express', 'moduleName') t.equal(actualMiddleware1MethodDescriptor.objectPath, 'use', 'objectPath') @@ -430,7 +432,7 @@ test(`${testName5} Should record middleware`, function (t) { actualBuilder = new MethodDescriptorBuilder('express', 'use') .setParameterDescriptor('(req, res, next)') - .setLineNumber(383) + .setLineNumber(385) .setFileName('express.test.js') const actualMiddleware2MethodDescriptor = apiMetaService.cacheApiWithBuilder(actualBuilder) spanEvent = trace.storage.storage[0] @@ -438,7 +440,7 @@ test(`${testName5} Should record middleware`, function (t) { t.true(actualMiddleware2MethodDescriptor.apiDescriptor.startsWith('express.Function.use(req, res, next)'), 'apiDescriptor') t.equal(actualMiddleware2MethodDescriptor.className, 'Function', 'className') t.equal(actualMiddleware2MethodDescriptor.fullName, 'express.use(req, res, next)', 'fullName') - t.equal(actualMiddleware2MethodDescriptor.lineNumber, 383, 'lineNumber') + t.equal(actualMiddleware2MethodDescriptor.lineNumber, 385, 'lineNumber') t.equal(actualMiddleware2MethodDescriptor.methodName, 'use', 'methodName') t.equal(actualMiddleware2MethodDescriptor.moduleName, 'express', 'moduleName') t.equal(actualMiddleware2MethodDescriptor.objectPath, 'use', 'objectPath') diff --git a/test/instrumentation/module/koa.test.js b/test/instrumentation/module/koa.test.js index ba6b98ee..1978027e 100644 --- a/test/instrumentation/module/koa.test.js +++ b/test/instrumentation/module/koa.test.js @@ -33,7 +33,7 @@ test(`${testName1} Should record request in basic route koa.test.js`, function ( ctx.body = 'ok. get' agent.callbackTraceClose((trace) => { - t.equal(trace.span.annotations[0].key, DefaultAnnotationKey.HTTP_STATUS_CODE.name, 'HTTP param key match') + t.equal(trace.span.annotations[0].key, DefaultAnnotationKey.HTTP_STATUS_CODE.name, 'HTTP status code') t.equal(trace.span.annotations[0].value.intValue, 200, 'response status is 200') let actualBuilder = new MethodDescriptorBuilder('koa', 'get') From 47ddf8e9b18e7fc6a702f9ed7cf88b55880fa63b Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Tue, 15 Feb 2022 23:17:12 +0900 Subject: [PATCH 14/14] [#104] remove no needs match wrap --- lib/instrumentation/module/koa-router.js | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/lib/instrumentation/module/koa-router.js b/lib/instrumentation/module/koa-router.js index c57da0c7..b84c28c8 100644 --- a/lib/instrumentation/module/koa-router.js +++ b/lib/instrumentation/module/koa-router.js @@ -74,17 +74,6 @@ module.exports = function (agent, version, router) { } }) - shimmer.wrap(router.prototype, 'match', function (original) { - return function (_, method) { - var matched = original.apply(this, arguments) - if (typeof method !== 'string') { - log.debug('unexpected method type in koa-router prototype.match: %s', typeof method) - return matched - } - return matched - } - }) - return router }