Skip to content

Commit

Permalink
fix(instrumentation-koa): handle koa routes being of type RegExp (#1754)
Browse files Browse the repository at this point in the history
* chore: replace deprecated SpanAttributes type with Attributes

* fix: handle layer path being a string or regex

* chore: add test for RegExp koa route

* fix: single quote string due to linting

---------

Co-authored-by: Amir Blum <[email protected]>
  • Loading branch information
ramesius and blumamir authored Nov 12, 2023
1 parent 24ffb3b commit e313938
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"@opentelemetry/instrumentation": "^0.45.1",
"@opentelemetry/semantic-conventions": "^1.0.0",
"@types/koa": "2.13.9",
"@types/koa__router": "12.0.1"
"@types/koa__router": "12.0.3"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-koa#readme"
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class KoaInstrumentation extends InstrumentationBase<typeof koa> {
private _patchLayer(
middlewareLayer: KoaPatchedMiddleware,
isRouter: boolean,
layerPath?: string
layerPath?: string | RegExp
): KoaMiddleware {
const layerType = isRouter ? KoaLayerType.ROUTER : KoaLayerType.MIDDLEWARE;
// Skip patching layer if its ignored in the config
Expand Down
10 changes: 5 additions & 5 deletions plugins/node/opentelemetry-instrumentation-koa/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@
import { KoaContext, KoaLayerType, KoaInstrumentationConfig } from './types';
import { KoaMiddleware } from './internal-types';
import { AttributeNames } from './enums/AttributeNames';
import { SpanAttributes } from '@opentelemetry/api';
import { Attributes } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

export const getMiddlewareMetadata = (
context: KoaContext,
layer: KoaMiddleware,
isRouter: boolean,
layerPath?: string
layerPath?: string | RegExp
): {
attributes: SpanAttributes;
attributes: Attributes;
name: string;
} => {
if (isRouter) {
return {
attributes: {
[AttributeNames.KOA_NAME]: layerPath,
[AttributeNames.KOA_NAME]: layerPath?.toString(),
[AttributeNames.KOA_TYPE]: KoaLayerType.ROUTER,
[SemanticAttributes.HTTP_ROUTE]: layerPath,
[SemanticAttributes.HTTP_ROUTE]: layerPath?.toString(),
},
name: context._matchedRouteName || `router - ${layerPath}`,
};
Expand Down
57 changes: 56 additions & 1 deletion plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('Koa Instrumentation', () => {
};

describe('Instrumenting @koa/router calls', () => {
it('should create a child span for middlewares', async () => {
it('should create a child span for middlewares (string route)', async () => {
const rootSpan = tracer.startSpan('rootSpan');
const rpcMetadata: RPCMetadata = { type: RPCType.HTTP, span: rootSpan };
app.use((ctx, next) =>
Expand Down Expand Up @@ -168,6 +168,10 @@ describe('Koa Instrumentation', () => {
requestHandlerSpan?.attributes[AttributeNames.KOA_TYPE],
KoaLayerType.ROUTER
);
assert.strictEqual(
requestHandlerSpan?.attributes[AttributeNames.KOA_NAME],
'/post/:id'
);

assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
Expand All @@ -179,6 +183,57 @@ describe('Koa Instrumentation', () => {
);
});

it('should create a child span for middlewares (RegExp route)', async () => {
const rootSpan = tracer.startSpan('rootSpan');
const rpcMetadata: RPCMetadata = { type: RPCType.HTTP, span: rootSpan };
app.use((ctx, next) =>
context.with(
setRPCMetadata(
trace.setSpan(context.active(), rootSpan),
rpcMetadata
),
next
)
);

const router = new KoaRouter();
router.get(/^\/post/, ctx => {
ctx.body = 'Post';
});

app.use(router.routes());

await context.with(
trace.setSpan(context.active(), rootSpan),
async () => {
await httpRequest.get(`http://localhost:${port}/post/0`);
rootSpan.end();

assert.deepStrictEqual(memoryExporter.getFinishedSpans().length, 2);
const requestHandlerSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name.includes('router - /^\\/post/'));
assert.notStrictEqual(requestHandlerSpan, undefined);

assert.strictEqual(
requestHandlerSpan?.attributes[AttributeNames.KOA_TYPE],
KoaLayerType.ROUTER
);
assert.strictEqual(
requestHandlerSpan?.attributes[AttributeNames.KOA_NAME],
'/^\\/post/'
);

assert.strictEqual(
requestHandlerSpan?.attributes[SemanticAttributes.HTTP_ROUTE],
'/^\\/post/'
);

assert.strictEqual(rpcMetadata.route, '/^\\/post/');
}
);
});

it('should create a named child span for middlewares', async () => {
const rootSpan = tracer.startSpan('rootSpan');
const rpcMetadata: RPCMetadata = { type: RPCType.HTTP, span: rootSpan };
Expand Down

0 comments on commit e313938

Please sign in to comment.