Skip to content

Commit 47bed61

Browse files
committed
feat(koa): Adds support to ignore a span by its layer name
1 parent 8015d74 commit 47bed61

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ 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 { getMiddlewareMetadata, isLayerIgnored, isLayerNameIgnored } from './utils';
2929
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
3030
import {
3131
kLayerPatched,
@@ -174,6 +174,11 @@ export class KoaInstrumentation extends InstrumentationBase<typeof koa> {
174174
isRouter,
175175
layerPath
176176
);
177+
178+
if (isLayerNameIgnored(metadata.layerName, this.getConfig())) {
179+
return middlewareLayer(context, next);
180+
}
181+
177182
const span = this.tracer.startSpan(metadata.name, {
178183
attributes: metadata.attributes,
179184
});

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
[SemanticAttributes.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
@@ -720,7 +720,7 @@ describe('Koa Instrumentation', () => {
720720
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
721721
NODE_NO_WARNINGS: '1',
722722
},
723-
checkResult: (err, stdout, stderr) => {
723+
checkResult: (err: any, stdout: any, stderr: any) => {
724724
assert.ifError(err);
725725
},
726726
checkCollector: (collector: testUtils.TestCollector) => {

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,53 @@ 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(
66+
'',
67+
{} as KoaInstrumentationConfig
68+
),
69+
false
70+
);
71+
assert.strictEqual(
72+
utils.isLayerNameIgnored('', {
73+
ignoreLayersName: {},
74+
} as KoaInstrumentationConfig),
75+
false
76+
);
77+
assert.strictEqual(
78+
utils.isLayerNameIgnored('logger', {
79+
ignoreLayersName: {},
80+
} as KoaInstrumentationConfig),
81+
false
82+
);
83+
assert.strictEqual(
84+
utils.isLayerNameIgnored('logger'),
85+
false
86+
);
87+
assert.strictEqual(
88+
utils.isLayerNameIgnored('', {
89+
ignoreLayersName: []
90+
} as KoaInstrumentationConfig),
91+
false
92+
);
93+
});
94+
95+
it('should ignore based on type', () => {
96+
assert.strictEqual(
97+
utils.isLayerNameIgnored('logger', {
98+
ignoreLayersName: ['logger'],
99+
}),
100+
true
101+
);
102+
assert.strictEqual(
103+
utils.isLayerNameIgnored('', {
104+
ignoreLayersName: ['logger'],
105+
}),
106+
false
107+
);
108+
});
109+
});
61110
});

0 commit comments

Comments
 (0)