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