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): Adds support to ignore a span by its layer name #2028

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions plugins/node/opentelemetry-instrumentation-koa/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Note that generator-based middleware are deprecated and won't be instrumented.
| Options | Type | Example | Description |
| ------------------ | ----------------------------------- | -------------------- | -------------------------------------------------------------------------------------------------------- |
| `ignoreLayersType` | `KoaLayerType[]` | `['middleware']` | Ignore layers of specified type. |
| `ignoreLayersName` | `string[]` | `['logger']` | Ignore layers with specified names. |
| `requestHook` | `KoaRequestCustomAttributeFunction` | `(span, info) => {}` | Function for adding custom attributes to Koa middleware layers. Receives params: `Span, KoaRequestInfo`. |

`ignoreLayersType` accepts an array of `KoaLayerType` which can take the following string values:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
import type * as koa from 'koa';
import { KoaLayerType, KoaInstrumentationConfig } from './types';
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
import { getMiddlewareMetadata, isLayerIgnored } from './utils';
import {
getMiddlewareMetadata,
isLayerIgnored,
isLayerNameIgnored,
} from './utils';
import { getRPCMetadata, RPCType } from '@opentelemetry/core';
import {
kLayerPatched,
Expand Down Expand Up @@ -161,6 +165,11 @@
isRouter,
layerPath
);

if (isLayerNameIgnored(metadata.layerName, this.getConfig())) {
return middlewareLayer(context, next);

Check warning on line 170 in plugins/node/opentelemetry-instrumentation-koa/src/instrumentation.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L170 was not covered by tests
}

const span = this.tracer.startSpan(metadata.name, {
attributes: metadata.attributes,
});
Expand Down
2 changes: 2 additions & 0 deletions plugins/node/opentelemetry-instrumentation-koa/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export interface KoaInstrumentationConfig<
> extends InstrumentationConfig {
/** Ignore specific layers based on their type */
ignoreLayersType?: KoaLayerType[];
/** Ignore specific layers based on their name */
ignoreLayersName?: string[];
/** Function for adding custom attributes to each middleware layer span */
requestHook?: KoaRequestCustomAttributeFunction<
KoaContextType,
Expand Down
19 changes: 19 additions & 0 deletions plugins/node/opentelemetry-instrumentation-koa/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const getMiddlewareMetadata = (
): {
attributes: Attributes;
name: string;
layerName: string;
} => {
if (isRouter) {
return {
Expand All @@ -36,6 +37,7 @@ export const getMiddlewareMetadata = (
[SEMATTRS_HTTP_ROUTE]: layerPath?.toString(),
},
name: context._matchedRouteName || `router - ${layerPath}`,
layerName: context._matchedRouteName || layerPath?.toString() || '',
};
} else {
return {
Expand All @@ -44,6 +46,7 @@ export const getMiddlewareMetadata = (
[AttributeNames.KOA_TYPE]: KoaLayerType.MIDDLEWARE,
},
name: `middleware - ${layer.name}`,
layerName: layer.name,
};
}
};
Expand All @@ -63,3 +66,19 @@ export const isLayerIgnored = (
config?.ignoreLayersType?.includes(type)
);
};

/**
* Check whether the given request name is ignored by configuration
* @param [list] List of ignore name patterns
* @param [onException] callback for doing something when an exception has
* occurred
*/
export const isLayerNameIgnored = (
layerName: string,
config?: KoaInstrumentationConfig
): boolean => {
return !!(
Array.isArray(config?.ignoreLayersName) &&
config?.ignoreLayersName?.includes(layerName)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ describe('Koa Instrumentation', () => {
'--experimental-loader=@opentelemetry/instrumentation/hook.mjs',
NODE_NO_WARNINGS: '1',
},
checkResult: (err, stdout, stderr) => {
checkResult: (err: any, stdout: any, stderr: any) => {
assert.ifError(err);
},
checkCollector: (collector: testUtils.TestCollector) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,47 @@ describe('Utils', () => {
);
});
});
describe('isLayerNameIgnored()', () => {
it('should not fail with invalid config', () => {
assert.strictEqual(utils.isLayerNameIgnored(''), false);
assert.strictEqual(
utils.isLayerNameIgnored('', {} as KoaInstrumentationConfig),
false
);
assert.strictEqual(
utils.isLayerNameIgnored('', {
ignoreLayersName: {},
} as KoaInstrumentationConfig),
false
);
assert.strictEqual(
utils.isLayerNameIgnored('logger', {
ignoreLayersName: {},
} as KoaInstrumentationConfig),
false
);
assert.strictEqual(utils.isLayerNameIgnored('logger'), false);
assert.strictEqual(
utils.isLayerNameIgnored('', {
ignoreLayersName: [],
} as KoaInstrumentationConfig),
false
);
});

it('should ignore based on type', () => {
assert.strictEqual(
utils.isLayerNameIgnored('logger', {
ignoreLayersName: ['logger'],
}),
true
);
assert.strictEqual(
utils.isLayerNameIgnored('', {
ignoreLayersName: ['logger'],
}),
false
);
});
});
});