Skip to content

Commit 60a17ee

Browse files
authored
feat: warn when hooked module is already loaded (#2926)
1 parent ce7e98f commit 60a17ee

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
99
### :rocket: (Enhancement)
1010

1111
* feat(ConsoleSpanExporter): export span links [#2917](https://github.com/open-telemetry/opentelemetry-js/pull/2917) @trentm
12+
* feat: warn when hooked module is already loaded [#2926](https://github.com/open-telemetry/opentelemetry-js/pull/2926) @nozik
1213

1314
### :bug: (Bug Fix)
1415

experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts

+16
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ export abstract class InstrumentationBase<T = any>
5959
}
6060
}
6161

62+
private _warnOnPreloadedModules(): void {
63+
this._modules.forEach((module: InstrumentationModuleDefinition<T>) => {
64+
const { name } = module;
65+
try {
66+
const resolvedModule = require.resolve(name);
67+
if (require.cache[resolvedModule]) {
68+
// Module is already cached, which means the instrumentation hook might not work
69+
this._diag.warn(`Module ${name} has been loaded before ${this.instrumentationName} so it might not work, please initialize it before requiring ${name}`);
70+
}
71+
} catch {
72+
// Module isn't available, we can simply skip
73+
}
74+
});
75+
}
76+
6277
private _extractPackageVersion(baseDir: string): string | undefined {
6378
try {
6479
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -134,6 +149,7 @@ export abstract class InstrumentationBase<T = any>
134149
return;
135150
}
136151

152+
this._warnOnPreloadedModules();
137153
for (const module of this._modules) {
138154
this._hooks.push(
139155
RequireInTheMiddle(

0 commit comments

Comments
 (0)