Skip to content

Commit

Permalink
Merge branch 'master' into add-express-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud authored Feb 12, 2020
2 parents 1102ca2 + 9b2737a commit 75dca3e
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 47 deletions.
4 changes: 2 additions & 2 deletions packages/opentelemetry-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ 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

[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
[metrics]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-metrics
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export interface Plugin<T = any> {
*/
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
Expand Down
7 changes: 0 additions & 7 deletions packages/opentelemetry-base/tsconfig-release.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import * as path from 'path';
/** This class represent the base to patch plugin. */
export abstract class BasePlugin<T> implements Plugin<T> {
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

Expand Down
7 changes: 0 additions & 7 deletions packages/opentelemetry-core/tsconfig-release.json

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
29 changes: 28 additions & 1 deletion 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);
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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()', () => {
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.

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.

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.

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.

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions packages/opentelemetry-web/test/WebTracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class DummyPlugin extends BasePlugin<unknown> {
constructor() {
super('dummy');
}
moduleName = 'dummy';

patch() {}
unpatch() {}
}
Expand Down
7 changes: 0 additions & 7 deletions packages/opentelemetry-web/tsconfig-release.json

This file was deleted.

0 comments on commit 75dca3e

Please sign in to comment.