From 5f5425bce60d911bfa1692ab0e54bf712e65fdac Mon Sep 17 00:00:00 2001 From: Dina Yakovlev Date: Thu, 28 Jan 2021 16:14:01 +0200 Subject: [PATCH 1/5] feat: ebable root span to contain route --- .../src/koa.ts | 26 ++++++++++++++++++- .../src/types.ts | 8 ++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts b/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts index 5b01a063a1..636e070914 100644 --- a/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts +++ b/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts @@ -28,6 +28,9 @@ import { KoaContext, KoaComponentName, kLayerPatched, + KoaLayerType, + AttributeNames, + KoaPluginSpan, } from './types'; import { VERSION } from './version'; import { getMiddlewareMetadata } from './utils'; @@ -127,7 +130,8 @@ export class KoaInstrumentation extends InstrumentationBase { middlewareLayer[kLayerPatched] = true; api.diag.debug('patching Koa middleware layer'); return async (context: KoaContext, next: koa.Next) => { - if (api.getSpan(api.context.active()) === undefined) { + const parent = api.getSpan(api.context.active()) as KoaPluginSpan; + if (parent === undefined) { return middlewareLayer(context, next); } const metadata = getMiddlewareMetadata( @@ -140,6 +144,26 @@ export class KoaInstrumentation extends InstrumentationBase { attributes: metadata.attributes, }); + if (!parent?.parentSpanId) { + context.request.ctx.parentSpan = parent; + } + + if ( + metadata.attributes[AttributeNames.KOA_TYPE] === KoaLayerType.ROUTER + ) { + if (context.request.ctx.parentSpan.name) { + const parentRoute = context.request.ctx.parentSpan.name.split(' ')[1]; + if ( + context._matchedRoute && + !context._matchedRoute.toString().includes(parentRoute) + ) { + context.request.ctx.parentSpan.updateName( + `${context.method} ${context._matchedRoute}` + ); + } + } + } + return api.context.with( api.setSpan(api.context.active(), span), async () => { diff --git a/plugins/node/opentelemetry-instrumentation-koa/src/types.ts b/plugins/node/opentelemetry-instrumentation-koa/src/types.ts index 4e3db9307c..9e9aaad17b 100644 --- a/plugins/node/opentelemetry-instrumentation-koa/src/types.ts +++ b/plugins/node/opentelemetry-instrumentation-koa/src/types.ts @@ -16,6 +16,7 @@ import type { Middleware, ParameterizedContext, DefaultState } from 'koa'; import type { RouterParamContext } from '@koa/router'; import type * as Router from '@koa/router'; +import type { Span } from '@opentelemetry/api'; /** * This symbol is used to mark a Koa layer as being already instrumented @@ -41,3 +42,10 @@ export enum KoaLayerType { } export const KoaComponentName = 'koa'; + +/** + * extends opentelemetry/api Span object to instrument the root span name of http plugin by getting parent span id + */ +export interface KoaPluginSpan extends Span { + parentSpanId?: string; +} From ea4ab560d793cd39d6e1b8168680418faeb5f6e0 Mon Sep 17 00:00:00 2001 From: Dina Yakovlev Date: Mon, 1 Feb 2021 13:04:41 +0200 Subject: [PATCH 2/5] fix: delete root span from ctx --- plugins/node/opentelemetry-instrumentation-koa/src/koa.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts b/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts index 636e070914..9ea83324de 100644 --- a/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts +++ b/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts @@ -160,6 +160,8 @@ export class KoaInstrumentation extends InstrumentationBase { context.request.ctx.parentSpan.updateName( `${context.method} ${context._matchedRoute}` ); + + delete context.request.ctx.parentSpan; } } } From 1df39b7325f5258b0597ef4666def5534622a7cd Mon Sep 17 00:00:00 2001 From: Dina Yakovlev Date: Sun, 28 Feb 2021 11:50:11 +0200 Subject: [PATCH 3/5] fix: tests --- .../node/opentelemetry-instrumentation-koa/test/koa.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts b/plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts index ba7db72aff..24633ff0df 100644 --- a/plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts +++ b/plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts @@ -158,7 +158,7 @@ describe('Koa Instrumentation', () => { const exportedRootSpan = memoryExporter .getFinishedSpans() - .find(span => span.name === 'rootSpan'); + .find(span => span.name === 'GET /post/:id'); assert.notStrictEqual(exportedRootSpan, undefined); }); }); @@ -200,7 +200,7 @@ describe('Koa Instrumentation', () => { const exportedRootSpan = memoryExporter .getFinishedSpans() - .find(span => span.name === 'rootSpan'); + .find(span => span.name === 'GET /:first/post/:id'); assert.notStrictEqual(exportedRootSpan, undefined); }); }); @@ -240,7 +240,7 @@ describe('Koa Instrumentation', () => { const exportedRootSpan = memoryExporter .getFinishedSpans() - .find(span => span.name === 'rootSpan'); + .find(span => span.name === 'GET /:first/post/:id'); assert.notStrictEqual(exportedRootSpan, undefined); }); }); From 57bfdbf2f9daccf4c42093f506dd19d21e782b57 Mon Sep 17 00:00:00 2001 From: Dina Yakovlev Date: Wed, 3 Mar 2021 10:09:15 +0200 Subject: [PATCH 4/5] fix: chcek root span from context --- plugins/node/opentelemetry-instrumentation-koa/src/koa.ts | 7 +++---- .../node/opentelemetry-instrumentation-koa/src/types.ts | 8 -------- 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts b/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts index 9ea83324de..5426a519cd 100644 --- a/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts +++ b/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts @@ -22,7 +22,7 @@ import { InstrumentationNodeModuleDefinition, } from '@opentelemetry/instrumentation'; -import type * as koa from 'koa'; +import * as koa from 'koa'; import { KoaMiddleware, KoaContext, @@ -30,7 +30,6 @@ import { kLayerPatched, KoaLayerType, AttributeNames, - KoaPluginSpan, } from './types'; import { VERSION } from './version'; import { getMiddlewareMetadata } from './utils'; @@ -130,7 +129,7 @@ export class KoaInstrumentation extends InstrumentationBase { middlewareLayer[kLayerPatched] = true; api.diag.debug('patching Koa middleware layer'); return async (context: KoaContext, next: koa.Next) => { - const parent = api.getSpan(api.context.active()) as KoaPluginSpan; + const parent = api.getSpan(api.context.active()); if (parent === undefined) { return middlewareLayer(context, next); } @@ -144,7 +143,7 @@ export class KoaInstrumentation extends InstrumentationBase { attributes: metadata.attributes, }); - if (!parent?.parentSpanId) { + if (!context.request.ctx.parentSpan) { context.request.ctx.parentSpan = parent; } diff --git a/plugins/node/opentelemetry-instrumentation-koa/src/types.ts b/plugins/node/opentelemetry-instrumentation-koa/src/types.ts index 9e9aaad17b..4e3db9307c 100644 --- a/plugins/node/opentelemetry-instrumentation-koa/src/types.ts +++ b/plugins/node/opentelemetry-instrumentation-koa/src/types.ts @@ -16,7 +16,6 @@ import type { Middleware, ParameterizedContext, DefaultState } from 'koa'; import type { RouterParamContext } from '@koa/router'; import type * as Router from '@koa/router'; -import type { Span } from '@opentelemetry/api'; /** * This symbol is used to mark a Koa layer as being already instrumented @@ -42,10 +41,3 @@ export enum KoaLayerType { } export const KoaComponentName = 'koa'; - -/** - * extends opentelemetry/api Span object to instrument the root span name of http plugin by getting parent span id - */ -export interface KoaPluginSpan extends Span { - parentSpanId?: string; -} From a02566c5956030cd03a9e041fead7b1c078603d8 Mon Sep 17 00:00:00 2001 From: Dina Yakovlev Date: Wed, 3 Mar 2021 10:52:45 +0200 Subject: [PATCH 5/5] refactor: rebase --- plugins/node/opentelemetry-instrumentation-koa/src/koa.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts b/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts index 5426a519cd..223223aece 100644 --- a/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts +++ b/plugins/node/opentelemetry-instrumentation-koa/src/koa.ts @@ -22,7 +22,7 @@ import { InstrumentationNodeModuleDefinition, } from '@opentelemetry/instrumentation'; -import * as koa from 'koa'; +import type * as koa from 'koa'; import { KoaMiddleware, KoaContext,