Skip to content

Commit

Permalink
feat(otel-node): restrict instrumentations to a list of know ones
Browse files Browse the repository at this point in the history
  • Loading branch information
david-luna committed Feb 24, 2025
1 parent c038401 commit 1b78f5e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
28 changes: 21 additions & 7 deletions packages/opentelemetry-node/lib/instrumentations.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ const EXCLUDED_INSTRUMENTATIONS = new Set([
'@opentelemetry/instrumentation-fs',
]);

const OTEL_INSTRUMENTATION_PREFIX = '@opentelemetry/instrumentation-';
const OTEL_INSTR_SHORT_NAMES = new Set();
const NON_OTEL_INSTR_NAMES = new Set();
for (const name of Object.keys(INSTRUMENTATIONS)) {
if (name.startsWith(OTEL_INSTRUMENTATION_PREFIX)) {
OTEL_INSTR_SHORT_NAMES.add(
name.replace(OTEL_INSTRUMENTATION_PREFIX, '')
);
} else {
NON_OTEL_INSTR_NAMES.add(name);
}
}

/**
* Reads a string in the format `value1,value2` and parses
* it into an array. This is the format specified for comma separated
Expand All @@ -183,14 +196,15 @@ const EXCLUDED_INSTRUMENTATIONS = new Set([
*/
function getInstrumentationsFromEnv(envvar) {
if (process.env[envvar]) {
const instrumentations = process.env[envvar]
.split(',')
.map((s) => s.trim())
.filter((s) => s)
.map((s) => `@opentelemetry/instrumentation-${s}`);
const instrumentations = [];
const names = process.env[envvar].split(',').map((s) => s.trim());

for (const name of instrumentations) {
if (!INSTRUMENTATIONS[name]) {
for (const name of names) {
if (OTEL_INSTR_SHORT_NAMES.has(name)) {
instrumentations.push(`${OTEL_INSTRUMENTATION_PREFIX}${name}`);
} else if (NON_OTEL_INSTR_NAMES.has(name)) {
instrumentations.push(name);
} else {
log.warn(
`Unknown instrumentation "${name}" specified in the environment variable ${envvar}`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ const testFixtures = [
'should enable instrumentation passed with surroinding spaces in env var'
);
t.ok(
getLine(
'Unknown instrumentation \\"@opentelemetry/instrumentation-bogus\\"'
),
getLine('Unknown instrumentation \\"bogus\\"'),
'should print a log for the bogus value in enable env var'
);
t.notOk(
Expand Down Expand Up @@ -125,9 +123,7 @@ const testFixtures = [
'should enable instrumentation not set in the var'
);
t.ok(
getLine(
'Unknown instrumentation \\"@opentelemetry/instrumentation-bogus\\"'
),
getLine('Unknown instrumentation \\"bogus\\"'),
'should print a log for the bogus value in enable env var'
);
},
Expand Down

0 comments on commit 1b78f5e

Please sign in to comment.