|
| 1 | +# Plugin Developer Guide |
| 2 | + |
| 3 | +The `NodeTracer` or `Node-SDK` is driven by a set of plugins that describe how to patch a module to generate trace spans when that module is used. We provide out-of-the-box instrumentation for many popular frameworks and libraries by using a plugin system (see [builtin plugins][builtin-plugins]), and provide a means for developers to create their own. |
| 4 | + |
| 5 | +We strongly recommended to create a dedicated package for newly added plugin, example: `@opentelemetry/plugin-xxx`. |
| 6 | + |
| 7 | +Each plugin must extend the abstract class [BasePlugin][base-plugin] implementing the below methods: |
| 8 | + |
| 9 | +- `patch`: A function describing how the module exports for a given file should be modified. |
| 10 | + |
| 11 | +- `unpatch`: A function describing how the module exports for a given file should be unpatched. This should generally mirror the logic in `patch`; for example, if `patch` wraps a method, `unpatch` should unwrap it. |
| 12 | + |
| 13 | +The core `PluginLoader` class is responsible for loading the instrumented plugins that use a patch mechanism to enable automatic tracing for specific target modules. In order to load new plugin, it should export `plugin` identifier. |
| 14 | +```typescript |
| 15 | +export const plugin = new HttpPlugin(...); |
| 16 | +``` |
| 17 | + |
| 18 | +> Example of simple module plugin created and used in the tests. |
| 19 | +https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-node-sdk/test/instrumentation/node_modules/%40opentelemetry/plugin-simple-module/simple-module.js |
| 20 | + |
| 21 | +After the plugin is created, it must be added in the [list of default supported plugins][DEFAULT_INSTRUMENTATION_PLUGINS]. |
| 22 | +```typescript |
| 23 | +export const DEFAULT_INSTRUMENTATION_PLUGINS: Plugins = { |
| 24 | + http: { |
| 25 | + enabled: true, |
| 26 | + path: '@opentelemetry/plugin-http', |
| 27 | + }, |
| 28 | + grpc: { |
| 29 | + enabled: true, |
| 30 | + path: '@opentelemetry/plugin-grpc', |
| 31 | + }, |
| 32 | + // [ADD NEW PLUGIN HERE] |
| 33 | + xxx: { |
| 34 | + enabled: true, |
| 35 | + // You may use a package name or absolute path to the file. |
| 36 | + path: '@opentelemetry/plugin-xxx', |
| 37 | + } |
| 38 | +}; |
| 39 | +``` |
| 40 | + |
| 41 | +We recommend using [`shimmer`][shimmer] to modify function properties on objects. |
| 42 | + |
| 43 | +Please refer to the [HTTP instrumentation][http-plugin] or [gRPC instrumentation][grpc-plugin] for more comprehensive examples. |
| 44 | + |
| 45 | + |
| 46 | +[shimmer]: https://github.com/othiym23/shimmer |
| 47 | +[builtin-plugins]: https://github.com/open-telemetry/opentelemetry-js#plugins |
| 48 | +[base-plugin]: https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts#L29 |
| 49 | +[http-plugin]: https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-plugin-http/src/http.ts#L44 |
| 50 | +[grpc-plugin]: https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-plugin-grpc/src/grpc.ts#L52 |
| 51 | +[DEFAULT_INSTRUMENTATION_PLUGINS]: https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-node-sdk/src/config.ts#L29 |
0 commit comments