Skip to content

Commit 61ea2ad

Browse files
mayurkale22danielkhan
authored andcommitted
chore: add plugin developer guide (open-telemetry#348)
* chore: add plugin developer guide * chore: add a link to example plugin
1 parent a433b9f commit 61ea2ad

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ OpenTelemetry can collect tracing data automatically using plugins. Vendors/User
4141
- [@opentelemetry/plugin-http](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-http)
4242
- [@opentelemetry/plugin-grpc](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-plugin-grpc)
4343

44-
To request automatic tracing support for a module not on this list, please [file an issue](https://github.com/open-telemetry/opentelemetry-js/issues). Alternatively, you can write a plugin yourself.
44+
To request automatic tracing support for a module not on this list, please [file an issue](https://github.com/open-telemetry/opentelemetry-js/issues). Alternatively, you can [write a plugin yourself](https://github.com/open-telemetry/opentelemetry-js/blob/master/doc/plugin-guide.md).
4545

4646
### Shims
4747

doc/plugin-guide.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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

Comments
 (0)