Edit

Share via


Migrate from Application Insights SDKs to Azure Monitor OpenTelemetry

This guide provides step-by-step instructions to migrate applications from using Application Insights SDKs (Classic API) to Azure Monitor OpenTelemetry.

Expect a similar experience with Azure Monitor OpenTelemetry instrumentation as with the Application Insights SDKs. For more information and a feature-by-feature comparison, see release state of features.

Use Application Insights .NET software development kit (SDK) 3.x to upgrade from Application Insights .NET SDK 2.x to an OpenTelemetry (OTel)-based implementation. The 3.x SDK keeps most TelemetryClient and TelemetryConfiguration application programming interfaces (APIs) and uses the Azure Monitor OpenTelemetry Exporter to send telemetry to Application Insights.

If you build a new application or you already use the Azure Monitor OpenTelemetry Distro, use the Azure Monitor OpenTelemetry Distro instead. Don't use Application Insights .NET SDK 3.x and the Azure Monitor OpenTelemetry Distro in the same application.

Application Insights .NET SDK 3.x overview

Application Insights .NET SDK 3.x provides these NuGet packages:

  • Microsoft.ApplicationInsights for TelemetryClient and TelemetryConfiguration
  • Microsoft.ApplicationInsights.AspNetCore for ASP.NET (Active Server Pages .NET) Core web apps
  • Microsoft.ApplicationInsights.WorkerService for Worker Service and console apps
  • Microsoft.ApplicationInsights.Web for ASP.NET apps on .NET Framework
  • Microsoft.ApplicationInsights.NLogTarget for NLog integration (beta)

Use the repository documentation for code examples and OpenTelemetry integration details:

Upgrade to 3.x

Step 1: Remove references to incompatible packages

Remove these packages because they aren't compatible with SDK 3.x:

  • Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel
  • Microsoft.ApplicationInsights.DependencyCollector
  • Microsoft.ApplicationInsights.EventCounterCollector
  • Microsoft.ApplicationInsights.PerfCounterCollector
  • Microsoft.ApplicationInsights.WindowsServer
  • Microsoft.Extensions.Logging.ApplicationInsights
  • Microsoft.ApplicationInsights.Log4NetAppender
  • Microsoft.ApplicationInsights.TraceListener
  • Microsoft.ApplicationInsights.DiagnosticSourceListener
  • Microsoft.ApplicationInsights.EtwCollector
  • Microsoft.ApplicationInsights.EventSourceListener

SDK 3.x doesn't publish 3.x versions of these packages. Use the supported 3.x packages listed in Application Insights .NET SDK 3.x overview instead.

Step 2: Upgrade package versions to 3.x

Upgrade any remaining supported Application Insights packages to the latest 3.x version.

Important

Don't mix Application Insights 2.x and 3.x packages in the same application. Upgrade all Application Insights package references together.

Step 3: Update code and configuration for breaking changes

Review the breaking changes reference and remove or replace APIs and settings that are no longer supported.

The most common changes include:

  • Remove TrackPageView calls.
  • Update Track* calls to remove the custom metrics parameter.
  • Replace instrumentation key configuration with a full connection string by using TelemetryConfiguration.ConnectionString.
  • Replace TelemetryModule, TelemetryInitializer, and TelemetryProcessor customizations with OpenTelemetry processors, instrumentation libraries, and resource detectors. The ApplicationInsightsServiceOptions class includes EnableQuickPulseMetricStream, EnablePerformanceCounterCollectionModule, EnableDependencyTrackingTelemetryModule, and EnableRequestTrackingTelemetryModule. These ApplicationInsightsServiceOptions settings configure exporter behavior and don't use TelemetryModule implementations.
  • Replace adaptive sampling (EnableAdaptiveSampling) with TracesPerSecond or SamplingRatio.
  • Target .NET Framework 4.6.2 or later for ASP.NET apps that use Microsoft.ApplicationInsights.Web.

Replace removed extensibility points

Application Insights .NET SDK 2.x provides Application Insights-specific extensibility types such as telemetry modules, initializers, and processors. Application Insights .NET SDK 3.x uses OpenTelemetry extensibility instead.

  • Use OpenTelemetry instrumentation and configuration options to control automatic collection.
  • Use OpenTelemetry processors to enrich or filter telemetry.

SDK 3.x keeps only a subset of TelemetryContext properties. You can set these properties on individual telemetry items:

Context Properties
User Id, AuthenticatedUserId, UserAgent
Operation Name
Location Ip
GlobalProperties (dictionary)

Configure sampling

Application Insights .NET SDK 3.x supports two sampling modes for traces (requests and dependencies):

  • Set SamplingRatio (0.0 to 1.0) for percentage-based sampling.
  • Set TracesPerSecond for rate-limited sampling (default: Five traces per second).

SDK 3.x applies the same sampling settings to requests and dependencies. SDK 3.x doesn't support separate sampling settings for requests and dependencies.

When a request or dependency is sampled in, SDK 3.x applies the sampling decision of the parent trace to related logs by default. To disable that behavior, set EnableTraceBasedLogsSampler to false.

You can set SamplingRatio, TracesPerSecond, and EnableTraceBasedLogsSampler in TelemetryConfiguration, appsettings.json, or applicationinsights.config.

Troubleshoot an upgrade

Use these steps to validate telemetry during an upgrade to SDK 3.x:

  • Collect Application Insights self-diagnostics logs to identify configuration errors and exporter failures.
  • Add the OpenTelemetry console exporter to verify that traces, metrics, and logs emit as expected before you rely on Azure Monitor ingestion.
  • Confirm that sampling settings behave as expected by validating parent-child trace decisions.
  • Validate resource attributes such as service name, role name, and environment to ensure correct attribution in Application Insights.

For detailed troubleshooting guidance and examples, use the following resources: