Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable root span to contain route #327

Merged
merged 7 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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