|
| 1 | +--- |
| 2 | +title: Migrating Azure Monitor Application Insights Node.js from Application Insights SDK 2.X to OpenTelemetry |
| 3 | +description: This article provides guidance on how to migrate from the Azure Monitor Application Insights Node.js SDK 2.X to OpenTelemetry. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.date: 04/16/2024 |
| 6 | +ms.devlang: javascript |
| 7 | +ms.custom: devx-track-js |
| 8 | +ms.reviewer: mmcc |
| 9 | +--- |
| 10 | + |
| 11 | +# Migrate from the Node.js Application Insights SDK 2.X to Azure Monitor OpenTelemetry |
| 12 | + |
| 13 | +This guide provides two options to upgrade from the Azure Monitor Application Insights Node.js SDK 2.X to OpenTelemetry. |
| 14 | + |
| 15 | +* **Clean install** the [Node.js Azure Monitor OpenTelemetry Distro](https://github.com/microsoft/opentelemetry-azure-monitor-js). |
| 16 | + * Remove dependencies on the Application Insights classic API. |
| 17 | + * Familiarize yourself with OpenTelemetry APIs and terms. |
| 18 | + * Position yourself to use all that OpenTelemetry offers now and in the future. |
| 19 | +* **Upgrade** to Node.js SDK 3.X. |
| 20 | + * Postpone code changes while preserving compatibility with existing custom events and metrics. |
| 21 | + * Access richer OpenTelemetry instrumentation libraries. |
| 22 | + * Maintain eligibility for the latest bug and security fixes. |
| 23 | + |
| 24 | +## [Clean install](#tab/cleaninstall) |
| 25 | + |
| 26 | +1. Gain prerequisite knowledge of the OpenTelemetry JavaScript Application Programming Interface (API) and Software Development Kit (SDK). |
| 27 | + |
| 28 | + * Read [OpenTelemetry JavaScript documentation](https://opentelemetry.io/docs/languages/js/). |
| 29 | + * Review [Configure Azure Monitor OpenTelemetry](opentelemetry-configuration.md?tabs=nodejs). |
| 30 | + * Evaluate [Add, modify, and filter OpenTelemetry](opentelemetry-add-modify.md?tabs=nodejs). |
| 31 | + |
| 32 | +2. Uninstall the `applicationinsights` dependency from your project. |
| 33 | + |
| 34 | + ```shell |
| 35 | + npm uninstall applicationinsights |
| 36 | + ``` |
| 37 | + |
| 38 | +3. Remove SDK 2.X implementation from your code. |
| 39 | + |
| 40 | + Remove all Application Insights instrumentation from your code. Delete any sections where the Application Insights client is initialized, modified, or called. |
| 41 | + |
| 42 | +4. Enable Application Insights with the Azure Monitor OpenTelemetry Distro. |
| 43 | + |
| 44 | + Follow [getting started](opentelemetry-enable.md?tabs=nodejs) to onboard to the Azure Monitor OpenTelemetry Distro. |
| 45 | + |
| 46 | +#### Azure Monitor OpenTelemetry Distro changes and limitations |
| 47 | + |
| 48 | +The APIs from the Application Insights SDK 2.X aren't available in the Azure Monitor OpenTelemetry Distro. You can access these APIs through a nonbreaking upgrade path in the Application Insights SDK 3.X. |
| 49 | +
|
| 50 | +## [Upgrade](#tab/upgrade) |
| 51 | +
|
| 52 | +1. Upgrade the `applicationinsights` package dependency. |
| 53 | +
|
| 54 | + ```shell |
| 55 | + npm update applicationinsights |
| 56 | + ``` |
| 57 | +
|
| 58 | +2. Rebuild your application. |
| 59 | +
|
| 60 | +3. Test your application. |
| 61 | +
|
| 62 | + To avoid using unsupported configuration options in the Application Insights SDK 3.X, see [Unsupported Properties](https://github.com/microsoft/ApplicationInsights-node.js/tree/beta?tab=readme-ov-file#applicationinsights-shim-unsupported-properties). |
| 63 | +
|
| 64 | + If the SDK logs warnings about unsupported API usage after a major version bump, and you need the related functionality, continue using the Application Insights SDK 2.X. |
| 65 | +
|
| 66 | +--- |
| 67 | +
|
| 68 | +## Changes and limitations |
| 69 | +
|
| 70 | +The following changes and limitations apply to both upgrade paths. |
| 71 | +
|
| 72 | +##### Node < 14 support |
| 73 | +
|
| 74 | +OpenTelemetry JavaScript's monitoring solutions officially support only Node version 14+. Check the [OpenTelemetry supported runtimes](https://github.com/open-telemetry/opentelemetry-js#supported-runtimes) for the latest updates. Users on older versions like Node 8, previously supported by the ApplicationInsights SDK, can still use OpenTelemetry solutions but can experience unexpected or breaking behavior. |
| 75 | + |
| 76 | +##### Configuration options |
| 77 | + |
| 78 | +The Application Insights SDK version 2.X offers configuration options that aren't available in the Azure Monitor OpenTelemetry Distro or in the major version upgrade to Application Insights SDK 3.X. To find these changes, along with the options we still support, see [SDK configuration documentation](https://github.com/microsoft/ApplicationInsights-node.js/tree/beta?tab=readme-ov-file#applicationinsights-shim-unsupported-properties). |
| 79 | +
|
| 80 | +##### Extended metrics |
| 81 | +
|
| 82 | +Extended metrics are supported in the Application Insights SDK 2.X; however, support for these metrics ends in both version 3.X of the ApplicationInsights SDK and the Azure Monitor OpenTelemetry Distro. |
| 83 | +
|
| 84 | +##### Telemetry Processors |
| 85 | +
|
| 86 | +While the Azure Monitor OpenTelemetry Distro and Application Insights SDK 3.X don't support TelemetryProcessors, they do allow you to pass span and log record processors. For more information on how, see [Azure Monitor OpenTelemetry Distro project](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/monitor/monitor-opentelemetry#modify-telemetry). |
| 87 | + |
| 88 | +This example shows the equivalent of creating and applying a telemetry processor that attaches a custom property in the Application Insights SDK 2.X. |
| 89 | + |
| 90 | +```typescript |
| 91 | +const applicationInsights = require("applicationinsights"); |
| 92 | +applicationInsights.setup("YOUR_CONNECTION_STRING"); |
| 93 | +applicationInsights.defaultClient.addTelemetryProcessor(addCustomProperty); |
| 94 | +applicationInsights.start(); |
| 95 | +
|
| 96 | +function addCustomProperty(envelope: EnvelopeTelemetry) { |
| 97 | + const data = envelope.data.baseData; |
| 98 | + if (data?.properties) { |
| 99 | + data.properties.customProperty = "Custom Property Value"; |
| 100 | + } |
| 101 | + return true; |
| 102 | +} |
| 103 | +``` |
| 104 | +
|
| 105 | +This example shows how to modify an Azure Monitor OpenTelemetry Distro implementation to pass a SpanProcessor to the configuration of the distro. |
| 106 | +
|
| 107 | +```typescript |
| 108 | +import { Context, Span} from "@opentelemetry/api"; |
| 109 | +import { ReadableSpan, SpanProcessor } from "@opentelemetry/sdk-trace-base"; |
| 110 | +const { useAzureMonitor } = require("@azure/monitor-opentelemetry"); |
| 111 | +
|
| 112 | +class SpanEnrichingProcessor implements SpanProcessor { |
| 113 | + forceFlush(): Promise<void> { |
| 114 | + return Promise.resolve(); |
| 115 | + } |
| 116 | + onStart(span: Span, parentContext: Context): void { |
| 117 | + return; |
| 118 | + } |
| 119 | + onEnd(span: ReadableSpan): void { |
| 120 | + span.attributes["custom-attribute"] = "custom-value"; |
| 121 | + } |
| 122 | + shutdown(): Promise<void> { |
| 123 | + return Promise.resolve(); |
| 124 | + } |
| 125 | +} |
| 126 | +
|
| 127 | +const options = { |
| 128 | + azureMonitorExporterOptions: { |
| 129 | + connectionString: "YOUR_CONNECTION_STRING" |
| 130 | + }, |
| 131 | + spanProcessors: [new SpanEnrichingProcessor()], |
| 132 | +}; |
| 133 | +useAzureMonitor(options); |
| 134 | +``` |
0 commit comments