diff --git a/packages/opentelemetry-api/README.md b/packages/opentelemetry-api/README.md index a000ae98fca..387b537ff4d 100644 --- a/packages/opentelemetry-api/README.md +++ b/packages/opentelemetry-api/README.md @@ -112,7 +112,7 @@ Apache 2.0 - See [LICENSE][license-url] for more information. [devDependencies-image]: https://david-dm.org/open-telemetry/opentelemetry-js/dev-status.svg?path=packages/opentelemetry-api [devDependencies-url]: https://david-dm.org/open-telemetry/opentelemetry-js?path=packages%2Fopentelemetry-api&type=dev [npm-url]: https://www.npmjs.com/package/@opentelemetry/api -[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Ftypes.svg +[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fapi.svg [trace-api-docs]: https://open-telemetry.github.io/opentelemetry-js/classes/traceapi.html [metrics-api-docs]: https://open-telemetry.github.io/opentelemetry-js/classes/metricsapi.html @@ -120,4 +120,4 @@ Apache 2.0 - See [LICENSE][license-url] for more information. [web]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-web [tracing]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-tracing [node]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node -[metrics]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-metrics \ No newline at end of file +[metrics]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-metrics diff --git a/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts b/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts index 67a54f3fefc..c255fbe8573 100644 --- a/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts +++ b/packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts @@ -28,6 +28,11 @@ export interface Plugin { */ supportedVersions?: string[]; + /** + * Name of the module that the plugin instrument. + */ + moduleName: string; + /** * Method that enables the instrumentation patch. * @param moduleExports The value of the `module.exports` property that would diff --git a/packages/opentelemetry-base/tsconfig-release.json b/packages/opentelemetry-base/tsconfig-release.json deleted file mode 100644 index ffc0f77968b..00000000000 --- a/packages/opentelemetry-base/tsconfig-release.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "types": [] - }, - "include": ["src/**/*.ts"] -} diff --git a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts b/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts index 16cb0dfbf63..601939df36e 100644 --- a/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts +++ b/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts @@ -29,7 +29,7 @@ import * as path from 'path'; /** This class represent the base to patch plugin. */ export abstract class BasePlugin implements Plugin { supportedVersions?: string[]; - readonly moduleName?: string; // required for internalFilesExports + abstract readonly moduleName: string; // required for internalFilesExports readonly version?: string; // required for internalFilesExports protected readonly _basedir?: string; // required for internalFilesExports diff --git a/packages/opentelemetry-core/tsconfig-release.json b/packages/opentelemetry-core/tsconfig-release.json deleted file mode 100644 index ffc0f77968b..00000000000 --- a/packages/opentelemetry-core/tsconfig-release.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "types": [] - }, - "include": ["src/**/*.ts"] -} diff --git a/packages/opentelemetry-exporter-collector/tsconfig-release.json b/packages/opentelemetry-exporter-collector/tsconfig-release.json deleted file mode 100644 index ffc0f77968b..00000000000 --- a/packages/opentelemetry-exporter-collector/tsconfig-release.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "types": [] - }, - "include": ["src/**/*.ts"] -} diff --git a/packages/opentelemetry-exporter-stackdriver-trace/src/trace.ts b/packages/opentelemetry-exporter-stackdriver-trace/src/trace.ts index 1e5c5f41259..80d54ebac63 100644 --- a/packages/opentelemetry-exporter-stackdriver-trace/src/trace.ts +++ b/packages/opentelemetry-exporter-stackdriver-trace/src/trace.ts @@ -37,7 +37,7 @@ export class StackdriverTraceExporter implements SpanExporter { private static readonly _cloudTrace = google.cloudtrace('v2'); - constructor(options: StackdriverExporterOptions) { + constructor(options: StackdriverExporterOptions = {}) { this._logger = options.logger || new NoopLogger(); this._auth = new GoogleAuth({ diff --git a/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts b/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts index d9d271acfc9..5517134bc7c 100644 --- a/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts +++ b/packages/opentelemetry-node/src/instrumentation/PluginLoader.ts @@ -83,6 +83,25 @@ export class PluginLoader { return this; } + const alreadyRequiredModules = Object.keys(require.cache); + 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) => { if (this._hookState !== HookState.ENABLED) return exports; @@ -113,8 +132,16 @@ export class PluginLoader { // Expecting a plugin from module; try { const plugin: Plugin = require(modulePath).plugin; - if (!utils.isSupportedVersion(version, plugin.supportedVersions)) { + this.logger.error( + `PluginLoader#load: Plugin ${name} only supports module ${plugin.moduleName} with the versions: ${plugin.supportedVersions}` + ); + return exports; + } + if (plugin.moduleName !== name) { + this.logger.error( + `PluginLoader#load: Entry ${name} use a plugin that instruments ${plugin.moduleName}` + ); return exports; } diff --git a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts index ad4edb7863a..5093d448f0d 100644 --- a/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts +++ b/packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts @@ -86,6 +86,20 @@ const notSupportedVersionPlugins: Plugins = { }, }; +const alreadyRequiredPlugins: Plugins = { + 'already-require-module': { + enabled: true, + path: '@opentelemetry/plugin-supported-module', + }, +}; + +const differentNamePlugins: Plugins = { + 'random-module': { + enabled: true, + path: '@opentelemetry/plugin-http-module', + }, +}; + describe('PluginLoader', () => { const provider = new NoopTracerProvider(); const logger = new NoopLogger(); @@ -219,6 +233,33 @@ 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(); + }); + + it('should not load a plugin that patches a different module that the one configured', () => { + const pluginLoader = new PluginLoader(provider, logger); + assert.strictEqual(pluginLoader['_plugins'].length, 0); + pluginLoader.load(differentNamePlugins); + // @ts-ignore only to trigger the loading of the plugin + const randomModule = require('random-module'); + assert.strictEqual(pluginLoader['_plugins'].length, 0); + pluginLoader.unload(); + }); }); describe('.unload()', () => { diff --git a/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-http-module/http-module.js b/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-http-module/http-module.js index 06f69e50f63..2c46ac30ee2 100644 --- a/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-http-module/http-module.js +++ b/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-http-module/http-module.js @@ -5,6 +5,7 @@ const shimmer = require("shimmer"); class HttpModulePlugin extends core_1.BasePlugin { constructor() { super(); + this.moduleName = 'http'; } patch() { diff --git a/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-notsupported-module/simple-module.js b/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-notsupported-module/simple-module.js index 86a27bc32d9..92bb7d4e807 100644 --- a/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-notsupported-module/simple-module.js +++ b/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-notsupported-module/simple-module.js @@ -5,6 +5,7 @@ const shimmer = require("shimmer"); class SimpleModulePlugin extends core_1.BasePlugin { constructor() { super(); + this.moduleName = 'notsupported-module'; } patch() { diff --git a/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-simple-module/simple-module.js b/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-simple-module/simple-module.js index 142b861d222..67e8f838d46 100644 --- a/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-simple-module/simple-module.js +++ b/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-simple-module/simple-module.js @@ -5,6 +5,7 @@ const shimmer = require("shimmer"); class SimpleModulePlugin extends core_1.BasePlugin { constructor() { super(); + this.moduleName = 'simple-module'; } patch() { diff --git a/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-supported-module/simple-module.js b/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-supported-module/simple-module.js index fb2a965e7f3..6395bd261af 100644 --- a/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-supported-module/simple-module.js +++ b/packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-supported-module/simple-module.js @@ -5,6 +5,7 @@ const shimmer = require("shimmer"); class SimpleModulePlugin extends core_1.BasePlugin { constructor() { super(); + this.moduleName = 'supported-module'; } patch() { diff --git a/packages/opentelemetry-node/test/instrumentation/node_modules/already-require-module/index.js b/packages/opentelemetry-node/test/instrumentation/node_modules/already-require-module/index.js new file mode 100644 index 00000000000..18c0f69a3b9 --- /dev/null +++ b/packages/opentelemetry-node/test/instrumentation/node_modules/already-require-module/index.js @@ -0,0 +1,4 @@ +module.exports = { + name: () => 'already-module', + value: () => 0, +}; diff --git a/packages/opentelemetry-node/test/instrumentation/node_modules/already-require-module/package.json b/packages/opentelemetry-node/test/instrumentation/node_modules/already-require-module/package.json new file mode 100644 index 00000000000..7ae0ab8f096 --- /dev/null +++ b/packages/opentelemetry-node/test/instrumentation/node_modules/already-require-module/package.json @@ -0,0 +1,4 @@ +{ + "name": "already-module", + "version": "0.1.0" +} diff --git a/packages/opentelemetry-node/test/instrumentation/node_modules/random-module/index.js b/packages/opentelemetry-node/test/instrumentation/node_modules/random-module/index.js new file mode 100644 index 00000000000..35a4110c28e --- /dev/null +++ b/packages/opentelemetry-node/test/instrumentation/node_modules/random-module/index.js @@ -0,0 +1,4 @@ +module.exports = { + name: () => 'random-module', + value: () => 0, +}; diff --git a/packages/opentelemetry-node/test/instrumentation/node_modules/random-module/package.json b/packages/opentelemetry-node/test/instrumentation/node_modules/random-module/package.json new file mode 100644 index 00000000000..a5c840081be --- /dev/null +++ b/packages/opentelemetry-node/test/instrumentation/node_modules/random-module/package.json @@ -0,0 +1,4 @@ +{ + "name": "random-module", + "version": "0.1.0" +} diff --git a/packages/opentelemetry-plugin-xml-http-request/tsconfig-release.json b/packages/opentelemetry-plugin-xml-http-request/tsconfig-release.json deleted file mode 100644 index ffc0f77968b..00000000000 --- a/packages/opentelemetry-plugin-xml-http-request/tsconfig-release.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "types": [] - }, - "include": ["src/**/*.ts"] -} diff --git a/packages/opentelemetry-propagator-jaeger/tsconfig-release.json b/packages/opentelemetry-propagator-jaeger/tsconfig-release.json deleted file mode 100644 index ffc0f77968b..00000000000 --- a/packages/opentelemetry-propagator-jaeger/tsconfig-release.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "types": [] - }, - "include": ["src/**/*.ts"] -} diff --git a/packages/opentelemetry-web/test/WebTracer.test.ts b/packages/opentelemetry-web/test/WebTracer.test.ts index 308dec11658..1232cb0cc22 100644 --- a/packages/opentelemetry-web/test/WebTracer.test.ts +++ b/packages/opentelemetry-web/test/WebTracer.test.ts @@ -27,6 +27,8 @@ class DummyPlugin extends BasePlugin { constructor() { super('dummy'); } + moduleName = 'dummy'; + patch() {} unpatch() {} } diff --git a/packages/opentelemetry-web/tsconfig-release.json b/packages/opentelemetry-web/tsconfig-release.json deleted file mode 100644 index ffc0f77968b..00000000000 --- a/packages/opentelemetry-web/tsconfig-release.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "./tsconfig.json", - "compilerOptions": { - "types": [] - }, - "include": ["src/**/*.ts"] -}