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 14, 2022
1 parent de6b611 commit e6a3c5a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 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}]`)
}
}

22 changes: 17 additions & 5 deletions test/instrumentation/module/koa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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 @@ -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) => {
Expand All @@ -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()
})
})

0 comments on commit e6a3c5a

Please sign in to comment.