Skip to content

Commit

Permalink
warn when hooked module is already loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
nozik committed Apr 25, 2022
1 parent cfda625 commit 44b8e6d
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,37 @@ export abstract class InstrumentationBase<T = any>
'No modules instrumentation has been defined,' +
' nothing will be patched'
);
} else {
this._warnOnPreloadedModules();
}

if (this._config.enabled) {
this.enable();
}
}

private _warnOnPreloadedModules(): void {
const preloadedModules: string[] = [];
this._modules.forEach((module: InstrumentationModuleDefinition<T>) => {
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
Expand Down

0 comments on commit 44b8e6d

Please sign in to comment.