Skip to content

Commit

Permalink
feat: allow enabling instrumentations selected via environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
atsu85 committed Feb 27, 2024
1 parent 02e65b8 commit c7393d9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
15 changes: 15 additions & 0 deletions metapackages/auto-instrumentations-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ For example, to enable only the `env`, `host` detectors:
export OTEL_NODE_RESOURCE_DETECTORS="env,host"
```


By default, all [Supported Instrumentations](#supported-instrumentations) are enabled,
but you can use the environment variable `OTEL_ENABLED_NODE_INSTRUMENTATIONS` to enable only certain instrumentations
by providing a comma-separated list of the instrumentation package names without the `@opentelemetry/instrumentation-` prefix.

For example, to enable only
[@opentelemetry/instrumentation-http](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation-http)
and [@opentelemetry/instrumentation-nestjs-core](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-nestjs-core)
instrumentations:

```shell
export OTEL_ENABLED_NODE_INSTRUMENTATIONS="http,nestjs-core"
```


To enable logging for troubleshooting, set the log level by setting the `OTEL_LOG_LEVEL` environment variable to one of the following:

- `none`
Expand Down
24 changes: 23 additions & 1 deletion metapackages/auto-instrumentations-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export function getNodeAutoInstrumentations(
inputConfigs: InstrumentationConfigMap = {}
): Instrumentation[] {
checkManuallyProvidedInstrumentationNames(Object.keys(inputConfigs));
const enabledInstrumentationsFromEnv = getEnabledInstrumentationsFromEnv();

const instrumentations: Instrumentation[] = [];

Expand All @@ -146,7 +147,10 @@ export function getNodeAutoInstrumentations(
// Defaults are defined by the instrumentation itself
const userConfig: any = inputConfigs[name] ?? {};

if (userConfig.enabled === false) {
if (
userConfig.enabled === false ||
!enabledInstrumentationsFromEnv.includes(name)
) {
diag.debug(`Disabling instrumentation for ${name}`);
continue;
}
Expand All @@ -172,6 +176,24 @@ function checkManuallyProvidedInstrumentationNames(
}
}

/**
* Returns the list of instrumentations that are enabled based on the environment variable.
*/
function getEnabledInstrumentationsFromEnv() {
if (!process.env.OTEL_ENABLED_NODE_INSTRUMENTATIONS) {
return Object.keys(InstrumentationMap);
}

const instrumentationsFromEnv = process.env.OTEL_ENABLED_NODE_INSTRUMENTATIONS.split(
','
).map(
instrumentationPkgSuffix =>
`@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}`
);
checkManuallyProvidedInstrumentationNames(instrumentationsFromEnv);
return instrumentationsFromEnv;
}

export function getResourceDetectorsFromEnv(): Array<Detector | DetectorSync> {
const resourceDetectors = new Map<
string,
Expand Down
18 changes: 18 additions & 0 deletions metapackages/auto-instrumentations-node/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ describe('utils', () => {
assert.strictEqual(instrumentation, undefined);
});

it('should return only instrumentations enabled via OTEL_ENABLED_NODE_INSTRUMENTATIONS environment variable', () => {
process.env.OTEL_ENABLED_NODE_INSTRUMENTATIONS = 'http,aws-sdk, nestjs-core'; // separator with and without whitespaces should be allowed
try {
const instrumentations = getNodeAutoInstrumentations();

assert.deepStrictEqual(
new Set(instrumentations.map(i => i.instrumentationName)),
new Set([
'@opentelemetry/instrumentation-http',
'@opentelemetry/instrumentation-aws-sdk',
'@opentelemetry/instrumentation-nestjs-core',
])
);
} finally {
delete process.env.OTEL_ENABLED_NODE_INSTRUMENTATIONS;
}
});

it('should show error for none existing instrumentation', () => {
const spy = sinon.stub(diag, 'error');
const name = '@opentelemetry/instrumentation-http2';
Expand Down

0 comments on commit c7393d9

Please sign in to comment.