Skip to content

Commit 945099c

Browse files
Merge pull request #251216 from hectorhdzg/hectorhdzg/addmodify
[Azure Monitor] Update add modify JS sample code
2 parents ada5546 + 42c63d3 commit 945099c

File tree

3 files changed

+132
-106
lines changed

3 files changed

+132
-106
lines changed

articles/azure-monitor/app/opentelemetry-add-modify.md

Lines changed: 129 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,21 @@ You can't extend the Java Distro with community instrumentation libraries. To re
233233
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.
234234

235235
```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");
237239
const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');
238240

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+
});
242251
```
243252

244253
### [Python](#tab/python)
@@ -422,10 +431,11 @@ public class Program {
422431
#### [Node.js](#tab/nodejs)
423432

424433
```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");
429439
let histogram = meter.createHistogram("histogram");
430440
histogram.record(1, { "testKey": "testValue" });
431441
histogram.record(30, { "testKey": "testValue2" });
@@ -544,10 +554,11 @@ public class Program {
544554
#### [Node.js](#tab/nodejs)
545555

546556
```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");
551562
let counter = meter.createCounter("counter");
552563
counter.add(1, { "testKey": "testValue" });
553564
counter.add(5, { "testKey2": "testValue" });
@@ -667,10 +678,11 @@ public class Program {
667678
#### [Node.js](#tab/nodejs)
668679

669680
```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");
674686
let gauge = meter.createObservableGauge("gauge");
675687
gauge.addCallback((observableResult: ObservableResult) => {
676688
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
818830
#### [Node.js](#tab/nodejs)
819831

820832
```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+
}
832845
```
833846

834847
#### [Python](#tab/python)
@@ -1003,12 +1016,13 @@ you can add your spans by using the OpenTelemetry API.
10031016
#### [Node.js](#tab/nodejs)
10041017

10051018
```javascript
1006-
const { ApplicationInsightsClient, ApplicationInsightsConfig } = require("applicationinsights");
1019+
const { useAzureMonitor } = require("@azure/monitor-opentelemetry");
1020+
const { trace } = require("@opentelemetry/api");
10071021

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();
10121026
```
10131027

10141028

@@ -1183,47 +1197,47 @@ Not available in .NET.
11831197
#### [Node.js](#tab/nodejs)
11841198

11851199

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)
11871201

11881202
```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();
11921206
```
11931207

1194-
Then use the `LogHandler` to send custom telemetry:
1208+
Then use the `TelemetryClient` to send custom telemetry:
11951209

11961210
##### Events
11971211

11981212
```javascript
1199-
let eventTelemetry = {
1200-
name: "testEvent"
1201-
};
1202-
logHandler.trackEvent(eventTelemetry);
1213+
let eventTelemetry = {
1214+
name: "testEvent"
1215+
};
1216+
appInsights.trackEvent(eventTelemetry);
12031217
```
12041218

12051219
##### Logs
12061220

12071221
```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);
12131227
```
12141228

12151229
##### Exceptions
12161230

12171231
```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+
}
12271241
```
12281242

12291243
#### [Python](#tab/python)
@@ -1361,27 +1375,29 @@ Adding one or more span attributes populates the `customDimensions` field in the
13611375
##### [Node.js](#tab/nodejs)
13621376

13631377
```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");
13671382

1368-
const appInsights = new ApplicationInsightsClient(new ApplicationInsightsConfig());
1383+
useAzureMonitor();
1384+
const tracerProvider = trace.getTracerProvider().getDelegate();
13691385

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+
}
13811398
}
1382-
}
13831399

1384-
appInsights.getTraceHandler().addSpanProcessor(new SpanEnrichingProcessor());
1400+
tracerProvider.addSpanProcessor(new SpanEnrichingProcessor());
13851401
```
13861402

13871403
##### [Python](#tab/python)
@@ -1449,15 +1465,15 @@ Use the add [custom property example](#add-a-custom-property-to-a-span), but rep
14491465

14501466
```typescript
14511467
...
1452-
const { SemanticAttributes } = require("@opentelemetry/semantic-conventions");
1468+
const { SemanticAttributes } = require("@opentelemetry/semantic-conventions");
14531469

1454-
class SpanEnrichingProcessor implements SpanProcessor{
1455-
...
1470+
class SpanEnrichingProcessor implements SpanProcessor{
1471+
...
14561472

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+
}
14591476
}
1460-
}
14611477
```
14621478

14631479
##### [Python](#tab/python)
@@ -1521,15 +1537,15 @@ Use the add [custom property example](#add-a-custom-property-to-a-span), but rep
15211537

15221538
```typescript
15231539
...
1524-
import { SemanticAttributes } from "@opentelemetry/semantic-conventions";
1540+
import { SemanticAttributes } from "@opentelemetry/semantic-conventions";
15251541

1526-
class SpanEnrichingProcessor implements SpanProcessor{
1527-
...
1542+
class SpanEnrichingProcessor implements SpanProcessor{
1543+
...
15281544

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+
}
15311548
}
1532-
}
15331549
```
15341550

15351551
##### [Python](#tab/python)
@@ -1566,20 +1582,24 @@ Logback, Log4j, and java.util.logging are [autoinstrumented](#logs). Attaching c
15661582

15671583
Attributes could be added only when calling manual track APIs only. Log attributes for console, bunyan and Winston are currently not supported.
15681584

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+
});
15831603
```
15841604

15851605
#### [Python](#tab/python)
@@ -1687,7 +1707,7 @@ See [sampling overrides](java-standalone-config.md#sampling-overrides-preview) a
16871707
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):
16881708
16891709
```typescript
1690-
const { ApplicationInsightsClient, ApplicationInsightsConfig } = require("applicationinsights");
1710+
const { useAzureMonitor, ApplicationInsightsOptions } = require("@azure/monitor-opentelemetry");
16911711
const { IncomingMessage } = require("http");
16921712
const { RequestOptions } = require("https");
16931713
const { HttpInstrumentationConfig }= require("@opentelemetry/instrumentation-http");
@@ -1709,9 +1729,14 @@ See [sampling overrides](java-standalone-config.md#sampling-overrides-preview) a
17091729
return false;
17101730
}
17111731
};
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);
17151740
```
17161741

17171742
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:
19031928
19041929
### [Node.js](#tab/nodejs)
19051930

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.
19081933
- 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).
19091934
- To learn more about OpenTelemetry and its community, see the [OpenTelemetry JavaScript GitHub repository](https://github.com/open-telemetry/opentelemetry-js).
19101935
- To enable usage experiences, [enable web or browser user monitoring](javascript.md).

articles/azure-monitor/app/opentelemetry-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ For more information about Java, see the [Java supplemental documentation](java-
637637
638638
```typescript
639639
const { useAzureMonitor, AzureMonitorOpenTelemetryOptions } = require("@azure/monitor-opentelemetry");
640+
const { trace } = require("@opentelemetry/api");
640641
const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-base');
641642
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
642643

articles/azure-monitor/app/opentelemetry-enable.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ As part of using Application Insights instrumentation, we collect and send diagn
312312
### [Node.js](#tab/nodejs)
313313

314314
- For details on adding and modifying Azure Monitor OpenTelemetry, see [Add and modify Azure Monitor OpenTelemetry](opentelemetry-add-modify.md)
315-
- To review the source code, see the [Application Insights Beta GitHub repository](https://github.com/microsoft/ApplicationInsights-node.js/tree/beta).
316-
- To install the npm package and check for updates, see the [`applicationinsights` npm Package](https://www.npmjs.com/package/applicationinsights/v/beta) page.
315+
- 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).
316+
- 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.
317317
- 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).
318318
- To learn more about OpenTelemetry and its community, see the [OpenTelemetry JavaScript GitHub repository](https://github.com/open-telemetry/opentelemetry-js).
319319
- To enable usage experiences, [enable web or browser user monitoring](javascript.md).

0 commit comments

Comments
 (0)