From 4eb2c7858c687d9408a9dc7e49952d5a6c956ae4 Mon Sep 17 00:00:00 2001 From: Siim Kallas Date: Mon, 19 Apr 2021 16:23:59 +0300 Subject: [PATCH] fix(instrumentation): support multiple module definitions with different versions --- .../src/platform/node/instrumentation.ts | 34 ++++++------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index ba94ce1411..e31476a369 100644 --- a/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -59,22 +59,6 @@ export abstract class InstrumentationBase } } - private _isSupported(name: string, version: string): boolean { - for (const module of this._modules) { - if (module.name === name) { - if (!module.supportedVersions) { - return true; - } - - return module.supportedVersions.some(supportedVersion => { - return semver.satisfies(version, supportedVersion); - }); - } - } - - return false; - } - private _onRequire( module: InstrumentationModuleDefinition, exports: T, @@ -93,7 +77,10 @@ export abstract class InstrumentationBase module.moduleVersion = version; if (module.name === name) { // main module - if (typeof version === 'string' && this._isSupported(name, version)) { + if ( + typeof version === 'string' && + isSupported(module.supportedVersions, version) + ) { if (typeof module.patch === 'function') { module.moduleExports = exports; if (this._enabled) { @@ -105,12 +92,7 @@ export abstract class InstrumentationBase // internal file const files = module.files ?? []; const file = files.find(file => file.name === name); - if ( - file && - file.supportedVersions.some(supportedVersion => - semver.satisfies(version, supportedVersion) - ) - ) { + if (file && isSupported(file.supportedVersions, version)) { file.moduleExports = exports; if (this._enabled) { return file.patch(exports, module.moduleVersion); @@ -179,3 +161,9 @@ export abstract class InstrumentationBase } } } + +function isSupported(supportedVersions: string[], version: string): boolean { + return supportedVersions.some(supportedVersion => { + return semver.satisfies(version, supportedVersion); + }); +}