Skip to content

Commit

Permalink
[#104] koa support code level visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Feb 11, 2022
1 parent de6b611 commit 200fe0d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 13 additions & 2 deletions lib/instrumentation/module/koa-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}]`)
}
}

7 changes: 3 additions & 4 deletions test/instrumentation/module/koa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -38,10 +35,11 @@ test(`${testName1} Should record request in basic route koa.test.js`, function (
agent.callbackTraceClose((trace) => {
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')
})
})
router.post(PATH, async (ctx, next) => {
Expand All @@ -61,6 +59,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()
})
})

0 comments on commit 200fe0d

Please sign in to comment.