Skip to content

Commit

Permalink
feat: enable root span to contain route (#327)
Browse files Browse the repository at this point in the history
Co-authored-by: Bartlomiej Obecny <bobecny@gmail.com>
Co-authored-by: Valentin Marchaud <thisismac47@gmail.com>
  • Loading branch information
3 people authored Mar 20, 2021
1 parent 557d9a4 commit 384482c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
27 changes: 26 additions & 1 deletion plugins/node/opentelemetry-instrumentation-koa/src/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
KoaContext,
KoaComponentName,
kLayerPatched,
KoaLayerType,
AttributeNames,
} from './types';
import { VERSION } from './version';
import { getMiddlewareMetadata } from './utils';
Expand Down Expand Up @@ -127,7 +129,8 @@ export class KoaInstrumentation extends InstrumentationBase<typeof koa> {
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());
if (parent === undefined) {
return middlewareLayer(context, next);
}
const metadata = getMiddlewareMetadata(
Expand All @@ -140,6 +143,28 @@ export class KoaInstrumentation extends InstrumentationBase<typeof koa> {
attributes: metadata.attributes,
});

if (!context.request.ctx.parentSpan) {
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}`
);

delete context.request.ctx.parentSpan;
}
}
}

return api.context.with(
api.setSpan(api.context.active(), span),
async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand Down Expand Up @@ -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);
});
});
Expand Down Expand Up @@ -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);
});
});
Expand Down

0 comments on commit 384482c

Please sign in to comment.