|
4 | 4 | [![devDependencies][devDependencies-image]][devDependencies-url] |
5 | 5 | [![Apache License][license-image]][license-image] |
6 | 6 |
|
7 | | -This module provides automatic tracing for Node.js applications. |
| 7 | +This module provides *automated instrumentation and tracing* for Node.js applications. |
8 | 8 |
|
9 | 9 | For manual instrumentation see the |
10 | 10 | [@opentelemetry/basic-tracer](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-basic-tracer) package. |
11 | 11 |
|
12 | | -## How does automatic tracing work? |
13 | | -> TODO: description |
| 12 | +## How does automated instrumentation work? |
| 13 | +node-sdk exposes a `NodeTracer` that will automatically hook into the module loader of Node.js. |
| 14 | + |
| 15 | +For this to work, please make sure that `NodeTracer` is initialized before any other module of your application, (like `http` or `express`) is loaded. |
| 16 | + |
| 17 | +OpenTelemetry comes with a growing number of instrumentation plugins for well know modules (see [supported modules](https://github.com/open-telemetry/opentelemetry-js#plugins)) and an API to create custom plugins (see [the plugin developer guide](https://github.com/open-telemetry/opentelemetry-js/blob/master/doc/plugin-guide.md)). |
| 18 | + |
| 19 | + |
| 20 | +Whenever a module is loaded `NodeTracer` will check if a matching instrumentation plugin has been installed. |
| 21 | + |
| 22 | +> **Please note:** This module does *not* bundle any plugins. They need to be installed separately. |
| 23 | +
|
| 24 | +If the respective plugin was found, it will be used to patch the original module to add instrumentation code. |
| 25 | +This is done by wrapping all tracing-relevant functions. |
| 26 | + |
| 27 | +This instrumentation code will automatically |
| 28 | +- extract a trace-context identifier from inbound requests to allow distributed tracing (if applicable) |
| 29 | +- make sure that this current trace-context is propagated while the transaction traverses an application (see [@opentelemetry/opentelemetry-scope-base](https://github.com/open-telemetry/opentelemetry-js/blob/master/packages/opentelemetry-scope-base/README.md) for an in-depth explanation) |
| 30 | +- add this trace-context identifier to outbound requests to allow continuing the distributed trace on the next hop (if applicable) |
| 31 | +- create and end spans |
| 32 | + |
| 33 | +In short, this means that this module will use provided plugins to automatically instrument your application to produce spans and provide end-to-end tracing by just adding a few lines of code. |
| 34 | + |
| 35 | +## Creating custom spans on top of auto-instrumentation |
| 36 | +Additionally to automated instrumentation, `NodeTracer` exposes the same API as [@opentelemetry/basic-tracer](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-basic-tracer), allowing creating custom spans if needed. |
14 | 37 |
|
15 | 38 | ## Installation |
16 | 39 |
|
17 | 40 | ```bash |
| 41 | +npm install --save @opentelemetry/core |
18 | 42 | npm install --save @opentelemetry/node-sdk |
| 43 | + |
| 44 | +# Install instrumentation plugins |
| 45 | +npm install --save @opentelemetry/plugin-http |
| 46 | +npm install --save @opentelemetry/plugin-grpc |
19 | 47 | ``` |
20 | 48 |
|
21 | 49 | ## Usage |
22 | 50 |
|
| 51 | +The following code will configure the `NodeTracer` to instrument `http` using `@opentelemetry/plugin-http`. |
| 52 | + |
23 | 53 | ```js |
24 | | -const opentelemetry = require('@opentelemetry/node-sdk'); |
| 54 | +const opentelemetry = require('@opentelemetry/core'); |
| 55 | +const { NodeTracer } = require('@opentelemetry/node-sdk'); |
25 | 56 |
|
26 | | -// TODO: DEMONSTRATE API |
| 57 | +// Create and configure NodeTracer |
| 58 | +const tracer = new NodeTracer({ |
| 59 | + plugins: { |
| 60 | + http: { |
| 61 | + enabled: true, |
| 62 | + // You may use a package name or absolute path to the file. |
| 63 | + path: '@opentelemetry/plugin-http', |
| 64 | + // http plugin options |
| 65 | + } |
| 66 | + } |
| 67 | +}); |
| 68 | + |
| 69 | +// Initialize the tracer |
| 70 | +opentelemetry.initGlobalTracer(tracer); |
| 71 | + |
| 72 | +// Your application code - http will automatically be instrumented if |
| 73 | +// @opentelemetry/plugin-http is present |
| 74 | +const http = require('http'); |
27 | 75 | ``` |
28 | 76 |
|
| 77 | +To enable instrumentation for all [supported modules](https://github.com/open-telemetry/opentelemetry-js#plugins), create an instance of `NodeTracer` without providing any plugin configuration to the constructor. |
| 78 | + |
| 79 | +```js |
| 80 | +const opentelemetry = require('@opentelemetry/core'); |
| 81 | +const { NodeTracer } = require('@opentelemetry/node-sdk'); |
| 82 | + |
| 83 | +// Create and initialize NodeTracer |
| 84 | +const tracer = new NodeTracer(); |
| 85 | + |
| 86 | +// Initialize the tracer |
| 87 | +opentelemetry.initGlobalTracer(tracer); |
| 88 | + |
| 89 | +// Your application code |
| 90 | +// ... |
| 91 | +``` |
| 92 | + |
| 93 | +## Examples |
| 94 | +See how to automatically instrument [http](https://github.com/open-telemetry/opentelemetry-js/tree/master/examples/http) and [gRPC](https://github.com/open-telemetry/opentelemetry-js/tree/master/examples/grpc) using node-sdk. |
| 95 | + |
| 96 | + |
29 | 97 | ## Useful links |
30 | 98 | - For more information on OpenTelemetry, visit: <https://opentelemetry.io/> |
31 | 99 | - For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js> |
|
0 commit comments