-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathlibraries.md
299 lines (225 loc) · 8.31 KB
/
libraries.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
---
title: Using instrumentation libraries
linkTitle: Libraries
weight: 40
description: How to instrument libraries an app depends on
cSpell:ignore: autoinstrumentation metapackage metapackages
---
{{% docs/languages/libraries-intro JavaScript %}}
## Use Instrumentation Libraries
If a library does not come with OpenTelemetry out of the box, you can use
[instrumentation libraries](/docs/specs/otel/glossary/#instrumentation-library)
in order to generate telemetry data for a library or framework.
For example,
[the instrumentation library for Express](https://www.npmjs.com/package/@opentelemetry/instrumentation-express)
will automatically create [spans](/docs/concepts/signals/traces/#spans) based on
the inbound HTTP requests.
### Setup
Each instrumentation library is an NPM package. For example, here’s how you can
install the
[instrumentation-express](https://www.npmjs.com/package/@opentelemetry/instrumentation-express)
and
[instrumentation-http](https://www.npmjs.com/package/@opentelemetry/instrumentation-http)
instrumentation libraries to instrument inbound and outbound HTTP traffic:
```sh
npm install --save @opentelemetry/instrumentation-http @opentelemetry/instrumentation-express
```
OpenTelemetry JavaScript also defines metapackages
[auto-instrumentation-node](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-node)
and
[auto-instrumentation-web](https://www.npmjs.com/package/@opentelemetry/auto-instrumentations-web),
that bundle all Node.js- or web-based instrumentation libraries into a single
package. It’s a convenient way to add automatically-generated telemetry for all
your libraries with minimal effort:
{{< tabpane text=true >}}
{{% tab Node.js %}}
```shell
npm install --save @opentelemetry/auto-instrumentations-node
```
{{% /tab %}}
{{% tab Browser %}}
```shell
npm install --save @opentelemetry/auto-instrumentations-web
```
{{% /tab %}} {{< /tabpane >}}
Note, that using those metapackages increases your dependency graph size. Use
individual instrumentation packages if you know exactly which ones you need.
### Registration
After installing the instrumentation libraries you need, register them with the
OpenTelemetry SDK for Node.js. If you followed the
[Getting Started](/docs/languages/js/getting-started/nodejs/) you already use
the metapackages. If you followed the instructions
[to initialize the SDK for manual instrumentation](/docs/languages/js/instrumentation/#initialize-tracing),
update your `instrumentation.ts` (or `instrumentation.js`) as follows:
{{< tabpane text=true >}}
{{% tab TypeScript %}}
```typescript
/*instrumentation.ts*/
...
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
const sdk = new NodeSDK({
...
// This registers all instrumentation packages
instrumentations: [getNodeAutoInstrumentations()]
});
sdk.start()
```
{{% /tab %}}
{{% tab JavaScript %}}
```javascript
/*instrumentation.js*/
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const sdk = new NodeSDK({
...
// This registers all instrumentation packages
instrumentations: [getNodeAutoInstrumentations()]
});
```
{{% /tab %}}
{{< /tabpane >}}
To disable individual instrumentation libraries you can apply the following
change:
{{< tabpane text=true >}}
{{% tab TypeScript %}}
```typescript
/*instrumentation.ts*/
...
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
const sdk = new NodeSDK({
...
// This registers all instrumentation packages
instrumentations: [
getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-fs': {
enabled: false,
},
}),
],
});
sdk.start()
```
{{% /tab %}}
{{% tab JavaScript %}}
```javascript
/*instrumentation.js*/
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const sdk = new NodeSDK({
...
// This registers all instrumentation packages
instrumentations: [
getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-fs': {
enabled: false,
},
}),
],
});
```
{{% /tab %}}
{{< /tabpane >}}
To only load individual instrumentation libraries, replace
`[getNodeAutoInstrumentations()]` with the list of those you need:
{{< tabpane text=true >}}
{{% tab TypeScript %}}
```typescript
/*instrumentation.ts*/
...
import { HttpInstrumentation } from "@opentelemetry/instrumentation-http";
import { ExpressInstrumentation } from "@opentelemetry/instrumentation-express";
const sdk = new NodeSDK({
...
instrumentations: [
// Express instrumentation expects HTTP layer to be instrumented
new HttpInstrumentation(),
new ExpressInstrumentation(),
]
});
sdk.start()
```
{{% /tab %}} {{% tab JavaScript %}}
```javascript
/*instrumentation.js*/
const { HttpInstrumentation } = require("@opentelemetry/instrumentation-http");
const { ExpressInstrumentation } = require("@opentelemetry/instrumentation-express");
const sdk = new NodeSDK({
...
instrumentations: [
// Express instrumentation expects HTTP layer to be instrumented
new HttpInstrumentation(),
new ExpressInstrumentation(),
]
});
```
{{% /tab %}}
{{< /tabpane >}}
### Configuration
Some instrumentation libraries offer additional configuration options.
For example,
[Express instrumentation](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-express#express-instrumentation-options)
offers ways to ignore specified middleware or enrich spans created automatically
with a request hook:
{{< tabpane text=true >}}
{{% tab TypeScript %}}
```typescript
import { Span } from '@opentelemetry/api';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import {
ExpressInstrumentation,
ExpressLayerType,
ExpressRequestInfo,
} from '@opentelemetry/instrumentation-express';
const expressInstrumentation = new ExpressInstrumentation({
requestHook: function (span: Span, info: ExpressRequestInfo) {
if (info.layerType === ExpressLayerType.REQUEST_HANDLER) {
span.setAttribute(SemanticAttributes.HTTP_METHOD, info.request.method);
span.setAttribute(SemanticAttributes.HTTP_URL, info.request.baseUrl);
}
},
});
```
{{% /tab %}}
{{% tab JavaScript %}}
```javascript
/*instrumentation.js*/
const { SemanticAttributes } = require('@opentelemetry/semantic-conventions');
const {
ExpressInstrumentation,
ExpressLayerType,
} = require('@opentelemetry/instrumentation-express');
const expressInstrumentation = new ExpressInstrumentation({
requestHook: function (span, info) {
if (info.layerType === ExpressLayerType.REQUEST_HANDLER) {
span.setAttribute(SemanticAttributes.HTTP_METHOD, info.request.method);
span.setAttribute(SemanticAttributes.HTTP_URL, info.request.baseUrl);
}
},
});
```
{{% /tab %}}
{{< /tabpane >}}
You'll need to refer to each instrumentation library's documentation for
advanced configuration.
### Available instrumentation libraries
You can find a list of available instrumentation in the
[registry](/ecosystem/registry/?language=js&component=instrumentation).
## Instrument a library natively
If you want to add native instrumentation to your library, you should review the
following documentation:
- The concept page [Libraries](/docs/concepts/instrumentation/libraries/)
provides you with insights on when to instrument and what to instrument
- The [manual instrumentation](/docs/languages/js/instrumentation/) provides you
with the required code examples to create traces, metrics and logs for your
library
- The
[Instrumentation Implementation Guide](https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/GUIDELINES.md)
for Node.js and browser contains JavaScript specific best practices for
creating library instrumentation.
## Create an instrumentation library
While having out of the box observability for an application is the preferred
way, this is not always possible or desired. In those cases, you can create an
instrumentation library, which would inject instrumentation calls, using
mechanisms such as wrapping interfaces, subscribing to library-specific
callbacks, or translating existing telemetry into the OpenTelemetry model.
To create such a library follow the
[Instrumentation Implementation Guide](https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/GUIDELINES.md)
for Node.js and browser.