@@ -233,12 +233,21 @@ You can't extend the Java Distro with community instrumentation libraries. To re
233
233
Other OpenTelemetry Instrumentations are available [ here] ( https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node ) and could be added using TraceHandler in ApplicationInsightsClient.
234
234
235
235
``` javascript
236
- const { ApplicationInsightsClient , ApplicationInsightsConfig } = require (" applicationinsights" );
236
+ const { useAzureMonitor } = require (" @azure/monitor-opentelemetry" );
237
+ const { metrics , trace } = require (" @opentelemetry/api" );
238
+ const { registerInstrumentations } = require ( " @opentelemetry/instrumentation" );
237
239
const { ExpressInstrumentation } = require (' @opentelemetry/instrumentation-express' );
238
240
239
- const appInsights = new ApplicationInsightsClient (new ApplicationInsightsConfig ());
240
- const traceHandler = appInsights .getTraceHandler ();
241
- traceHandler .addInstrumentation (new ExpressInstrumentation ());
241
+ useAzureMonitor ();
242
+ const tracerProvider = trace .getTracerProvider ().getDelegate ();
243
+ const meterProvider = metrics .getMeterProvider ();
244
+ registerInstrumentations ({
245
+ instrumentations: [
246
+ new ExpressInstrumentation (),
247
+ ],
248
+ tracerProvider: tracerProvider,
249
+ meterProvider: meterProvider
250
+ });
242
251
```
243
252
244
253
### [ Python] ( #tab/python )
@@ -422,10 +431,11 @@ public class Program {
422
431
#### [ Node.js] ( #tab/nodejs )
423
432
424
433
``` javascript
425
- const { ApplicationInsightsClient , ApplicationInsightsConfig } = require (" applicationinsights" );
426
- const appInsights = new ApplicationInsightsClient (new ApplicationInsightsConfig ());
427
- const customMetricsHandler = appInsights .getMetricHandler ().getCustomMetricsHandler ();
428
- const meter = customMetricsHandler .getMeter ();
434
+ const { useAzureMonitor } = require (" @azure/monitor-opentelemetry" );
435
+ const { metrics } = require (" @opentelemetry/api" );
436
+
437
+ useAzureMonitor ();
438
+ const meter = metrics .getMeter (" testMeter" );
429
439
let histogram = meter .createHistogram (" histogram" );
430
440
histogram .record (1 , { " testKey" : " testValue" });
431
441
histogram .record (30 , { " testKey" : " testValue2" });
@@ -544,10 +554,11 @@ public class Program {
544
554
#### [ Node.js] ( #tab/nodejs )
545
555
546
556
``` javascript
547
- const { ApplicationInsightsClient , ApplicationInsightsConfig } = require (" applicationinsights" );
548
- const appInsights = new ApplicationInsightsClient (new ApplicationInsightsConfig ());
549
- const customMetricsHandler = appInsights .getMetricHandler ().getCustomMetricsHandler ();
550
- const meter = customMetricsHandler .getMeter ();
557
+ const { useAzureMonitor } = require (" @azure/monitor-opentelemetry" );
558
+ const { metrics } = require (" @opentelemetry/api" );
559
+
560
+ useAzureMonitor ();
561
+ const meter = metrics .getMeter (" testMeter" );
551
562
let counter = meter .createCounter (" counter" );
552
563
counter .add (1 , { " testKey" : " testValue" });
553
564
counter .add (5 , { " testKey2" : " testValue" });
@@ -667,10 +678,11 @@ public class Program {
667
678
#### [ Node.js] ( #tab/nodejs )
668
679
669
680
``` typescript
670
- const { ApplicationInsightsClient, ApplicationInsightsConfig } = require (" applicationinsights" );
671
- const appInsights = new ApplicationInsightsClient (new ApplicationInsightsConfig ());
672
- const customMetricsHandler = appInsights .getMetricHandler ().getCustomMetricsHandler ();
673
- const meter = customMetricsHandler .getMeter ();
681
+ const { useAzureMonitor } = require (" @azure/monitor-opentelemetry" );
682
+ const { metrics } = require (" @opentelemetry/api" );
683
+
684
+ useAzureMonitor ();
685
+ const meter = metrics .getMeter (" testMeter" );
674
686
let gauge = meter .createObservableGauge (" gauge" );
675
687
gauge .addCallback ((observableResult : ObservableResult ) => {
676
688
let randomNumber = Math .floor (Math .random () * 100 );
@@ -818,17 +830,18 @@ You can use `opentelemetry-api` to update the status of a span and record except
818
830
#### [ Node.js] ( #tab/nodejs )
819
831
820
832
``` javascript
821
- const { ApplicationInsightsClient , ApplicationInsightsConfig } = require (" applicationinsights" );
822
-
823
- const appInsights = new ApplicationInsightsClient (new ApplicationInsightsConfig ());
824
- const tracer = appInsights .getTraceHandler ().getTracer ();
825
- let span = tracer .startSpan (" hello" );
826
- try {
827
- throw new Error (" Test Error" );
828
- }
829
- catch (error){
830
- span .recordException (error);
831
- }
833
+ const { useAzureMonitor } = require (" @azure/monitor-opentelemetry" );
834
+ const { trace } = require (" @opentelemetry/api" );
835
+
836
+ useAzureMonitor ();
837
+ const tracer = trace .getTracer (" testTracer" );
838
+ let span = tracer .startSpan (" hello" );
839
+ try {
840
+ throw new Error (" Test Error" );
841
+ }
842
+ catch (error){
843
+ span .recordException (error);
844
+ }
832
845
```
833
846
834
847
#### [ Python] ( #tab/python )
@@ -1003,12 +1016,13 @@ you can add your spans by using the OpenTelemetry API.
1003
1016
#### [ Node.js] ( #tab/nodejs )
1004
1017
1005
1018
``` javascript
1006
- const { ApplicationInsightsClient , ApplicationInsightsConfig } = require (" applicationinsights" );
1019
+ const { useAzureMonitor } = require (" @azure/monitor-opentelemetry" );
1020
+ const { trace } = require (" @opentelemetry/api" );
1007
1021
1008
- const appInsights = new ApplicationInsightsClient ( new ApplicationInsightsConfig () );
1009
- const tracer = appInsights . getTraceHandler (). getTracer ();
1010
- let span = tracer .startSpan (" hello" );
1011
- span .end ();
1022
+ useAzureMonitor ( );
1023
+ const tracer = trace . getTracer (" testTracer " );
1024
+ let span = tracer .startSpan (" hello" );
1025
+ span .end ();
1012
1026
```
1013
1027
1014
1028
@@ -1183,47 +1197,47 @@ Not available in .NET.
1183
1197
#### [Node.js](#tab/nodejs)
1184
1198
1185
1199
1186
- First, get the `LogHandler`:
1200
+ You need to use `applicationinsights` v3 Beta package to achieve this. (https://www.npmjs.com/package/applicationinsights/v/beta)
1187
1201
1188
1202
```javascript
1189
- const { ApplicationInsightsClient, ApplicationInsightsConfig } = require("applicationinsights");
1190
- const appInsights = new ApplicationInsightsClient(new ApplicationInsightsConfig());
1191
- const logHandler = appInsights.getLogHandler ();
1203
+ const { TelemetryClient } = require("applicationinsights");
1204
+
1205
+ const appInsights = new TelemetryClient ();
1192
1206
```
1193
1207
1194
- Then use the ` LogHandler ` to send custom telemetry:
1208
+ Then use the ` TelemetryClient ` to send custom telemetry:
1195
1209
1196
1210
##### Events
1197
1211
1198
1212
``` javascript
1199
- let eventTelemetry = {
1200
- name: " testEvent"
1201
- };
1202
- logHandler .trackEvent (eventTelemetry);
1213
+ let eventTelemetry = {
1214
+ name: " testEvent"
1215
+ };
1216
+ appInsights .trackEvent (eventTelemetry);
1203
1217
```
1204
1218
1205
1219
##### Logs
1206
1220
1207
1221
``` javascript
1208
- let traceTelemetry = {
1209
- message: " testMessage" ,
1210
- severity: " Information"
1211
- };
1212
- logHandler .trackTrace (traceTelemetry);
1222
+ let traceTelemetry = {
1223
+ message: " testMessage" ,
1224
+ severity: " Information"
1225
+ };
1226
+ appInsights .trackTrace (traceTelemetry);
1213
1227
```
1214
1228
1215
1229
##### Exceptions
1216
1230
1217
1231
``` javascript
1218
- try {
1219
- ...
1220
- } catch (error) {
1221
- let exceptionTelemetry = {
1222
- exception: error,
1223
- severity: " Critical"
1224
- };
1225
- logHandler .trackException (exceptionTelemetry);
1226
- }
1232
+ try {
1233
+ ...
1234
+ } catch (error) {
1235
+ let exceptionTelemetry = {
1236
+ exception: error,
1237
+ severity: " Critical"
1238
+ };
1239
+ appInsights .trackException (exceptionTelemetry);
1240
+ }
1227
1241
```
1228
1242
1229
1243
#### [ Python] ( #tab/python )
@@ -1361,27 +1375,29 @@ Adding one or more span attributes populates the `customDimensions` field in the
1361
1375
##### [ Node.js] ( #tab/nodejs )
1362
1376
1363
1377
``` typescript
1364
- const { ApplicationInsightsClient, ApplicationInsightsConfig } = require (" applicationinsights" );
1365
- const { ReadableSpan, Span, SpanProcessor } = require (" @opentelemetry/sdk-trace-base" );
1366
- const { SemanticAttributes } = require (" @opentelemetry/semantic-conventions" );
1378
+ const { useAzureMonitor } = require (" @azure/monitor-opentelemetry" );
1379
+ const { trace } = require (" @opentelemetry/api" );
1380
+ const { ReadableSpan, Span, SpanProcessor } = require (" @opentelemetry/sdk-trace-base" );
1381
+ const { SemanticAttributes } = require (" @opentelemetry/semantic-conventions" );
1367
1382
1368
- const appInsights = new ApplicationInsightsClient (new ApplicationInsightsConfig ());
1383
+ useAzureMonitor ();
1384
+ const tracerProvider = trace .getTracerProvider ().getDelegate ();
1369
1385
1370
- class SpanEnrichingProcessor implements SpanProcessor {
1371
- forceFlush(): Promise <void >{
1372
- return Promise .resolve ();
1373
- }
1374
- shutdown(): Promise <void >{
1375
- return Promise .resolve ();
1376
- }
1377
- onStart(_span : Span ): void {}
1378
- onEnd(span : ReadableSpan ){
1379
- span .attributes [" CustomDimension1" ] = " value1" ;
1380
- span .attributes [" CustomDimension2" ] = " value2" ;
1386
+ class SpanEnrichingProcessor implements SpanProcessor {
1387
+ forceFlush(): Promise <void >{
1388
+ return Promise .resolve ();
1389
+ }
1390
+ shutdown(): Promise <void >{
1391
+ return Promise .resolve ();
1392
+ }
1393
+ onStart(_span : Span ): void {}
1394
+ onEnd(span : ReadableSpan ){
1395
+ span .attributes [" CustomDimension1" ] = " value1" ;
1396
+ span .attributes [" CustomDimension2" ] = " value2" ;
1397
+ }
1381
1398
}
1382
- }
1383
1399
1384
- appInsights . getTraceHandler () .addSpanProcessor (new SpanEnrichingProcessor ());
1400
+ tracerProvider .addSpanProcessor (new SpanEnrichingProcessor ());
1385
1401
```
1386
1402
1387
1403
##### [ Python] ( #tab/python )
@@ -1449,15 +1465,15 @@ Use the add [custom property example](#add-a-custom-property-to-a-span), but rep
1449
1465
1450
1466
``` typescript
1451
1467
...
1452
- const { SemanticAttributes } = require (" @opentelemetry/semantic-conventions" );
1468
+ const { SemanticAttributes } = require (" @opentelemetry/semantic-conventions" );
1453
1469
1454
- class SpanEnrichingProcessor implements SpanProcessor {
1455
- ...
1470
+ class SpanEnrichingProcessor implements SpanProcessor {
1471
+ ...
1456
1472
1457
- onEnd(span ){
1458
- span .attributes [SemanticAttributes .HTTP_CLIENT_IP ] = " <IP Address>" ;
1473
+ onEnd(span ){
1474
+ span .attributes [SemanticAttributes .HTTP_CLIENT_IP ] = " <IP Address>" ;
1475
+ }
1459
1476
}
1460
- }
1461
1477
```
1462
1478
1463
1479
##### [ Python] ( #tab/python )
@@ -1521,15 +1537,15 @@ Use the add [custom property example](#add-a-custom-property-to-a-span), but rep
1521
1537
1522
1538
``` typescript
1523
1539
...
1524
- import { SemanticAttributes } from " @opentelemetry/semantic-conventions" ;
1540
+ import { SemanticAttributes } from " @opentelemetry/semantic-conventions" ;
1525
1541
1526
- class SpanEnrichingProcessor implements SpanProcessor {
1527
- ...
1542
+ class SpanEnrichingProcessor implements SpanProcessor {
1543
+ ...
1528
1544
1529
- onEnd(span : ReadableSpan ){
1530
- span .attributes [SemanticAttributes .ENDUSER_ID ] = " <User ID>" ;
1545
+ onEnd(span : ReadableSpan ){
1546
+ span .attributes [SemanticAttributes .ENDUSER_ID ] = " <User ID>" ;
1547
+ }
1531
1548
}
1532
- }
1533
1549
```
1534
1550
1535
1551
##### [ Python] ( #tab/python )
@@ -1566,20 +1582,24 @@ Logback, Log4j, and java.util.logging are [autoinstrumented](#logs). Attaching c
1566
1582
1567
1583
Attributes could be added only when calling manual track APIs only. Log attributes for console, bunyan and Winston are currently not supported.
1568
1584
1569
- ``` javascript
1570
- const config = new ApplicationInsightsConfig ();
1571
- config .instrumentations .http = httpInstrumentationConfig;
1572
- const appInsights = new ApplicationInsightsClient (config);
1573
- const logHandler = appInsights .getLogHandler ();
1574
- const attributes = {
1575
- " testAttribute1" : " testValue1" ,
1576
- " testAttribute2" : " testValue2" ,
1577
- " testAttribute3" : " testValue3"
1578
- };
1579
- logHandler .trackEvent ({
1580
- name: " testEvent" ,
1581
- properties: attributes
1582
- });
1585
+ ``` typescript
1586
+ const { useAzureMonitor } = require (" @azure/monitor-opentelemetry" );
1587
+ const { logs } = require (" @opentelemetry/api-logs" );
1588
+
1589
+ useAzureMonitor ();
1590
+ const logger = logs .getLogger (" testLogger" );
1591
+ const logRecord = {
1592
+ body : " testEvent" ,
1593
+ attributes: {
1594
+ " testAttribute1" : " testValue1" ,
1595
+ " testAttribute2" : " testValue2" ,
1596
+ " testAttribute3" : " testValue3"
1597
+ }
1598
+ };
1599
+ logger .emit ({
1600
+ name: " testEvent" ,
1601
+ properties: attributes
1602
+ });
1583
1603
```
1584
1604
1585
1605
#### [ Python] ( #tab/python )
@@ -1687,7 +1707,7 @@ See [sampling overrides](java-standalone-config.md#sampling-overrides-preview) a
1687
1707
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):
1688
1708
1689
1709
```typescript
1690
- const { ApplicationInsightsClient , ApplicationInsightsConfig } = require (" applicationinsights " );
1710
+ const { useAzureMonitor , ApplicationInsightsOptions } = require (" @azure/monitor-opentelemetry " );
1691
1711
const { IncomingMessage } = require (" http" );
1692
1712
const { RequestOptions } = require (" https" );
1693
1713
const { HttpInstrumentationConfig }= require (" @opentelemetry/instrumentation-http" );
@@ -1709,9 +1729,14 @@ See [sampling overrides](java-standalone-config.md#sampling-overrides-preview) a
1709
1729
return false ;
1710
1730
}
1711
1731
};
1712
- const config = new ApplicationInsightsConfig ();
1713
- config .instrumentations .http = httpInstrumentationConfig ;
1714
- const appInsights = new ApplicationInsightsClient (config );
1732
+ const config : ApplicationInsightsOptions = {
1733
+ instrumentationOptions : {
1734
+ http : {
1735
+ httpInstrumentationConfig
1736
+ },
1737
+ },
1738
+ };
1739
+ useAzureMonitor (config );
1715
1740
```
1716
1741
1717
1742
2 . 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 `.
@@ -1903,8 +1928,8 @@ Get the request trace ID and the span ID in your code:
1903
1928
1904
1929
### [Node.js](#tab/nodejs)
1905
1930
1906
- - To review the source code , see the [Application Insights Beta GitHub repository ](https :// github.com/microsoft/ApplicationInsights-node. js/tree/beta ).
1907
- - To install the npm package and check for updates , see the [`applicationinsights ` npm Package ](https :// www.npmjs.com/package/applicationinsights/v/beta ) page.
1931
+ - To review the source code , see the [Azure Monitor OpenTelemetry GitHub repository ](https :// github.com/Azure/azure-sdk-for- js/tree/main/sdk/monitor/monitor-opentelemetry ).
1932
+ - To install the npm package and check for updates , see the [`@azure / monitor - opentelemetry ` npm Package ](https :// www.npmjs.com/package/@azure/monitor-opentelemetry ) page.
1908
1933
- To become more familiar with Azure Monitor Application Insights and OpenTelemetry , see the [Azure Monitor Example Application ](https :// github.com/Azure-Samples/azure-monitor-opentelemetry-node.js).
1909
1934
- To learn more about OpenTelemetry and its community , see the [OpenTelemetry JavaScript GitHub repository ](https :// github.com/open-telemetry/opentelemetry-js).
1910
1935
- To enable usage experiences , [enable web or browser user monitoring ](javascript .md ).
0 commit comments