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
Changes from 1 commit
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
Next Next commit
feat: warn user when a instrumented package was already required #636
vmarchaud committed Feb 8, 2020
commit f31ee6dc180cb5bfde6e4dde98bc88f769fdf5ca
10 changes: 10 additions & 0 deletions packages/opentelemetry-node/src/instrumentation/PluginLoader.ts
Original file line number Diff line number Diff line change
@@ -83,6 +83,16 @@ 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.some(cached => cached.includes(name))
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
);
if (requiredModulesToHook.length > 0) {
this.logger.warn(
`Some modules (${requiredModulesToHook}) were already required when their respective plugin were loaded, some plugins might not work. Make sure the SDK is setup before you require in other modules.`
vmarchaud marked this conversation as resolved.
Show resolved Hide resolved
);
}

// 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;
Original file line number Diff line number Diff line change
@@ -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();
@@ -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(tracer, verifyWarnLogger);
pluginLoader.load(alreadyRequiredPlugins);
pluginLoader.unload();
});
});

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

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.