Skip to content

App Insights Instrumentation Key to Connection String in Simple Steps

azure application insights instrumentation key to connection string

Application Insights Instrumentation Key

Support for instrumentation key–based ingestion in Application Insights (part of Azure Monitor) will end soon. Your Application Insights resources will still receive data, but updates and customer support for the old system will stop.

Application Insights is shifting from global ingestion endpoints to regional endpoints using connection strings. This change offers:

  • Authenticated telemetry ingestion.
  • Regional data residency & privacy: Ensures data stays within a geographic region by eliminating global ingestion endpoints.
  • Endpoint customization: For intranet or hybrid environments.

To continue receiving technical support, you need to migrate to connection string–based ingestion before March 31, 2025.

In this post, we’ll cover how to migrate Application Insights Instrumentation keys to connection strings, common pitfalls, and tips to speed up the process.

Verify RBAC Permissions

Ensure you have at least reader RBAC access to view connection strings in Application Insights. Any other roles such as owner/contributor should also work.

application insights view rbac access

Identify Instrumentation Key Resources

Ensure you have sufficient rights to view resources using Application Insights.

Identifying applications that use Application Insights instrumentation keys can be a tedious task. These keys are often stored in application settings. To simplify this process, I used the following script to scan and identify Instrumentation Key usages among resources across the entire Azure subscription.

It currently scans Azure App Service, Azure App Service deployment slots, Azure Functions, and Azure Container Instances. You can customize the script according to your needs.

# Login to Azure & optionally run
Connect-AzAccount

# Optional: Select subscription
#Select-AzureSubscription -SubscriptionName 'xxx'

# Define the resource types to search
$resourceTypes = @(
    "Microsoft.Web/sites",
    "Microsoft.Web/sites/slots",
    "Microsoft.Web/sites/functions",
    "Microsoft.ContainerInstance/containerGroups"
)

$instrumentationKeyResources = @()
foreach ($resourceType in $resourceTypes) {
    # Get all resources of the specified type
    $resources = Get-AzResource -ResourceType $resourceType

    foreach ($resource in $resources) {
        try {
            if ($resourceType -eq "Microsoft.Web/sites" -or $resourceType -eq "Microsoft.Web/sites/slots" -or $resourceType -eq "Microsoft.Web/sites/functions") {
                $app = Get-AzWebApp -Name $resource.Name -ResourceGroupName $resource.ResourceGroupName
                foreach ($envVar in $app.SiteConfig.AppSettings) {
                    if ($envVar.Name -like "*InstrumentationKey*") {
                        $instrumentationKeyResources += $resource
                        break
                    }
                }
            }
            elseif ($resourceType -eq "Microsoft.ContainerInstance/containerGroups") {
                $containerGroup = Get-AzContainerGroup -ResourceGroupName $resource.ResourceGroupName -Name $resource.Name
                foreach ($envVar in $containerGroup.Containers.EnvironmentVariables) {
                    if ($envVar.Name -like "*InstrumentationKey*") {
                        $instrumentationKeyResources += $resource
                        break
                    }
                }
            }
        }
        catch {
            Write-Warning "Unable to get details for resource $($resource.Name): $_"
        }
    }
}

# Output the results
$instrumentationKeyResources | Format-Table -Property Name, ResourceType, ResourceGroupName

# Export results to CSV
$instrumentationKeyResources | Export-Csv -Path "InstrumentationKeyResources.csv" -NoTypeInformation

 Update to the Latest SDK

Application Insights connection strings are supported from the following NuGet package versions:

  • Microsoft.ApplicationInsights: Version 2.15.0 and later
  • Microsoft.ApplicationInsights.AspNetCore: Version 2.15.0 and later

Generate Application Insights Connection String

Navigate to the Application Insights overview page. With sufficient access rights, you’ll see both the instrumentation key and the connection string. Example:

  • Instrumentation Key: cf391594-1da8-4d8d-ad5c-d46acd62620c
  • Connection String: InstrumentationKey=cf391594-1da8-4d8d-ad5c-d46acd62620c;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/;LiveEndpoint=https://westeurope.livediagnostics.monitor.azure.com/;ApplicationId=e4f34481-c2c9-43da-acbc-751c06880588
application insights view instrumentation key and connection string

Note that the new connection string is composed of:

  • InstrumentationKey: No change here. It’s a GUID that identifies the Application Insights resource to your subscription.
  • IngestionEndpoint: Endpoint for collecting data such as console logs, exceptions, and dependency calls.
  • LiveEndpoint: Endpoint for collecting live metrics.
  • ApplicationId: API access key for Application Insights.

Currently, Microsoft does not support Microsoft Entra authentication for auto instrumentation scenarios, but this is planned for the future

Update Configuration Files

Ideally, the Application Insights Instrumentation Key should be stored under the application setting APPINSIGHTS_INSTRUMENTATIONKEY. However, this isn’t always the case. It’s recommended to perform thorough checks to ensure this.

Option 1: Default Settings

If the client accepts the default settings, simply change the application setting name from APPINSIGHTS_INSTRUMENTATIONKEY to APPLICATIONINSIGHTS_CONNECTION_STRING and configure the connection string accordingly.

Before:

After:

Option 2: Hardcoded References

If the application has hardcoded references to the Application Insights Instrumentation Key, you will need to update the code. This is typically found in startup.cs or web.config/applicationsettings.json.

Some examples of hardcoded references:

var instrumentationKey = hostingContext.Configuration.GetSection("APPINSIGHTS_INSTRUMENTATIONKEY").Value;

var telemetryConfiguration = new TelemetryConfiguration {
 InstrumentationKey = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY")
};

var telemetryClient = new TelemetryClient(
  new TelemetryConfiguration { InstrumentationKey = "my-application-instr-key" });

services.AddApplicationInsightsTelemetry(
  Configuration["ApplicationInsights:InstrumentationKey"]);

Recommended approach: Update these references to use APPLICATIONINSIGHTS_CONNECTION_STRING instead.

Most Application Insights NuGet implementations’ constructors can accept either the Application Insights Instrumentation Key or the connection string. If nothing is passed, it will fetch the value accordingly. Thus, it’s best to either remove the hardcoded references or update them to use APPLICATIONINSIGHTS_CONNECTION_STRING.

Important Note: Microsoft advises against using both a connection string and an instrumentation key. The latter, if set, will supersede the former and could result in telemetry not appearing on the portal.

Using Infrastructure as a Code for Migration

Planning to use DevOps or Infrastructure as Code (IaC) to update your Application Insights connection references? Here are some snippets to help you upgrade your implementation.

ARM Template for application insights connection string

Before:

{
  "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
  "value": "[reference(resourceId('Microsoft.Insights/components', 'appInsightsName'), '2015-05-01').InstrumentationKey]"
}

After:

{
  "name": "APPLICATIONINSIGHTS_CONNECTION_STRING",
  "value": "[reference(resourceId('Microsoft.Insights/components', 'appInsightsName'), '2015-05-01').ConnectionString]"
}

Bicep sample for application insights connection string

Before:

appSettings: [
  {
    name: 'APPINSIGHTS_INSTRUMENTATIONKEY'
    value: appInsights.properties.InstrumentationKey
  }
]

After:

appSettings: [
  {
    name: 'APPLICATIONINSIGHTS_CONNECTION_STRING'
    value: appInsights.properties.ConnectionString
  }
]

Terraform samples for application insights connection string

Before:

app_settings = {
  "APPINSIGHTS_INSTRUMENTATIONKEY" = azurerm_application_insights.example.instrumentation_key
}

After:

app_settings = {
  "APPLICATIONINSIGHTS_CONNECTION_STRING" = azurerm_application_insights.example.connection_string
}

Verify the Changes

After migration, verify that everything works correctly. Logging to Application Insights is asynchronous and can take a few minutes to appear. Ensure your application is still logging data as expected.

Remember, a broken Application Insights connection won’t always break the application, so thorough verification is essential.