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 dc87a89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.

### :rocket: (Enhancement)

* feat: warn when hooked module is already loaded [#2926](https://github.com/open-telemetry/opentelemetry-js/pull/2926) @nozik

### :bug: (Bug Fix)

* fix: sanitize attributes inputs [#2881](https://github.com/open-telemetry/opentelemetry-js/pull/2881) @legendecas
Expand Down
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 dc87a89

Please sign in to comment.