Skip to content

Commit

Permalink
feat: 2 level merge of user supplied and default plugin configs
Browse files Browse the repository at this point in the history
Signed-off-by: Naseem <[email protected]>
  • Loading branch information
Naseem committed Apr 28, 2020
1 parent 32dc2c8 commit 434d742
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
24 changes: 21 additions & 3 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 @@ -40,10 +40,28 @@ export class NodeTracerProvider extends BasicTracerProvider {

this._pluginLoader = new PluginLoader(this, this.logger);

// Shallow-merge default plugins with user supplied config
/**
* For user supplied config of plugin(s) that are loaded by default,
* merge the user supplied and default configs of said plugin(s)
*/
let mergedUserSuppliedPlugins: Plugins = {};

for (const plugin in config.plugins) {
if (DEFAULT_INSTRUMENTATION_PLUGINS.hasOwnProperty(plugin)) {
mergedUserSuppliedPlugins[plugin] = {
...DEFAULT_INSTRUMENTATION_PLUGINS[plugin],
...config.plugins[plugin],
};
} else {
// TODO: enable non-default but user-configured plugins unless
// explicitly disabled
mergedUserSuppliedPlugins[plugin] = config.plugins[plugin];
}
}

this._pluginLoader.load({
...DEFAULT_INSTRUMENTATION_PLUGINS,
...config.plugins,
...mergedUserSuppliedPlugins,
});
}

Expand Down
7 changes: 6 additions & 1 deletion packages/opentelemetry-node/test/NodeTracerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('NodeTracerProvider', () => {
assert.ok(provider instanceof NodeTracerProvider);
});

it('should load user configured and default plugins that have been shallowly merged', () => {
it('should load a merge of user configured and default plugins', () => {
provider = new NodeTracerProvider({
logger: new NoopLogger(),
plugins: {
Expand All @@ -95,6 +95,9 @@ describe('NodeTracerProvider', () => {
ignoreMethods: [],
ignoreUrls: [],
},
http: {
path: '@opentelemetry/plugin-http-module',
},
},
});
const pluginLoader = provider['_pluginLoader'];
Expand All @@ -105,6 +108,8 @@ describe('NodeTracerProvider', () => {
assert.strictEqual(pluginLoader['_plugins'].length, 2);
require('http');
assert.strictEqual(pluginLoader['_plugins'].length, 3);
const httpPlugin = pluginLoader['_plugins'][2];
console.log(httpPlugin);
});

it('should construct an instance with default attributes', () => {
Expand Down

0 comments on commit 434d742

Please sign in to comment.