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/plugin injection #1704

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions packages/opentelemetry-core/src/trace/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export interface PluginConfig {
*/
path?: string;

/**
* Plugin to load
* @example import {plugin} from '@opentelemetry/plugin-http' in case of http.
*/
plugin?: Plugin;

/**
* Request methods that match any string in ignoreMethods will not be traced.
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/opentelemetry-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ In the following example:
- the default express plugin is disabled
- the http plugin has a custom config for a `requestHook`
- the customPlugin is loaded from the user supplied path
- the customPlugin2 is resolved locally and injected
- all default plugins are still loaded if installed.

```js
import {plugin as httpsPlugin} from '@opentelemetry/plugin-https';

const provider = new NodeTracerProvider({
plugins: {
express: {
Expand All @@ -98,6 +101,9 @@ const provider = new NodeTracerProvider({
customPlugin: {
path: "/path/to/custom/module",
},
customPlugin2: {
plugin: httpsPlugin,
},
},
});
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export interface Plugins {
function filterPlugins(plugins: Plugins): Plugins {
const keys = Object.keys(plugins);
return keys.reduce((acc: Plugins, key: string) => {
if (plugins[key].enabled && plugins[key].path) acc[key] = plugins[key];
if (plugins[key].enabled && (plugins[key].path || plugins[key].plugin)) {
acc[key] = plugins[key];
}
return acc;
}, {});
}
Expand Down Expand Up @@ -107,6 +109,7 @@ export class PluginLoader {
if (this._hookState !== HookState.ENABLED) return exports;
const config = pluginsToLoad[name];
const modulePath = config.path!;
const modulePlugin = config.plugin;
let version = null;

if (!baseDir) {
Expand Down Expand Up @@ -150,7 +153,7 @@ export class PluginLoader {

// Expecting a plugin from module;
try {
const plugin: Plugin = require(modulePath).plugin;
const plugin: Plugin = modulePlugin ?? require(modulePath).plugin;
if (!utils.isSupportedVersion(version, plugin.supportedVersions)) {
this.logger.warn(
`PluginLoader#load: Plugin ${name} only supports module ${plugin.moduleName} with the versions: ${plugin.supportedVersions}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ const simplePlugins: Plugins = {
},
};

const simpleModulePlugin = require('@opentelemetry/plugin-simple-module');
const resolvedPlugins: Plugins = {
'simple-module': {
enabled: true,
plugin: simpleModulePlugin.plugin,
},
};

const httpPlugins: Plugins = {
http: {
enabled: true,
Expand Down Expand Up @@ -247,6 +255,18 @@ describe('PluginLoader', () => {
pluginLoader.unload();
});

it('should load a resolved plugin and patch the target modules', () => {
const pluginLoader = new PluginLoader(provider, logger);
assert.strictEqual(pluginLoader['_plugins'].length, 0);
pluginLoader.load(resolvedPlugins);
// The hook is only called the first time the module is loaded.
const simpleModule = require('simple-module');
assert.strictEqual(pluginLoader['_plugins'].length, 1);
assert.strictEqual(simpleModule.value(), 1);
assert.strictEqual(simpleModule.name(), 'patched-simple-module');
pluginLoader.unload();
});

it('should load a plugin and patch the core module', () => {
const pluginLoader = new PluginLoader(provider, logger);
assert.strictEqual(pluginLoader['_plugins'].length, 0);
Expand Down