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: merge user supplied and default plugin configs #980

Merged
merged 22 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
35 changes: 33 additions & 2 deletions packages/opentelemetry-node/src/NodeTracerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
SDKRegistrationConfig,
} from '@opentelemetry/tracing';
import { DEFAULT_INSTRUMENTATION_PLUGINS, NodeTracerConfig } from './config';
import { PluginLoader } from './instrumentation/PluginLoader';
import { PluginLoader, Plugins } from './instrumentation/PluginLoader';

/**
* Register this TracerProvider for use with the OpenTelemetry API.
Expand All @@ -39,7 +39,12 @@ export class NodeTracerProvider extends BasicTracerProvider {
super(config);

this._pluginLoader = new PluginLoader(this, this.logger);
this._pluginLoader.load(config.plugins || DEFAULT_INSTRUMENTATION_PLUGINS);

config.plugins
? this._pluginLoader.load(
this._mergePlugins(DEFAULT_INSTRUMENTATION_PLUGINS, config.plugins)
)
: this._pluginLoader.load(DEFAULT_INSTRUMENTATION_PLUGINS);
}

stop() {
Expand All @@ -54,4 +59,30 @@ export class NodeTracerProvider extends BasicTracerProvider {

super.register(config);
}

/**
* Two layer merge.
* First, for user supplied config of plugin(s) that are loaded by default,
* merge the user supplied and default configs of said plugin(s).
* Then merge the results with the default plugins.
* @returns 2-layer deep merge of default and user supplied plugins.
*/
private _mergePlugins(defaultPlugins: Plugins, userSuppliedPlugins: Plugins): Plugins {
const mergedUserSuppliedPlugins: Plugins = {};

for (const pluginName in userSuppliedPlugins) {
mergedUserSuppliedPlugins[pluginName] = {
// Any user-supplied non-default plugin should be enabled by default
...(DEFAULT_INSTRUMENTATION_PLUGINS[pluginName] || { enabled: true }),
...userSuppliedPlugins[pluginName],
};
}

const mergedPlugins: Plugins = {
...defaultPlugins,
...mergedUserSuppliedPlugins,
};

return mergedPlugins;
}
}
72 changes: 65 additions & 7 deletions packages/opentelemetry-node/test/NodeTracerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,38 @@ describe('NodeTracerProvider', () => {
assert.ok(provider instanceof NodeTracerProvider);
});

it('should load user configured plugins', () => {
it('should load a merge of user configured and default plugins and implictly enable non-default plugins', () => {
provider = new NodeTracerProvider({
logger: new NoopLogger(),
plugins: {
'simple-module': {
enabled: true,
path: '@opentelemetry/plugin-simple-module',
},
'supported-module': {
enabled: true,
path: '@opentelemetry/plugin-supported-module',
enhancedDatabaseReporting: false,
ignoreMethods: [],
ignoreUrls: [],
},
'random-module': {
enabled: false,
path: '@opentelemetry/random-module',
},
http: {
path: '@opentelemetry/plugin-http-module',
},
},
});
const pluginLoader = provider['_pluginLoader'];
assert.strictEqual(pluginLoader['_plugins'].length, 0);
const plugins = provider['_pluginLoader']['_plugins'];
assert.strictEqual(plugins.length, 0);
require('simple-module');
assert.strictEqual(pluginLoader['_plugins'].length, 1);
assert.strictEqual(plugins.length, 1);
require('supported-module');
assert.strictEqual(pluginLoader['_plugins'].length, 2);
assert.strictEqual(plugins.length, 2);
require('random-module');
assert.strictEqual(plugins.length, 2);
require('http');
assert.strictEqual(plugins.length, 3);
});

it('should construct an instance with default attributes', () => {
Expand Down Expand Up @@ -269,3 +278,52 @@ describe('NodeTracerProvider', () => {
});
});
});

describe('mergePlugins', () => {
const defaultPlugins = {
naseemkullah marked this conversation as resolved.
Show resolved Hide resolved
module1: {
enabled: true,
path: 'testpath',
},
module2: {
enabled: true,
path: 'testpath2',
},
module3: {
enabled: true,
path: 'testpath3',
},
};

const userPlugins = {
module2: {
path: 'userpath',
},
module3: {
enabled: false,
},
nonDefaultModule: {
path: 'userpath2',
},
};

const provider = new NodeTracerProvider();

const mergedPlugins = provider["_mergePlugins"](defaultPlugins, userPlugins);

it('should merge user and default configs', () => {
assert.equal(mergedPlugins.module1.enabled, true);
assert.equal(mergedPlugins.module1.path, 'testpath');
assert.equal(mergedPlugins.module2.enabled, true);
assert.equal(mergedPlugins.module2.path, 'userpath');
assert.equal(mergedPlugins.module3.enabled, false);
assert.equal(mergedPlugins.nonDefaultModule.enabled, true);
assert.equal(mergedPlugins.nonDefaultModule.path, 'userpath2');
});

it('should should not mangle default config', () => {
assert.equal(defaultPlugins.module2.path, 'testpath2');
assert.equal(defaultPlugins.module3.enabled, true);
assert.equal(defaultPlugins.module3.path, 'testpath3');
});
});

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