From e6a3c5a12998a0ee68eb69b6825b6355e9c67c3c Mon Sep 17 00:00:00 2001 From: Yongseok Kang Date: Fri, 11 Feb 2022 15:02:19 +0900 Subject: [PATCH] [#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() }) })