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

Koa plugin enhancements #326

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions plugins/node/opentelemetry-koa-instrumentation/src/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import {
KoaContext,
KoaComponentName,
kLayerPatched,
KoaLayerType,
AttributeNames,
KoaPluginSpan,
} from './types';
import { VERSION } from './version';
import { getMiddlewareMetadata } from './utils';
Expand All @@ -41,7 +44,7 @@ export class KoaInstrumentation extends BasePlugin<typeof koa> {
*/
protected patch(): typeof koa {
this._logger.debug('Patching Koa');
if (this._moduleExports == null) {
if (this._moduleExports === null) {
return this._moduleExports;
}
this._logger.debug('Patching Koa.use');
Expand Down Expand Up @@ -118,7 +121,8 @@ export class KoaInstrumentation extends BasePlugin<typeof koa> {
middlewareLayer[kLayerPatched] = true;
this._logger.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(
Expand All @@ -131,6 +135,26 @@ export class KoaInstrumentation extends BasePlugin<typeof koa> {
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 () => {
Expand Down
8 changes: 8 additions & 0 deletions plugins/node/opentelemetry-koa-instrumentation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('Koa Instrumentation - Router Tests', () => {

const exportedRootSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name === 'rootSpan');
.find(span => span.name === 'GET /post/:id');
assert.notStrictEqual(exportedRootSpan, undefined);
});
});
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('Koa Instrumentation - Router Tests', () => {

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 @@ -200,7 +200,7 @@ describe('Koa Instrumentation - Router Tests', () => {

const exportedRootSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name === 'rootSpan');
.find(span => span.name === 'GET /:first/post/:id');
assert.notStrictEqual(exportedRootSpan, undefined);
});
});
Expand Down