Edit

Share via


Migrate from .NET Application Insights SDKs to Azure Monitor OpenTelemetry

This guide provides step-by-step instructions to migrate various .NET applications from using Application Insights software development kits (SDKs) 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.

If you're getting started with Application Insights and don't need to migrate from the Classic API, see Enable Azure Monitor OpenTelemetry.

Note

For Azure Function Apps, see Use OpenTelemetry with Azure Functions.

Prerequisites

  • An ASP.NET Core web application already instrumented with Application Insights without any customizations
  • An actively supported version of .NET

Tip

Our product group is actively seeking feedback on this documentation. Provide feedback to [email protected] or see the Support section.

Remove the Application Insights SDK

Note

Before continuing with these steps, you should confirm that you have a current backup of your application.

  1. Remove NuGet packages

    Remove the Microsoft.ApplicationInsights.AspNetCore package from your csproj.

    dotnet remove package Microsoft.ApplicationInsights.AspNetCore
    
  2. Remove Initialization Code and customizations

    Remove any references to Application Insights types in your codebase.

    Tip

    After removing the Application Insights package, you can re-build your application to get a list of references that need to be removed.

    • Remove Application Insights from your ServiceCollection by deleting the following line:

      builder.Services.AddApplicationInsightsTelemetry();
      
    • Remove the ApplicationInsights section from your appsettings.json.

      {
          "ApplicationInsights": {
              "ConnectionString": "<Your Connection String>"
          }
      }
      
  3. Clean and Build

    Inspect your bin directory to validate that all references to Microsoft.ApplicationInsights.* were removed.

  4. Test your application

    Verify that your application has no unexpected consequences.

Tip

Our product group is actively seeking feedback on this documentation. Provide feedback to [email protected] or see the Support section.

Enable OpenTelemetry

We recommended creating a development resource and using its connection string when following these instructions.

Screenshot that shows the Application Insights overview and connection string.

Plan to update the connection string to send telemetry to the original resource after confirming migration is successful.

  1. Install the Azure Monitor Distro

    Our Azure Monitor Distro enables automatic telemetry by including OpenTelemetry instrumentation libraries for collecting traces, metrics, logs, and exceptions, and allows collecting custom telemetry.

    Installing the Azure Monitor Distro brings the OpenTelemetry SDK as a dependency.

    dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore
    
  2. Add and configure both OpenTelemetry and Azure Monitor

    The OpenTelemery SDK must be configured at application startup as part of your ServiceCollection, typically in the Program.cs.

    OpenTelemetry has a concept of three signals; Traces, Metrics, and Logs. The Azure Monitor Distro configures each of these signals.

Program.cs

The following code sample demonstrates the basics.

using Azure.Monitor.OpenTelemetry.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        // Call AddOpenTelemetry() to add OpenTelemetry to your ServiceCollection.
        // Call UseAzureMonitor() to fully configure OpenTelemetry.
        builder.Services.AddOpenTelemetry().UseAzureMonitor();

        var app = builder.Build();
        app.MapGet("/", () => "Hello World!");
        app.Run();
    }
}

We recommend setting your Connection String in an environment variable:

APPLICATIONINSIGHTS_CONNECTION_STRING=<Your Connection String>

More options to configure the Connection String are detailed here: Configure the Application Insights Connection String.

Tip

Our product group is actively seeking feedback on this documentation. Provide feedback to [email protected] or see the Support section.

Install and configure instrumentation libraries

Instrumentation libraries can be added to your project to auto collect telemetry about specific components or dependencies.

The following libraries are included in the Distro.

Customizing instrumentation libraries

The Azure Monitor Distro includes .NET OpenTelemetry instrumentation for ASP.NET Core, HttpClient, and SQLClient. You can customize these included instrumentations or manually add extra instrumentation on your own using the OpenTelemetry API.

Here are some examples of how to customize the instrumentation:

Customizing AspNetCoreTraceInstrumentationOptions
builder.Services.AddOpenTelemetry().UseAzureMonitor();
builder.Services.Configure<AspNetCoreTraceInstrumentationOptions>(options =>
{
    options.RecordException = true;
    options.Filter = (httpContext) =>
    {
        // only collect telemetry about HTTP GET requests
        return HttpMethods.IsGet(httpContext.Request.Method);
    };
});
Customizing HttpClientTraceInstrumentationOptions
builder.Services.AddOpenTelemetry().UseAzureMonitor();
builder.Services.Configure<HttpClientTraceInstrumentationOptions>(options =>
{
    options.RecordException = true;
    options.FilterHttpRequestMessage = (httpRequestMessage) =>
    {
        // only collect telemetry about HTTP GET requests
        return HttpMethods.IsGet(httpRequestMessage.Method.Method);
    };
});
Customizing SqlClientInstrumentationOptions

We provide the SQLClient instrumentation within our package while it's still in beta. When it reaches a stable release, we'll include it as a standard package reference. Until then, to customize the SQLClient instrumentation, add the OpenTelemetry.Instrumentation.SqlClient package reference to your project and use its public API.

dotnet add package --prerelease OpenTelemetry.Instrumentation.SqlClient
builder.Services.AddOpenTelemetry().UseAzureMonitor().WithTracing(builder =>
{
    builder.AddSqlClientInstrumentation(options =>
    {
        options.SetDbStatementForStoredProcedure = false;
    });
});

Configure Azure Monitor

Application Insights offered many more configuration options via ApplicationInsightsServiceOptions.

Application Insights Setting OpenTelemetry Alternative
AddAutoCollectedMetricExtractor N/A
ApplicationVersion Set "service.version" on Resource
ConnectionString See instructions on configuring the Connection String.
DependencyCollectionOptions N/A. To customize dependencies, review the available configuration options for applicable Instrumentation libraries.
DeveloperMode N/A
EnableActiveTelemetryConfigurationSetup N/A
EnableAdaptiveSampling N/A. Only fixed-rate sampling is supported.
EnableAppServicesHeartbeatTelemetryModule N/A
EnableAuthenticationTrackingJavaScript N/A
EnableAzureInstanceMetadataTelemetryModule N/A
EnableDependencyTrackingTelemetryModule See instructions on filtering Traces.
EnableDiagnosticsTelemetryModule N/A
EnableEventCounterCollectionModule N/A
EnableHeartbeat N/A
EnablePerformanceCounterCollectionModule N/A
EnableQuickPulseMetricStream AzureMonitorOptions.EnableLiveMetrics
EnableRequestTrackingTelemetryModule See instructions on filtering Traces.
EndpointAddress Use ConnectionString.
InstrumentationKey Use ConnectionString.
RequestCollectionOptions N/A. See OpenTelemetry.Instrumentation.AspNetCore options.

Remove custom configurations

The following scenarios are optional and only apply to advanced users.

  • If you have any more references to the TelemetryClient, which could be used to manually record telemetry, they should be removed.

  • If you added any custom filtering or enrichment in the form of a custom TelemetryProcessor or TelemetryInitializer, they should be removed. They can be found in your ServiceCollection.

    builder.Services.AddSingleton<ITelemetryInitializer, MyCustomTelemetryInitializer>();
    
    builder.Services.AddApplicationInsightsTelemetryProcessor<MyCustomTelemetryProcessor>();
    
  • Remove JavaScript Snippet

    If you used the Snippet provided by the Application Insights .NET SDK, it must also be removed. For full code samples of what to remove, review the guide enable client-side telemetry for web applications.

    If you added the JavaScript SDK to collect client-side telemetry, it can also be removed although it continues to work without the .NET SDK. For full code samples of what to remove, review the onboarding guide for the JavaScript SDK.

  • Remove any Visual Studio Artifacts

    If you used Visual Studio to onboard to Application Insights, you could have more files left over in your project.

    • Properties/ServiceDependencies directory might have a reference to your Application Insights resource.

Tip

Our product group is actively seeking feedback on this documentation. Provide feedback to [email protected] or see the Support section.

Next steps

Tip

Our product group is actively seeking feedback on this documentation. Provide feedback to [email protected] or see the Support section.

Support