From 44b8e6dafb921aef4aa054dc409a053c5076e4c5 Mon Sep 17 00:00:00 2001 From: Ran Nozik Date: Wed, 20 Apr 2022 09:06:46 +0300 Subject: [PATCH] warn when hooked module is already loaded --- .../src/platform/node/instrumentation.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts index f67476930d7..19d4ca138a4 100644 --- a/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts +++ b/experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts @@ -52,6 +52,8 @@ export abstract class InstrumentationBase 'No modules instrumentation has been defined,' + ' nothing will be patched' ); + } else { + this._warnOnPreloadedModules(); } if (this._config.enabled) { @@ -59,6 +61,28 @@ export abstract class InstrumentationBase } } + private _warnOnPreloadedModules(): void { + const preloadedModules: string[] = []; + this._modules.forEach((module: InstrumentationModuleDefinition) => { + const { name } = module; + try { + const resolvedModule = require.resolve(name); + if (require.cache[resolvedModule]) { + // Module is already cached, which means the instrumentation hook might not work + preloadedModules.push(name); + } + } catch { + // Module isn't available, we can simply skip + } + }); + + if (!preloadedModules.length) { + return; + } + + this._diag.warn(`Some modules (${preloadedModules.join(', ')}) were already required when their respective plugin was loaded, some plugins might not work. Make sure the SDK is setup before you require in other modules.`); + } + private _extractPackageVersion(baseDir: string): string | undefined { try { // eslint-disable-next-line @typescript-eslint/no-var-requires