Skip to content

Commit

Permalink
[#104] Support call stack Router.<computed> [as get] with `Function…
Browse files Browse the repository at this point in the history
….app.<computed> [as get]`
  • Loading branch information
feelform committed Feb 4, 2022
1 parent e91162a commit a3ec75a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/context/method-descriptor-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ function makeFunctionName(namedGroup) {
if (computedGroups && computedGroups.length > 0 && namedGroup.methodName) {
return computedGroups.groups.functionName + namedGroup.methodName
}
if (functionName === '<computed>' && typeof namedGroup.type === 'string' && typeof namedGroup.methodName === 'string') {
return namedGroup.methodName
}
return namedGroup.functionName
}

Expand Down
31 changes: 31 additions & 0 deletions test/context/make-method-descriptor-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.<computed> [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.<computed>', '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()
})

Expand Down Expand Up @@ -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.<computed> [as get] (/Users/test/workspace/pinpoint/pinpoint-node-agent/node_modules/koa-router/lib/router.js:202:10)
Expand All @@ -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()
})

0 comments on commit a3ec75a

Please sign in to comment.