@azure/monitor-opentelemetry 1.8.1 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -27,8 +27,8 @@ See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUP
27
27
 
28
28
  > _Important:_ `useAzureMonitor` must be called _before_ you import anything else. There may be resulting telemetry loss if other libraries are imported first.
29
29
 
30
- ```typescript
31
- import { useAzureMonitor, AzureMonitorOpenTelemetryOptions } from "@azure/monitor-opentelemetry";
30
+ ```ts snippet:ReadmeSampleUseAzureMonitor
31
+ import { AzureMonitorOpenTelemetryOptions, useAzureMonitor } from "@azure/monitor-opentelemetry";
32
32
 
33
33
  const options: AzureMonitorOpenTelemetryOptions = {
34
34
  azureMonitorExporterOptions: {
@@ -43,9 +43,9 @@ useAzureMonitor(options);
43
43
 
44
44
  ## Configuration
45
45
 
46
- ```typescript
47
- import { AzureMonitorOpenTelemetryOptions, useAzureMonitor } from "@azure/monitor-opentelemetry";
46
+ ```ts snippet:ReadmeSampleConfiguration
48
47
  import { Resource } from "@opentelemetry/resources";
48
+ import { AzureMonitorOpenTelemetryOptions, useAzureMonitor } from "@azure/monitor-opentelemetry";
49
49
 
50
50
  const resource = new Resource({ testAttribute: "testValue" });
51
51
  const options: AzureMonitorOpenTelemetryOptions = {
@@ -82,23 +82,88 @@ const options: AzureMonitorOpenTelemetryOptions = {
82
82
  logRecordProcessors: [],
83
83
  spanProcessors: [],
84
84
  };
85
-
86
85
  useAzureMonitor(options);
87
86
  ```
88
87
 
89
- |Property|Description|Default|
90
- | ------------------------------- |------------------------------------------------------------------------------------------------------------|-------|
91
- | azureMonitorExporterOptions | Azure Monitor OpenTelemetry Exporter Configuration. [More info here](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/monitor/monitor-opentelemetry-exporter) | | | |
92
- | samplingRatio | Sampling ratio must take a value in the range [0,1], 1 meaning all data will sampled and 0 all Tracing data will be sampled out. | 1|
93
- | instrumentationOptions| Allow configuration of OpenTelemetry Instrumentations. | {"http": { enabled: true },"azureSdk": { enabled: false },"mongoDb": { enabled: false },"mySql": { enabled: false },"postgreSql": { enabled: false },"redis": { enabled: false },"bunyan": { enabled: false }, "winston": { enabled: false } }|
94
- | browserSdkLoaderOptions| Allow configuration of Web Instrumentations. | { enabled: false, connectionString: "" } |
95
- | resource | Opentelemetry Resource. [More info here](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-resources) ||
96
- | samplingRatio | Sampling ratio must take a value in the range [0,1], 1 meaning all data will sampled and 0 all Tracing data will be sampled out. |1|
97
- | enableLiveMetrics | Enable/Disable Live Metrics. |true|
98
- | enableStandardMetrics | Enable/Disable Standard Metrics. |true|
99
- | logRecordProcessors | Array of log record processors to register to the global logger provider. ||
100
- | spanProcessors | Array of span processors to register to the global tracer provider. ||
101
- | enableTraceBasedSamplingForLogs | Enable log sampling based on trace. |false| |
88
+ <table>
89
+ <tr>
90
+ <th>Option</th>
91
+ <th>Description</th>
92
+ <th>Default</th>
93
+ </tr>
94
+ <tr>
95
+ <td><code>azureMonitorExporterOptions</code></td>
96
+ <td>Azure Monitor OpenTelemetry Exporter Configuration. <a href="https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/monitor/monitor-opentelemetry-exporter">More info here</a></td>
97
+ <td></td>
98
+ </tr>
99
+ <tr>
100
+ <td><code>samplingRatio</code></td>
101
+ <td>Sampling ratio must take a value in the range [0,1], 1 meaning all data will sampled and 0 all Tracing data will be sampled out.</td>
102
+ <td><code>1</code></td>
103
+ </tr>
104
+ <tr>
105
+ <td><code>instrumentationOptions</code></td>
106
+ <td>Instrumentation libraries configuration. <a href="#instrumentation-libraries">More info here</a></td>
107
+ <td>
108
+ <pre><code class="language-javascript">
109
+ {
110
+ http: { enabled: true },
111
+ azureSdk: { enabled: false },
112
+ mongoDb: { enabled: false },
113
+ mySql: { enabled: false },
114
+ postgreSql: { enabled: false },
115
+ redis: { enabled: false },
116
+ bunyan: { enabled: false },
117
+ winston: { enabled: false }
118
+ }
119
+ </code></pre>
120
+ </td>
121
+ </tr>
122
+ <tr>
123
+ <td><code>browserSdkLoaderOptions</code></td>
124
+ <td>Allow configuration of Web Instrumentations.</td>
125
+ <td>
126
+ <pre>
127
+ <code class="language-javascript">
128
+ {
129
+ enabled: false,
130
+ connectionString: ""
131
+ }
132
+ </code>
133
+ </pre>
134
+ </td>
135
+ </tr>
136
+ <tr>
137
+ <td><code>resource</code></td>
138
+ <td>Opentelemetry Resource. <a href="https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-resources">More info here</a></td>
139
+ <td></td>
140
+ </tr>
141
+ <tr>
142
+ <td><code>enableLiveMetrics</code></td>
143
+ <td>Enable/Disable Live Metrics.</td>
144
+ <td><code>true</code></td>
145
+ </tr>
146
+ <tr>
147
+ <td><code>enableStandardMetrics</code></td>
148
+ <td>Enable/Disable Standard Metrics. </td>
149
+ <td><code>true</code></td>
150
+ </tr>
151
+ <tr>
152
+ <td><code>logRecordProcessors</code></td>
153
+ <td>Array of log record processors to register to the global logger provider. </td>
154
+ <td></td>
155
+ </tr>
156
+ <tr>
157
+ <td><code>spanProcessors</code></td>
158
+ <td>Array of span processors to register to the global tracer provider. </td>
159
+ <td></td>
160
+ </tr>
161
+ <tr>
162
+ <td><code>enableTraceBasedSamplingForLogs</code></td>
163
+ <td>Enable log sampling based on trace.</td>
164
+ <td><code>false</code></td>
165
+ </tr>
166
+ </table>
102
167
 
103
168
  Options could be set using configuration file `applicationinsights.json` located under root folder of @azure/monitor-opentelemetry package installation folder, Ex: `node_modules/@azure/monitor-opentelemetry`. These configuration values will be applied to all AzureMonitorOpenTelemetryClient instances.
104
169
 
@@ -118,11 +183,8 @@ Options could be set using configuration file `applicationinsights.json` located
118
183
 
119
184
  Custom JSON file could be provided using `APPLICATIONINSIGHTS_CONFIGURATION_FILE` environment variable.
120
185
 
121
- ```javascript
122
- process.env.APPLICATIONINSIGHTS_CONFIGURATION_FILE =
123
- "C:/applicationinsights/config/customConfig.json";
124
-
125
- // Application Insights SDK setup....
186
+ ```ts snippet:ReadmeSampleCustomConfig
187
+ process.env["APPLICATIONINSIGHTS_CONFIGURATION_FILE"] = "path/to/customConfig.json";
126
188
  ```
127
189
 
128
190
  ## Instrumentation libraries
@@ -153,18 +215,17 @@ The following OpenTelemetry Instrumentation libraries are included as part of Az
153
215
 
154
216
  Other OpenTelemetry Instrumentations are available [here](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node) and could be added using TracerProvider in AzureMonitorOpenTelemetryClient.
155
217
 
156
- ```typescript
218
+ ```ts snippet:ReadmeSampleCustomInstrumentation
157
219
  import { useAzureMonitor } from "@azure/monitor-opentelemetry";
158
- import { metrics, trace } from "@opentelemetry/api";
159
220
  import { registerInstrumentations } from "@opentelemetry/instrumentation";
221
+ import { trace, metrics } from "@opentelemetry/api";
160
222
  import { ExpressInstrumentation } from "@opentelemetry/instrumentation-express";
161
223
 
162
224
  useAzureMonitor();
163
- const instrumentations = [new ExpressInstrumentation()];
164
225
  registerInstrumentations({
165
226
  tracerProvider: trace.getTracerProvider(),
166
227
  meterProvider: metrics.getMeterProvider(),
167
- instrumentations: instrumentations,
228
+ instrumentations: [new ExpressInstrumentation()],
168
229
  });
169
230
  ```
170
231
 
@@ -184,18 +245,22 @@ Further information on usage of the browser SDK loader can be found [here](https
184
245
 
185
246
  You might set the Cloud Role Name and the Cloud Role Instance via [OpenTelemetry Resource](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#resource-sdk) attributes.
186
247
 
187
- ```typescript
188
- import { useAzureMonitor, AzureMonitorOpenTelemetryOptions } from "@azure/monitor-opentelemetry";
248
+ ```ts snippet:ReadmeSampleSetRoleNameAndInstance
189
249
  import { Resource } from "@opentelemetry/resources";
190
- import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
250
+ import {
251
+ ATTR_SERVICE_NAME,
252
+ SEMRESATTRS_SERVICE_NAMESPACE,
253
+ SEMRESATTRS_SERVICE_INSTANCE_ID,
254
+ } from "@opentelemetry/semantic-conventions";
255
+ import { AzureMonitorOpenTelemetryOptions, useAzureMonitor } from "@azure/monitor-opentelemetry";
191
256
 
192
257
  // ----------------------------------------
193
258
  // Setting role name and role instance
194
259
  // ----------------------------------------
195
260
  const customResource = Resource.EMPTY;
196
- customResource.attributes[SemanticResourceAttributes.SERVICE_NAME] = "my-helloworld-service";
197
- customResource.attributes[SemanticResourceAttributes.SERVICE_NAMESPACE] = "my-namespace";
198
- customResource.attributes[SemanticResourceAttributes.SERVICE_INSTANCE_ID] = "my-instance";
261
+ customResource.attributes[ATTR_SERVICE_NAME] = "my-helloworld-service";
262
+ customResource.attributes[SEMRESATTRS_SERVICE_NAMESPACE] = "my-namespace";
263
+ customResource.attributes[SEMRESATTRS_SERVICE_INSTANCE_ID] = "my-instance";
199
264
 
200
265
  const options: AzureMonitorOpenTelemetryOptions = { resource: customResource };
201
266
  useAzureMonitor(options);
@@ -224,23 +289,24 @@ Any [attributes](#add-span-attributes) you add to spans are exported as custom p
224
289
 
225
290
  Use a custom processor:
226
291
 
227
- ```typescript
228
- import { useAzureMonitor, AzureMonitorOpenTelemetryOptions } from "@azure/monitor-opentelemetry";
229
- import { ReadableSpan, Span, SpanProcessor } from "@opentelemetry/sdk-trace-base";
230
- import { SemanticAttributes } from "@opentelemetry/semantic-conventions";
292
+ ```ts snippet:ReadmeSampleAddCustomProperty
293
+ import { SpanProcessor, ReadableSpan } from "@opentelemetry/sdk-trace-base";
294
+ import { Span } from "@opentelemetry/api";
295
+ import { SEMATTRS_HTTP_CLIENT_IP } from "@opentelemetry/semantic-conventions";
296
+ import { AzureMonitorOpenTelemetryOptions, useAzureMonitor } from "@azure/monitor-opentelemetry";
231
297
 
232
298
  class SpanEnrichingProcessor implements SpanProcessor {
233
- forceFlush(): Promise<void> {
234
- return Promise.resolve();
299
+ async forceFlush(): Promise<void> {
300
+ // Flush code here
235
301
  }
236
- shutdown(): Promise<void> {
237
- return Promise.resolve();
302
+ async shutdown(): Promise<void> {
303
+ // shutdown code here
238
304
  }
239
305
  onStart(_span: Span): void {}
240
- onEnd(span: ReadableSpan) {
306
+ onEnd(span: ReadableSpan): void {
241
307
  span.attributes["CustomDimension1"] = "value1";
242
308
  span.attributes["CustomDimension2"] = "value2";
243
- span.attributes[SemanticAttributes.HTTP_CLIENT_IP] = "<IP Address>";
309
+ span.attributes[SEMATTRS_HTTP_CLIENT_IP] = "<IP Address>";
244
310
  }
245
311
  }
246
312
 
@@ -249,6 +315,7 @@ const options: AzureMonitorOpenTelemetryOptions = {
249
315
  // Add the SpanEnrichingProcessor
250
316
  spanProcessors: [new SpanEnrichingProcessor()],
251
317
  };
318
+
252
319
  useAzureMonitor(options);
253
320
  ```
254
321
 
@@ -256,43 +323,42 @@ useAzureMonitor(options);
256
323
 
257
324
  Use a custom span processor and log record processor in order to attach and correlate operation name from requests to dependencies and logs.
258
325
 
259
- ```typescript
260
- import { useAzureMonitor, AzureMonitorOpenTelemetryOptions } from "@azure/monitor-opentelemetry";
261
- import { ReadableSpan, Span, SpanProcessor } from "@opentelemetry/sdk-trace-base";
262
- import { LogRecordProcessor } from "@opentelemetry/sdk-logs";
263
- import { SemanticAttributes } from "@opentelemetry/semantic-conventions";
326
+ ```ts snippet:ReadmeSampleAddOperationName
327
+ import { SpanProcessor, ReadableSpan } from "@opentelemetry/sdk-trace-base";
328
+ import { Span, Context, trace } from "@opentelemetry/api";
264
329
  import { AI_OPERATION_NAME } from "@azure/monitor-opentelemetry-exporter";
265
- import { Context, trace } from "@opentelemetry/api";
330
+ import { LogRecordProcessor, LogRecord } from "@opentelemetry/sdk-logs";
331
+ import { AzureMonitorOpenTelemetryOptions, useAzureMonitor } from "@azure/monitor-opentelemetry";
266
332
 
267
333
  class SpanEnrichingProcessor implements SpanProcessor {
268
- forceFlush(): Promise<void> {
269
- return Promise.resolve();
334
+ async forceFlush(): Promise<void> {
335
+ // Flush code here
270
336
  }
271
- shutdown(): Promise<void> {
272
- return Promise.resolve();
337
+ async shutdown(): Promise<void> {
338
+ // shutdown code here
273
339
  }
274
340
  onStart(_span: Span, _context: Context): void {
275
341
  const parentSpan = trace.getSpan(_context);
276
342
  if (parentSpan && "name" in parentSpan) {
277
343
  // If the parent span has a name we can assume it is a ReadableSpan and cast it.
278
- _span.attributes[AI_OPERATION_NAME] = (parentSpan as unknown as ReadableSpan).name;
344
+ _span.setAttribute(AI_OPERATION_NAME, (parentSpan as unknown as ReadableSpan).name);
279
345
  }
280
346
  }
281
- onEnd(span: ReadableSpan) {}
347
+ onEnd(_span: ReadableSpan): void {}
282
348
  }
283
349
 
284
350
  class LogRecordEnrichingProcessor implements LogRecordProcessor {
285
- forceFlush(): Promise<void> {
286
- return Promise.resolve();
351
+ async forceFlush(): Promise<void> {
352
+ // Flush code here
287
353
  }
288
- shutdown(): Promise<void> {
289
- return Promise.resolve();
354
+ async shutdown(): Promise<void> {
355
+ // shutdown code here
290
356
  }
291
- onEmit(_logRecord, _context): void {
357
+ onEmit(_logRecord: LogRecord, _context: Context): void {
292
358
  const parentSpan = trace.getSpan(_context);
293
359
  if (parentSpan && "name" in parentSpan) {
294
360
  // If the parent span has a name we can assume it is a ReadableSpan and cast it.
295
- _logRecord.attributes[AI_OPERATION_NAME] = (parentSpan as unknown as ReadableSpan).name;
361
+ _logRecord.setAttribute(AI_OPERATION_NAME, (parentSpan as unknown as ReadableSpan).name);
296
362
  }
297
363
  }
298
364
  }
@@ -303,6 +369,7 @@ const options: AzureMonitorOpenTelemetryOptions = {
303
369
  spanProcessors: [new SpanEnrichingProcessor()],
304
370
  logRecordProcessors: [new LogRecordEnrichingProcessor()],
305
371
  };
372
+
306
373
  useAzureMonitor(options);
307
374
  ```
308
375
 
@@ -314,14 +381,13 @@ You might use the following ways to filter out telemetry before it leaves your a
314
381
 
315
382
  The following example shows how to exclude a certain URL from being tracked by using the [HTTP/HTTPS instrumentation library](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation-http):
316
383
 
317
- ```typescript
384
+ ```ts snippet:ReadmeSampleExcludeUrl
385
+ import { HttpInstrumentationConfig } from "@opentelemetry/instrumentation-http";
386
+ import { IncomingMessage, RequestOptions } from "node:http";
318
387
  import {
319
- useAzureMonitor,
320
388
  AzureMonitorOpenTelemetryOptions,
389
+ useAzureMonitor,
321
390
  } from "@azure/monitor-opentelemetry";
322
- import { IncomingMessage } from "http";
323
- import { RequestOptions } from "https";
324
- import { HttpInstrumentationConfig } from "@opentelemetry/instrumentation-http";
325
391
 
326
392
  const httpInstrumentationConfig: HttpInstrumentationConfig = {
327
393
  enabled: true,
@@ -340,30 +406,38 @@ You might use the following ways to filter out telemetry before it leaves your a
340
406
  return false;
341
407
  },
342
408
  };
409
+
343
410
  const options: AzureMonitorOpenTelemetryOptions = {
344
411
  instrumentationOptions: {
345
412
  http: httpInstrumentationConfig,
346
413
  },
347
414
  };
415
+
348
416
  useAzureMonitor(options);
349
417
  ```
350
418
 
351
419
  1. Use a custom processor. You can use a custom span processor to exclude certain spans from being exported. To mark spans to not be exported, set `TraceFlag` to `DEFAULT`.
352
420
  Use the add [custom property example](#add-a-custom-property-to-a-trace), but replace the following lines of code:
353
421
 
354
- ```typescript
355
- ...
356
- import { SpanKind, TraceFlags } from "@opentelemetry/api";
357
- import { ReadableSpan, SpanProcessor } from "@opentelemetry/sdk-trace-base";
422
+ ```ts snippet:ReadmeSampleCustomProcessor
423
+ import { SpanProcessor, ReadableSpan } from "@opentelemetry/sdk-trace-base";
424
+ import { Span, Context, SpanKind, TraceFlags } from "@opentelemetry/api";
358
425
 
359
426
  class SpanEnrichingProcessor implements SpanProcessor {
360
- ...
361
-
362
- onEnd(span: ReadableSpan) {
363
- if(span.kind == SpanKind.INTERNAL){
364
- span.spanContext().traceFlags = TraceFlags.NONE;
365
- }
427
+ async forceFlush(): Promise<void> {
428
+ // Force flush code here
429
+ }
430
+ onStart(_span: Span, _parentContext: Context): void {
431
+ // Normal code here
432
+ }
433
+ async shutdown(): Promise<void> {
434
+ // Shutdown code here
435
+ }
436
+ onEnd(span: ReadableSpan): void {
437
+ if (span.kind === SpanKind.INTERNAL) {
438
+ span.spanContext().traceFlags = TraceFlags.NONE;
366
439
  }
440
+ }
367
441
  }
368
442
  ```
369
443
 
@@ -393,18 +467,18 @@ The following table shows the recommended aggregation types] for each of the Ope
393
467
  The [OpenTelemetry Specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#instrument)
394
468
  describes the instruments and provides examples of when you might use each one.
395
469
 
396
- ```typescript
470
+ ```ts snippet:ReadmeSampleCustomMetrics
397
471
  import { useAzureMonitor } from "@azure/monitor-opentelemetry";
398
- import { ObservableResult, metrics } from "@opentelemetry/api";
472
+ import { metrics, ObservableResult } from "@opentelemetry/api";
399
473
 
400
474
  useAzureMonitor();
401
475
  const meter = metrics.getMeter("testMeter");
402
476
 
403
- let histogram = meter.createHistogram("histogram");
404
- let counter = meter.createCounter("counter");
405
- let gauge = meter.createObservableGauge("gauge");
477
+ const histogram = meter.createHistogram("histogram");
478
+ const counter = meter.createCounter("counter");
479
+ const gauge = meter.createObservableGauge("gauge");
406
480
  gauge.addCallback((observableResult: ObservableResult) => {
407
- let randomNumber = Math.floor(Math.random() * 100);
481
+ const randomNumber = Math.floor(Math.random() * 100);
408
482
  observableResult.observe(randomNumber, { testKey: "testValue" });
409
483
  });
410
484
 
@@ -424,14 +498,14 @@ However, you may want to manually report exceptions beyond what instrumention li
424
498
  For instance, exceptions caught by your code are _not_ ordinarily not reported, and you may wish to report them
425
499
  and thus draw attention to them in relevant experiences including the failures blade and end-to-end transaction view.
426
500
 
427
- ```typescript
501
+ ```ts snippet:ReadmeSampleCustomExceptions
428
502
  import { useAzureMonitor } from "@azure/monitor-opentelemetry";
429
503
  import { trace, Exception } from "@opentelemetry/api";
430
504
 
431
505
  useAzureMonitor();
432
506
  const tracer = trace.getTracer("testMeter");
433
507
 
434
- let span = tracer.startSpan("hello");
508
+ const span = tracer.startSpan("hello");
435
509
  try {
436
510
  throw new Error("Test Error");
437
511
  } catch (error) {
@@ -445,13 +519,12 @@ try {
445
519
 
446
520
  Azure Monitor OpenTelemetry uses the OpenTelemetry API Logger for internal logs. To enable it, use the following code:
447
521
 
448
- ```typescript
522
+ ```ts snippet:ReadmeSampleSelfDiagnostics
449
523
  import { useAzureMonitor } from "@azure/monitor-opentelemetry";
450
- import { DiagLogLevel } from "@opentelemetry/api";
451
524
 
452
- process.env.APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL = "VERBOSE";
453
- process.env.APPLICATIONINSIGHTS_LOG_DESTINATION = "file";
454
- process.env.APPLICATIONINSIGHTS_LOGDIR = "C:/applicationinsights/logs";
525
+ process.env["APPLICATIONINSIGHTS_INSTRUMENTATION_LOGGING_LEVEL"] = "VERBOSE";
526
+ process.env["APPLICATIONINSIGHTS_LOG_DESTINATION"] = "file";
527
+ process.env["APPLICATIONINSIGHTS_LOGDIR"] = "path/to/logs";
455
528
 
456
529
  useAzureMonitor();
457
530
  ```
@@ -477,5 +550,3 @@ If you cannot your library in the registry, feel free to suggest a new plugin re
477
550
  ## Contributing
478
551
 
479
552
  If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code.
480
-
481
- ![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js/sdk/monitor/monitor-opentelemetry/README.png)