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

Decouple plugin loader from tracer provider #1186

Closed
dyladan opened this issue Jun 11, 2020 · 1 comment
Closed

Decouple plugin loader from tracer provider #1186

dyladan opened this issue Jun 11, 2020 · 1 comment

Comments

@dyladan
Copy link
Member

dyladan commented Jun 11, 2020

Currently, instrumentations are loaded by the PluginLoader which is called by the NodeTracerProvider.

  1. This setup assumes that the user wants to use traces and not just metrics
  2. This setup encourages tracing-only plugins
  3. This setup does not allow easily sharing plugin instances between meter providers and tracer providers.

Suggested Solution

  • Remove the PluginLoader entirely
  • Update base plugin class to include a hook method which registers a require in the middle hook
import * as hook from "require-in-the-middle";

abstract class BasePlugin {
  abstract readonly name: string;
  abstract readonly version?: string;

  abstract readonly patchModule: string;
  abstract readonly supportedVersions: string[];

  abstract enable(
    moduleExports: T,
    logger: Logger,
    config?: PluginConfig
  ): T;

  constructor(config) {

  }

  hook() {
    hook([this.modulename], (exports: unknown, name: string, basedir?: string) => {
      if (basedir && this._alreadyRequiredModule(basedir)) {
        // log warning
        return exports;
      }

      const version = _getVersion(basedir);
      if (!_isVersionSupported(version)) {
        // log?
        return exports;
      }

      // kept the old signature for backwards compatibility reasons.
      // this should allow us to keep the old method and upgrade over time.
      return this.enable(exports, this.logger, this.config);
    });
  }

  private _alreadyRequiredModule(baseDir: string): boolean {
    const cache = new Set(Object.keys(require.cache));
  }
}

class HttpPlugin extends BasePlugin {
  public readonly name: "@opentelemetry/plugin-http";
  public readonly version: "0.8.1";

  public readonly patchModule: "http";

  // same enable method as current
}

const httpPlugin = new HttpPlugin({/* config */});
httpPlugin.hook();

This would also allow us to export a hook method from the plugin metapackage, which the @opentelemetry/node-sdk could call, moving the list of default supported plugins out of the node class and making the metapackage act as the default plugins list.

const plugins = tryRequire("@opentelemetry/plugins-node-all");

if (plugins) { plugins.hook() }
@pichlermarc
Copy link
Member

Closing as the PluginLoader and associated concepts do not exist anymore as of #2081 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants