Skip to content

Commit dc87a89

Browse files
committed
warn when hooked module is already loaded
1 parent cfda625 commit dc87a89

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ All notable changes to this project will be documented in this file.
88

99
### :rocket: (Enhancement)
1010

11+
* feat: warn when hooked module is already loaded [#2926](https://github.com/open-telemetry/opentelemetry-js/pull/2926) @nozik
12+
1113
### :bug: (Bug Fix)
1214

1315
* fix: sanitize attributes inputs [#2881](https://github.com/open-telemetry/opentelemetry-js/pull/2881) @legendecas

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

+24
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,37 @@ export abstract class InstrumentationBase<T = any>
5252
'No modules instrumentation has been defined,' +
5353
' nothing will be patched'
5454
);
55+
} else {
56+
this._warnOnPreloadedModules();
5557
}
5658

5759
if (this._config.enabled) {
5860
this.enable();
5961
}
6062
}
6163

64+
private _warnOnPreloadedModules(): void {
65+
const preloadedModules: string[] = [];
66+
this._modules.forEach((module: InstrumentationModuleDefinition<T>) => {
67+
const { name } = module;
68+
try {
69+
const resolvedModule = require.resolve(name);
70+
if (require.cache[resolvedModule]) {
71+
// Module is already cached, which means the instrumentation hook might not work
72+
preloadedModules.push(name);
73+
}
74+
} catch {
75+
// Module isn't available, we can simply skip
76+
}
77+
});
78+
79+
if (!preloadedModules.length) {
80+
return;
81+
}
82+
83+
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.`);
84+
}
85+
6286
private _extractPackageVersion(baseDir: string): string | undefined {
6387
try {
6488
// eslint-disable-next-line @typescript-eslint/no-var-requires

0 commit comments

Comments
 (0)