Skip to content

Commit 882914e

Browse files
committed
feat(koa): Adds support to ignore a span by its layer name
1 parent 17a0bc1 commit 882914e

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

plugins/node/opentelemetry-instrumentation-koa/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Note that generator-based middleware are deprecated and won't be instrumented.
5252
| Options | Type | Example | Description |
5353
| ------------------ | ----------------------------------- | -------------------- | -------------------------------------------------------------------------------------------------------- |
5454
| `ignoreLayersType` | `KoaLayerType[]` | `['middleware']` | Ignore layers of specified type. |
55+
| `ignoreLayersName` | `string[]` | `['logger']` | Ignore layers with specified names. |
5556
| `requestHook` | `KoaRequestCustomAttributeFunction` | `(span, info) => {}` | Function for adding custom attributes to Koa middleware layers. Receives params: `Span, KoaRequestInfo`. |
5657

5758
`ignoreLayersType` accepts an array of `KoaLayerType` which can take the following string values:

plugins/node/opentelemetry-instrumentation-koa/src/instrumentation.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import {
2525
import type * as koa from 'koa';
2626
import { KoaContext, KoaLayerType, KoaInstrumentationConfig } from './types';
2727
import { VERSION } from './version';
28-
import { getMiddlewareMetadata, isLayerIgnored } from './utils';
28+
import {
29+
getMiddlewareMetadata,
30+
isLayerIgnored,
31+
isLayerNameIgnored,
32+
} from './utils';
2933
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
3034
import {
3135
kLayerPatched,
@@ -174,6 +178,11 @@ export class KoaInstrumentation extends InstrumentationBase<typeof koa> {
174178
isRouter,
175179
layerPath
176180
);
181+
182+
if (isLayerNameIgnored(metadata.layerName, this.getConfig())) {
183+
return middlewareLayer(context, next);
184+
}
185+
177186
const span = this.tracer.startSpan(metadata.name, {
178187
attributes: metadata.attributes,
179188
});

plugins/node/opentelemetry-instrumentation-koa/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export interface KoaRequestCustomAttributeFunction {
4646
export interface KoaInstrumentationConfig extends InstrumentationConfig {
4747
/** Ignore specific layers based on their type */
4848
ignoreLayersType?: KoaLayerType[];
49+
/** Ignore specific layers based on their name */
50+
ignoreLayersName?: string[];
4951
/** Function for adding custom attributes to each middleware layer span */
5052
requestHook?: KoaRequestCustomAttributeFunction;
5153
}

plugins/node/opentelemetry-instrumentation-koa/src/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const getMiddlewareMetadata = (
2727
): {
2828
attributes: Attributes;
2929
name: string;
30+
layerName: string;
3031
} => {
3132
if (isRouter) {
3233
return {
@@ -36,6 +37,7 @@ export const getMiddlewareMetadata = (
3637
[SEMATTRS_HTTP_ROUTE]: layerPath?.toString(),
3738
},
3839
name: context._matchedRouteName || `router - ${layerPath}`,
40+
layerName: context._matchedRouteName || layerPath?.toString() || '',
3941
};
4042
} else {
4143
return {
@@ -44,6 +46,7 @@ export const getMiddlewareMetadata = (
4446
[AttributeNames.KOA_TYPE]: KoaLayerType.MIDDLEWARE,
4547
},
4648
name: `middleware - ${layer.name}`,
49+
layerName: layer.name,
4750
};
4851
}
4952
};
@@ -63,3 +66,19 @@ export const isLayerIgnored = (
6366
config?.ignoreLayersType?.includes(type)
6467
);
6568
};
69+
70+
/**
71+
* Check whether the given request name is ignored by configuration
72+
* @param [list] List of ignore name patterns
73+
* @param [onException] callback for doing something when an exception has
74+
* occurred
75+
*/
76+
export const isLayerNameIgnored = (
77+
layerName: string,
78+
config?: KoaInstrumentationConfig
79+
): boolean => {
80+
return !!(
81+
Array.isArray(config?.ignoreLayersName) &&
82+
config?.ignoreLayersName?.includes(layerName)
83+
);
84+
};

plugins/node/opentelemetry-instrumentation-koa/test/koa.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ describe('Koa Instrumentation', () => {
721721
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
722722
NODE_NO_WARNINGS: '1',
723723
},
724-
checkResult: (err, stdout, stderr) => {
724+
checkResult: (err: any, stdout: any, stderr: any) => {
725725
assert.ifError(err);
726726
},
727727
checkCollector: (collector: testUtils.TestCollector) => {

plugins/node/opentelemetry-instrumentation-koa/test/utils.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,47 @@ describe('Utils', () => {
5858
);
5959
});
6060
});
61+
describe('isLayerNameIgnored()', () => {
62+
it('should not fail with invalid config', () => {
63+
assert.strictEqual(utils.isLayerNameIgnored(''), false);
64+
assert.strictEqual(
65+
utils.isLayerNameIgnored('', {} as KoaInstrumentationConfig),
66+
false
67+
);
68+
assert.strictEqual(
69+
utils.isLayerNameIgnored('', {
70+
ignoreLayersName: {},
71+
} as KoaInstrumentationConfig),
72+
false
73+
);
74+
assert.strictEqual(
75+
utils.isLayerNameIgnored('logger', {
76+
ignoreLayersName: {},
77+
} as KoaInstrumentationConfig),
78+
false
79+
);
80+
assert.strictEqual(utils.isLayerNameIgnored('logger'), false);
81+
assert.strictEqual(
82+
utils.isLayerNameIgnored('', {
83+
ignoreLayersName: [],
84+
} as KoaInstrumentationConfig),
85+
false
86+
);
87+
});
88+
89+
it('should ignore based on type', () => {
90+
assert.strictEqual(
91+
utils.isLayerNameIgnored('logger', {
92+
ignoreLayersName: ['logger'],
93+
}),
94+
true
95+
);
96+
assert.strictEqual(
97+
utils.isLayerNameIgnored('', {
98+
ignoreLayersName: ['logger'],
99+
}),
100+
false
101+
);
102+
});
103+
});
61104
});

0 commit comments

Comments
 (0)