Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3c4bc0a

Browse files
committedFeb 23, 2024
feat: allow enabling instrumentations selected via environment variable
Closes open-telemetry#1672
1 parent 1bd8972 commit 3c4bc0a

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed
 

‎metapackages/auto-instrumentations-node/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ For example, to enable only the `env`, `host` detectors:
7575
export OTEL_NODE_RESOURCE_DETECTORS="env,host"
7676
```
7777

78+
79+
By default, all [Supported Instrumentations](#supported-instrumentations) are enabled,
80+
but you can use the environment variable `OTEL_NODE_INSTRUMENTATIONS` to enable only certain instrumentations
81+
by providing a comma-separated list of the instrumentation package names without the `@opentelemetry/instrumentation-` prefix.
82+
83+
For example, to enable only
84+
[@opentelemetry/instrumentation-http](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation-http)
85+
and [@opentelemetry/instrumentation-nestjs-core](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-nestjs-core)
86+
instrumentations:
87+
88+
```shell
89+
export OTEL_NODE_INSTRUMENTATIONS="http,nestjs-core"
90+
```
91+
92+
7893
To enable logging for troubleshooting, set the log level by setting the `OTEL_LOG_LEVEL` environment variable to one of the following:
7994

8095
- `none`

‎metapackages/auto-instrumentations-node/src/utils.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export function getNodeAutoInstrumentations(
136136
inputConfigs: InstrumentationConfigMap = {}
137137
): Instrumentation[] {
138138
checkManuallyProvidedInstrumentationNames(Object.keys(inputConfigs));
139+
const instrumentationsFromEnv = getAllowedInstrumentationsFromEnv();
139140

140141
const instrumentations: Instrumentation[] = [];
141142

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

149-
if (userConfig.enabled === false) {
150+
if (
151+
userConfig.enabled === false ||
152+
!instrumentationsFromEnv.includes(name)
153+
) {
150154
diag.debug(`Disabling instrumentation for ${name}`);
151155
continue;
152156
}
@@ -172,6 +176,24 @@ function checkManuallyProvidedInstrumentationNames(
172176
}
173177
}
174178

179+
/**
180+
* Returns the list of instrumentations that are allowed based on the environment variable.
181+
*/
182+
function getAllowedInstrumentationsFromEnv() {
183+
if (!process.env.OTEL_NODE_INSTRUMENTATIONS) {
184+
return Object.keys(InstrumentationMap);
185+
}
186+
187+
const instrumentationsFromEnv = process.env.OTEL_NODE_INSTRUMENTATIONS.split(
188+
','
189+
).map(
190+
instrumentationPkgSuffix =>
191+
`@opentelemetry/instrumentation-${instrumentationPkgSuffix.trim()}`
192+
);
193+
checkManuallyProvidedInstrumentationNames(instrumentationsFromEnv);
194+
return instrumentationsFromEnv;
195+
}
196+
175197
export function getResourceDetectorsFromEnv(): Array<Detector | DetectorSync> {
176198
const resourceDetectors = new Map<
177199
string,

‎metapackages/auto-instrumentations-node/test/utils.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ describe('utils', () => {
7070
assert.strictEqual(instrumentation, undefined);
7171
});
7272

73+
it('should return only instrumentations enabled via OTEL_NODE_INSTRUMENTATIONS environment variable', () => {
74+
process.env.OTEL_NODE_INSTRUMENTATIONS = 'http,aws-sdk, nestjs-core'; // separator with and without whitespaces should be allowed
75+
try {
76+
const instrumentations = getNodeAutoInstrumentations();
77+
78+
assert.deepStrictEqual(
79+
new Set(instrumentations.map(i => i.instrumentationName)),
80+
new Set([
81+
'@opentelemetry/instrumentation-http',
82+
'@opentelemetry/instrumentation-aws-sdk',
83+
'@opentelemetry/instrumentation-nestjs-core',
84+
])
85+
);
86+
} finally {
87+
delete process.env.OTEL_NODE_INSTRUMENTATIONS;
88+
}
89+
});
90+
7391
it('should show error for none existing instrumentation', () => {
7492
const spy = sinon.stub(diag, 'error');
7593
const name = '@opentelemetry/instrumentation-http2';

0 commit comments

Comments
 (0)
Please sign in to comment.