From 1b78f5e9d2de699664525eec6e97184f920057a7 Mon Sep 17 00:00:00 2001 From: David Luna Date: Mon, 24 Feb 2025 15:58:19 +0100 Subject: [PATCH] feat(otel-node): restrict instrumentations to a list of know ones --- .../lib/instrumentations.js | 28 ++++++++++++++----- ...DE_ENABLE_DISABLE_INSTRUMENTATIONS.test.js | 8 ++---- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/packages/opentelemetry-node/lib/instrumentations.js b/packages/opentelemetry-node/lib/instrumentations.js index 896527ec..341ed631 100644 --- a/packages/opentelemetry-node/lib/instrumentations.js +++ b/packages/opentelemetry-node/lib/instrumentations.js @@ -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 @@ -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}` ); diff --git a/packages/opentelemetry-node/test/OTEL_NODE_ENABLE_DISABLE_INSTRUMENTATIONS.test.js b/packages/opentelemetry-node/test/OTEL_NODE_ENABLE_DISABLE_INSTRUMENTATIONS.test.js index e04fb908..da565f85 100644 --- a/packages/opentelemetry-node/test/OTEL_NODE_ENABLE_DISABLE_INSTRUMENTATIONS.test.js +++ b/packages/opentelemetry-node/test/OTEL_NODE_ENABLE_DISABLE_INSTRUMENTATIONS.test.js @@ -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( @@ -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' ); },