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(koa): Skip update HTTP's span name and update RpcMetadata's route instead #1567

Merged
merged 12 commits into from
Aug 13, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { AttributeNames } from './enums/AttributeNames';
import { VERSION } from './version';
import { getMiddlewareMetadata, isLayerIgnored } from './utils';
import { getRPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
import { kLayerPatched, KoaPatchedMiddleware } from './internal-types';

/** Koa instrumentation for OpenTelemetry */
Expand Down Expand Up @@ -174,21 +174,8 @@ export class KoaInstrumentation extends InstrumentationBase<typeof koa> {

const rpcMetadata = getRPCMetadata(api.context.active());

if (
metadata.attributes[AttributeNames.KOA_TYPE] === KoaLayerType.ROUTER &&
rpcMetadata?.type === RPCType.HTTP
) {
rpcMetadata.span.updateName(
`${context.method} ${context._matchedRoute}`
);
}

let newContext = api.trace.setSpan(api.context.active(), span);
if (rpcMetadata?.type === RPCType.HTTP) {
newContext = setRPCMetadata(
newContext,
Object.assign(rpcMetadata, { route: context._matchedRoute })
);
if (rpcMetadata?.type === RPCType.HTTP && context._matchedRoute) {
rpcMetadata.route = context._matchedRoute.toString();
}

if (this.getConfig().requestHook) {
Expand All @@ -208,6 +195,7 @@ export class KoaInstrumentation extends InstrumentationBase<typeof koa> {
);
}

const newContext = api.trace.setSpan(api.context.active(), span);
return api.context.with(newContext, async () => {
try {
return await middlewareLayer(context, next);
Expand Down
34 changes: 12 additions & 22 deletions plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import * as sinon from 'sinon';
import { AddressInfo } from 'net';
import { KoaLayerType, KoaRequestInfo } from '../src/types';
import { AttributeNames } from '../src/enums/AttributeNames';
import { RPCType, setRPCMetadata } from '@opentelemetry/core';
import { RPCMetadata, RPCType, setRPCMetadata } from '@opentelemetry/core';

const httpRequest = {
get: (options: http.ClientRequestArgs | string) => {
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('Koa Instrumentation', () => {
describe('Instrumenting @koa/router calls', () => {
it('should create a child span for middlewares', async () => {
const rootSpan = tracer.startSpan('rootSpan');
const rpcMetadata = { type: RPCType.HTTP, span: rootSpan };
const rpcMetadata: RPCMetadata = { type: RPCType.HTTP, span: rootSpan };
app.use((ctx, next) =>
context.with(
setRPCMetadata(
Expand Down Expand Up @@ -174,17 +174,14 @@ describe('Koa Instrumentation', () => {
'/post/:id'
);

const exportedRootSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name === 'GET /post/:id');
assert.notStrictEqual(exportedRootSpan, undefined);
assert.strictEqual(rpcMetadata.route, '/post/:id');
}
);
});

it('should create a named child span for middlewares', async () => {
const rootSpan = tracer.startSpan('rootSpan');
const rpcMetadata = { type: RPCType.HTTP, span: rootSpan };
const rpcMetadata: RPCMetadata = { type: RPCType.HTTP, span: rootSpan };
app.use((ctx, next) =>
context.with(
setRPCMetadata(
Expand Down Expand Up @@ -224,17 +221,14 @@ describe('Koa Instrumentation', () => {
'/post/:id'
);

const exportedRootSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name === 'GET /post/:id');
assert.notStrictEqual(exportedRootSpan, undefined);
assert.strictEqual(rpcMetadata.route, '/post/:id');
}
);
});

it('should correctly instrument nested routers', async () => {
const rootSpan = tracer.startSpan('rootSpan');
const rpcMetadata = { type: RPCType.HTTP, span: rootSpan };
const rpcMetadata: RPCMetadata = { type: RPCType.HTTP, span: rootSpan };
app.use((ctx, next) =>
context.with(
setRPCMetadata(
Expand Down Expand Up @@ -276,17 +270,14 @@ describe('Koa Instrumentation', () => {
'/:first/post/:id'
);

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

it('should correctly instrument prefixed routers', async () => {
const rootSpan = tracer.startSpan('rootSpan');
const rpcMetadata = { type: RPCType.HTTP, span: rootSpan };
const rpcMetadata: RPCMetadata = { type: RPCType.HTTP, span: rootSpan };
app.use((ctx, next) =>
context.with(
setRPCMetadata(
Expand Down Expand Up @@ -326,10 +317,7 @@ describe('Koa Instrumentation', () => {
'/:first/post/:id'
);

const exportedRootSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name === 'GET /:first/post/:id');
assert.notStrictEqual(exportedRootSpan, undefined);
assert.strictEqual(rpcMetadata.route, '/:first/post/:id');
}
);
});
Expand Down Expand Up @@ -364,7 +352,9 @@ describe('Koa Instrumentation', () => {
.find(span => span.name.includes('spanCreateMiddleware'));
assert.notStrictEqual(fooParentSpan, undefined);

const fooSpan = memoryExporter.getFinishedSpans().find(span => 'foo');
const fooSpan = memoryExporter
.getFinishedSpans()
.find(span => span.name === 'foo');
assert.notStrictEqual(fooSpan, undefined);
assert.strictEqual(
fooSpan!.parentSpanId,
Expand Down
Loading