Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warn user when a instrumented package was already required #636 #654

Merged
merged 7 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/opentelemetry-node/src/instrumentation/PluginLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@ export class PluginLoader {
return this;
}

const alreadyRequiredModules = Object.keys(require.cache);
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
const requiredModulesToHook = modulesToHook.filter(
name =>
alreadyRequiredModules.find(cached => {
try {
return require.resolve(name) === cached;
} catch (err) {
return false;
}
}) !== undefined
);
if (requiredModulesToHook.length > 0) {
this.logger.warn(
`Some modules (${requiredModulesToHook.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.`
);
}

// Enable the require hook.
hook(modulesToHook, (exports, name, baseDir) => {
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
if (this._hookState !== HookState.ENABLED) return exports;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ const notSupportedVersionPlugins: Plugins = {
},
};

const alreadyRequiredPlugins: Plugins = {
'already-require-module': {
enabled: true,
path: '@opentelemetry/plugin-supported-module',
},
};

describe('PluginLoader', () => {
const provider = new NoopTracerProvider();
const logger = new NoopLogger();
Expand Down Expand Up @@ -219,6 +226,23 @@ describe('PluginLoader', () => {
assert.strictEqual(require('simple-module').value(), 0);
pluginLoader.unload();
});

it(`should warn when module was already loaded`, callback => {
const verifyWarnLogger = {
error: logger.error,
info: logger.info,
debug: logger.debug,
warn: (message: string, ...args: unknown[]) => {
assert(message.match(/were already required when/));
assert(message.match(/(already-require-module)/));
return callback();
},
};
require('already-require-module');
const pluginLoader = new PluginLoader(provider, verifyWarnLogger);
pluginLoader.load(alreadyRequiredPlugins);
pluginLoader.unload();
});
});

describe('.unload()', () => {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.