Skip to content

Commit fe53994

Browse files
authored
docs(node-sdk): extend README (open-telemetry#367)
1 parent 734da94 commit fe53994

File tree

1 file changed

+73
-5
lines changed

1 file changed

+73
-5
lines changed

packages/opentelemetry-node-sdk/README.md

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,96 @@
44
[![devDependencies][devDependencies-image]][devDependencies-url]
55
[![Apache License][license-image]][license-image]
66

7-
This module provides automatic tracing for Node.js applications.
7+
This module provides *automated instrumentation and tracing* for Node.js applications.
88

99
For manual instrumentation see the
1010
[@opentelemetry/basic-tracer](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-basic-tracer) package.
1111

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.
1437

1538
## Installation
1639

1740
```bash
41+
npm install --save @opentelemetry/core
1842
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
1947
```
2048

2149
## Usage
2250

51+
The following code will configure the `NodeTracer` to instrument `http` using `@opentelemetry/plugin-http`.
52+
2353
```js
24-
const opentelemetry = require('@opentelemetry/node-sdk');
54+
const opentelemetry = require('@opentelemetry/core');
55+
const { NodeTracer } = require('@opentelemetry/node-sdk');
2556

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');
2775
```
2876

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+
2997
## Useful links
3098
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
3199
- For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js>

0 commit comments

Comments
 (0)