Skip to content

Commit 33e6f71

Browse files
svrnmvmarchaud
andauthored
docs: fix examples in website_docs/instrumentation.md (#2412)
Co-authored-by: Valentin Marchaud <[email protected]>
1 parent 5b4ca1c commit 33e6f71

File tree

2 files changed

+26
-43
lines changed

2 files changed

+26
-43
lines changed

website_docs/getting_started/nodejs.md

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ The following dependencies are required to trace a Node.js application.
7272

7373
These dependencies are required to configure the tracing SDK and create spans.
7474

75-
- `@opentelemetry/api`
76-
- `@opentelemetry/sdk-trace-node`
77-
- `@opentelemetry/sdk-trace-base`
75+
```shell
76+
npm install @opentelemetry/sdk-node @opentelemetry/api
77+
```
7878

7979
#### Exporter
8080

@@ -91,6 +91,10 @@ Many common modules such as the `http` standard library module, `express`, and o
9191

9292
You can also install all instrumentations maintained by the OpenTelemetry authors by using the `@opentelemetry/auto-instrumentations-node` module.
9393

94+
```shell
95+
npm install @opentelemetry/auto-instrumentations-node
96+
```
97+
9498
### Setup
9599

96100
The tracing setup and configuration should be run before your application code. One tool commonly used for this task is the [`-r, --require module`](https://nodejs.org/api/cli.html#cli_r_require_module) flag.
@@ -101,47 +105,27 @@ Create a file with a name like `tracing.js` which will contain your tracing setu
101105
/* tracing.js */
102106

103107
// Require dependencies
104-
const { NodeTracerProvider } = require("@opentelemetry/sdk-trace-node");
105-
const { SimpleSpanProcessor, ConsoleSpanExporter } = require("@opentelemetry/sdk-trace-base");
106-
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node');
107-
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
108-
109-
// Create a tracer provider
110-
const provider = new NodeTracerProvider();
111-
112-
// The exporter handles sending spans to your tracing backend
113-
const exporter = new ConsoleSpanExporter();
114-
115-
// The simple span processor sends spans to the exporter as soon as they are ended.
116-
const processor = new SimpleSpanProcessor(exporter);
117-
provider.addSpanProcessor(processor);
108+
const opentelemetry = require("@opentelemetry/sdk-node");
109+
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");
118110

119-
// The provider must be registered in order to
120-
// be used by the OpenTelemetry API and instrumentations
121-
provider.register();
122-
123-
// This will automatically enable all instrumentations
124-
registerInstrumentations({
125-
instrumentations: [getNodeAutoInstrumentations()],
111+
const sdk = new opentelemetry.NodeSDK({
112+
traceExporter: new opentelemetry.tracing.ConsoleSpanExporter(),
113+
instrumentations: [getNodeAutoInstrumentations()]
126114
});
115+
116+
sdk.start()
127117
```
128118

129119
### Run Application
130120

131-
First, install the dependencies as described above. Here you need to add the following:
132-
133-
```shell
134-
npm install --save @opentelemetry/sdk-trace-node @opentelemetry/auto-instrumentations-node
135-
```
136-
137121
Now you can run your application as you normally would, but you can use the `--require` flag to load the tracing code before the application code.
138122

139123
```shell
140124
$ node --require './tracing.js' app.js
141125
Listening for requests on http://localhost:8080
142126
```
143127

144-
Now, when you open <http://localhost:8080> in your web browser, you should see the spans printed in the console by the `ConsoleSpanExporter`.
128+
Open <http://localhost:8080> in your web browser and reload the page a few times, after a while you should see the spans printed in the console by the `ConsoleSpanExporter`.
145129

146130
<details>
147131
<summary>View example output</summary>

website_docs/instrumentation.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,17 @@ for (let i = 0; i < 10; i += 1) {
108108
function doWork(parent) {
109109
// Start another span. In this example, the main method already started a
110110
// span, so that'll be the parent span, and this will be a child span.
111-
const span = tracer.startSpan('doWork', {
112-
parent,
113-
});
111+
const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), parent);
112+
const span = tracer.startSpan('doWork', undefined, ctx);
114113

115114
// simulate some random work.
116115
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
117116
// empty
118117
}
119118
span.end();
120119
}
120+
// Be sure to end the span.
121+
parentSpan.end();
121122
```
122123

123124
Invoking your application once again will give you a list of traces being exported.
@@ -128,9 +129,8 @@ Attributes can be used to describe your spans. Attributes can be added to a span
128129

129130
```javascript
130131
function doWork(parent) {
131-
const span = tracer.startSpan('doWork', {
132-
parent, attributes: { attribute1 : 'value1' }
133-
});
132+
const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), parent);
133+
const span = tracer.startSpan('doWork', { attributes: { attribute1 : 'value1' } }, ctx);
134134
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
135135
// empty
136136
}
@@ -159,9 +159,8 @@ Finally, you can update your file to include semantic attributes:
159159

160160
```javascript
161161
function doWork(parent) {
162-
const span = tracer.startSpan('doWork', {
163-
parent, attributes: { SemanticAttributes.CODE_FUNCTION : 'doWork' }
164-
});
162+
const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), parent);
163+
const span = tracer.startSpan('doWork', { attributes: { [SemanticAttributes.CODE_FUNCTION] : 'doWork' } }, ctx);
165164
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
166165
// empty
167166
}
@@ -178,9 +177,9 @@ The status can be set at any time before the span is finished:
178177

179178
```javascript
180179
function doWork(parent) {
181-
const span = tracer.startSpan('doWork', {
182-
parent,
183-
});
180+
const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), parent);
181+
const span = tracer.startSpan('doWork', undefined, ctx);
182+
184183
span.setStatus({
185184
code: opentelemetry.SpanStatusCode.OK,
186185
message: 'Ok.'

0 commit comments

Comments
 (0)