Skip to content

Commit

Permalink
fix(node): allow passing in plugin loader instead of a module path
Browse files Browse the repository at this point in the history
In environments like Yarn PnP you cannot call `require` to load arbitrary modules,
you can only load modules that your module explicitly lists in its dependencies.

This change allows developers in these environments to pass in a function that does
the require operation within the context of a module that does declare the
appropriate dependency.
  • Loading branch information
dobesv committed Jan 20, 2021
1 parent 2fcb76d commit 6ec7660
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 9 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,15 @@ export interface PluginConfig {
*/
path?: string;

/**
* Function to load the plugin. Needed in environments like Yarn PnP
* where modules cannot use require() to load modules not listed in their
* own dependencies.
*
* Example: `loader: () => require('@opentelemetry/plugin-xxx').plugin`
*/
loader?: () => Plugin;

/**
* Request methods that match any string in ignoreMethods will not be traced.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ export class PluginLoader {

// Expecting a plugin from module;
try {
const plugin: Plugin = require(modulePath).plugin;
const plugin: Plugin = config.loader
? config.loader()
: 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

0 comments on commit 6ec7660

Please sign in to comment.