Create and configure Application Insights resources
Article
Important
This article applies to workspace-based Application Insights resources. Classic Application Insights resources have been retired. Transition to workspace-based Application Insights to take advantage of new capabilities.
Application Insights integrates with Log Analytics and sends telemetry to a common Log Analytics workspace. This setup provides full access to Log Analytics features, consolidates logs in one location, and allows for unified Azure role-based access control which eliminates the need for cross-app/workspace queries.
This article shows you how to create and configure Application Insights resources. Along with the Application Insights resource itself, you can add various configurations like setting the daily cap and pricing plan. You can also create availability tests, set up metric alerts, and automate the process using Azure Resource Manager.
Note
Data ingestion and retention for workspace-based Application Insights resources are billed through the Log Analytics workspace where the data is located. To learn more about billing, see Azure Monitor Logs pricing details.
To access Application Insights Azure CLI commands, you first need to run:
Azure CLI
az extension add -n application-insights
If you don't run the az extension add command, you see an error message that states az : ERROR: az monitor: 'app-insights' is not in the 'az monitor' command group. See 'az monitor --help'.
Open the category Monitoring & Diagnostics, then select Application Insights.
Enter all relevant information, then Review + create your Application Insights resource.
Note
If you don't connect to an existing Log Analytics workspace during resource creation, a new Log Analytics resource is created automatically along with your Application Insights resource.
After creating your resource, you can find the corresponding workspace information in the Application Insights Overview pane.
Select the blue link text to go to the associated Log Analytics workspace where you can take advantage of the new unified workspace query environment.
Note
We still provide full backward compatibility for your Application Insights classic resource queries, workbooks, and log-based alerts. To query or view the new workspace-based table structure or schema, you must first go to your Log Analytics workspace. Select Logs (Analytics) in the Application Insights panes for access to the classic Application Insights query experience.
To create a workspace-based Application Insights resource, a Log Analytics workspace is required. If you don't have one already, you can use the following Azure CLI command to create one.
To create a workspace-based Application Insights resource, a Log Analytics workspace is required. If you don't have one already, you can use the following Azure PowerShell command to create one.
To create a workspace-based Application Insights resource, a Log Analytics workspace is required. If you don't have one already, you can use the following REST API call to create one.
Create a new .bicepparam file (for example, my-parameters.bicepparam), copy the following content into it, and replace the placeholders <application-insights-resource-name>, <application-type>, <azure-region-name>, <subscription-id>, <resource-group-name>, and <log-analytics-workspace-name> with your specific values:
Here's how to create a new Application Insights resource using an ARM template.
Create a template
Create a new .json file (for example, my-template.json) and copy the following content into it:
JSON
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"name": {
"type": "string",
"metadata": {
"description": "Name of Application Insights resource."
}
},
"type": {
"type": "string",
"metadata": {
"description": "Type of app you are deploying. This field is for legacy reasons and will not impact the type of Application Insights resource you deploy."
}
},
"regionId": {
"type": "string",
"metadata": {
"description": "Which Azure region to deploy the resource to. This must be a valid Azure regionId."
}
},
"tagsArray": {
"type": "object",
"metadata": {
"description": "See documentation on tags: https://learn.microsoft.com/azure/azure-resource-manager/management/tag-resources."
}
},
"requestSource": {
"type": "string",
"metadata": {
"description": "Source of Azure Resource Manager deployment"
}
},
"workspaceResourceId": {
"type": "string",
"metadata": {
"description": "Log Analytics workspace ID to associate with your Application Insights resource."
}
}
},
"resources": [
{
"type": "Microsoft.Insights/components",
"apiVersion": "2020-02-02",
"name": "[parameters('name')]",
"location": "[parameters('regionId')]",
"tags": "[parameters('tagsArray')]",
"kind": "other",
"properties": {
"Application_Type": "[parameters('type')]",
"Request_Source": "[parameters('requestSource')]",
"WorkspaceResourceId": "[parameters('workspaceResourceId')]"
}
}
]
}
Note
For more information on resource properties, see Property values.
Flow_Type and Request_Source aren't used but are included in this sample for completeness.
Create a parameter file
Create a new .json file (for example, my-parameters.json), copy the following content into it, and replace the placeholders <application-insights-resource-name>, <application-type>, <azure-region-name>, <subscription-id>, <resource-group-name>, and <log-analytics-workspace-name> with your specific values:
-ResourceGroupName is the group where you want to create the new resources.
-TemplateFile must occur before the custom parameters.
-TemplateParameterFile is the name of the parameter file.
Configure monitoring
After creating an Application Insights resource, you configure monitoring.
Get the connection string
The connection string identifies the resource that you want to associate your telemetry data with. You can also use it to modify the endpoints your resource uses as a destination for your telemetry. You must copy the connection string and add it to your application's code or to an environment variable.
To get the connection string of your Application Insights resource:
Open your Application Insights resource in the Azure portal.
On the Overview pane in the Essentials section, look for Connection string.
If you hover over the connection string, an icon appears which allows you to copy it to your clipboard.
To get the connection string, run the following Azure CLI command in your terminal and replace the placeholders <application-insights-resource-name> and <resource-group-name> with your specific values:
Azure CLI
az monitor app-insights component show --app<application-insights-resource-name>--resource-group<resource-group-name>--query connectionString --output tsv
For more information about the az monitor app-insights component show command, see to the Azure CLI documentation.
To get the connection string, run the following Azure PowerShell command in your terminal and replace the placeholders <resource-group-name> and <application-insights-resource-name> with your specific values:
To retrieve the details of your Application Insights resource, use the following request and replace the placeholders <subscription-id>, <resource-group-name>, <application-insights-resource-name>, and <access-token> with your specific values:
HTTP
GET https://management.azure.com/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Insights/components/<application-insights-resource-name>?api-version=2015-05-01
Authorization: Bearer <access-token>
Look for the properties.connectionString field in the JSON response.
For more information about retrieving information about Application Insights resources using the REST API, see the REST API documentation.
Not applicable to Bicep templates.
Not applicable to ARM templates.
Application monitoring with OpenTelemetry
For application monitoring with OpenTelemetry, you install the appropriate Azure Monitor OpenTelemetry Distro and point the connection string to your newly created resource.
For information on how to set up application monitoring with OpenTelemetry, see the following documentation specific to the language:
For monitoring services like Azure Functions and Azure App Service, you can first create your Application Insights resource, then point to it when you enable monitoring. Alternatively, you can create a new Application Insights resource during the enablement process.
Configure Application Insights resources
Modify the associated workspace
After creating an Application Insights resource, you can modify the associated Log Analytics workspace.
In your Application Insights resource, select Properties > Change workspace > Log Analytics Workspaces.
To change the Log Analytics workspace, run the following Azure CLI command in your terminal and replace the placeholders <application-insights-resource-name>, <resource-group-name>, and <log-analytics-workspace-name> with your specific values:
Azure CLI
az monitor app-insights component update --app<application-insights-resource-name>--resource-group<resource-group-name>--workspace<log-analytics-workspace-name>
For more information about the az monitor app-insights component update command, see the Azure CLI documentation.
To change the Log Analytics workspace, run the following Azure PowerShell command in your terminal and replace the placeholders <resource-group-name>, <application-insights-resource-name>, <subscription-id>, and <log-analytics-workspace-name> with your specific values:
To change the Log Analytics workspace using REST API, use the following request and replace the placeholders <subscription-id>, <resource-group-name>, <application-insights-resource-name>, <access-token>, <azure-region-name>, and <log-analytics-workspace-name> with your specific values:
For more information about modifying the associated workspace using the REST API, see the REST API documentation.
To change the Log Analytics workspace, paste the following code into your template and replace the placeholders <application-insights-resource-name>, <azure-region-name>, <application-type>, and <log-analytics-workspace-name> with your specific values:
To change the Log Analytics workspace, paste the following code into your template and replace the placeholders <application-insights-resource-name>, <azure-region-name>, <application-type>, and <log-analytics-workspace-name> with your specific values:
The legacy continuous export functionality isn't supported for workspace-based resources. Instead, use Diagnostic settings.
Note
Diagnostic settings export might increase costs. For more information, see Export telemetry from Application Insights.
For pricing information for this feature, see the Azure Monitor pricing page. Before the start of billing, notifications are sent. If you continue to use telemetry export after the notice period, you'll be billed at the applicable rate.
In your Application Insights resource, select Diagnostic settings > Add diagnostic setting.
You can select all tables or a subset of tables to archive to a storage account. You can also stream to an event hub.
To export telemetry using diagnostic settings, run the following Azure CLI command in your terminal and replace the placeholders <diagnostic-setting-name>, <application-insights-resource-name>, <resource-group-name>, and <storage-account-name> with your specific values:
Azure CLI
az monitor diagnostic-settings create --name<diagnostic-setting-name>--resource<application-insights-resource-name>--resource-group<resource-group-name>--resource-type Microsoft.Insights/components --storage-account<storage-account-name>
This example command enables diagnostic settings and sends all logs of your Application Insights resource to the specified storage account. To also send metrics, add --metrics '[{"category": "AllMetrics", "enabled": true}]' to the command.
For more information about the az monitor diagnostic-settings create command, see the Azure CLI documentation.
To export telemetry using diagnostic settings, run the following Azure PowerShell command in your terminal and replace the placeholders <application-insights-resource-id>, <diagnostic-setting-name>, and <storage-account-id> with your specific values:
This example command enables diagnostic settings and sends all metrics and logs of your Application Insights resource to the specified storage account.
To export telemetry to an Azure storage account using a diagnostic setting, use the following request and replace the placeholders <subscription-id>, <resource-group-name>, <application-insights-resource-name>, <diagnostic-setting-name>, <access-token>, and <storage-account-name> with your specific values:
This example call enables diagnostic settings and sends all metrics and logs of your Application Insights resource to the specified storage account.
For more information about creating a diagnostic setting using the REST API, see the REST API documentation.
To export telemetry using diagnostic settings, paste the following code into your template and replace the placeholders <application-insights-resource-name>, <azure-region-name>, <application-type>, <diagnostic-setting-name>, <subscription-id>, <resource-group>, and <storage-account-name> with your specific values:
To export telemetry using diagnostic settings, paste the following code into your template and replace the placeholders <application-insights-resource-name>, <azure-region-name>, <application-type>, <diagnostic-setting-name>, <subscription-id>, <resource-group>, and <storage-account-name> with your specific values:
To set the data retention for the associated Log Analytics workspace, paste the following code into your template and replace the placeholders <log-analytics-workspace-name>, <azure-region-name>, <retention-period-in-days> with your specific values:
To set the data retention for the associated Log Analytics workspace, paste the following code into your template and replace the placeholders <log-analytics-workspace-name>, <azure-region-name>, <retention-period-in-days> with your specific values:
The daily cap must be set independently for both Application Insights and the underlying Log Analytics workspace. The effective daily cap is the minimum of the two settings.
Currently, Azure doesn't provide a way to set the daily cap for Application Insights via the Azure CLI.
To change the daily cap for Log Analytics, run the following Azure CLI command in your terminal and replace the placeholders <resource-group-name>, <log-analytics-workspace-name>, and <daily-cap-in-gb> with your specific values.
Azure CLI
az monitor log-analytics workspace update --resource-group<resource-group-name>--workspace-name<log-analytics-workspace-name>--set workspaceCapping.dailyQuotaGb=<daily-cap-in-gb>
For more information about the az monitor log-analytics workspace update command, see the Azure CLI documentation.
To change the daily cap for Application Insights and Log Analytics, run the following Azure PowerShell commands in your terminal and replace the placeholders with your specific values.
Currently, Azure doesn't provide a way to set the daily cap for Application Insights with the Azure CLI.
To change the daily cap for Log Analytics, use the following request and replace the placeholders <subscription-id>, <resource-group-name>, <log-analytics-workspace-name>, <access-token>, <azure-region-name>, and <daily-cap-in-gb> with your specific values:
For more information about the setting the Log Analytics daily cap, see the REST API documentation
Note
Currently, Azure doesn't provide a way to set the daily cap for Application Insights with a Bicep template.
To set the daily cap for Log Analytics, paste the following code into your template and replace the placeholders <log-analytics-workspace-name>, <azure-region-name>, and <daily-cap-in-gb> with your specific values:
Currently, Azure doesn't provide a way to set the daily cap for Application Insights using an ARM template.
To set the daily cap for Log Analytics, paste the following code into your template and replace the placeholders <log-analytics-workspace-name>, <azure-region-name>, and <daily-cap-in-gb> with your specific values:
The pricing plan for Application Insights resources can be set in the associated Log Analytics workspace. For more information about available pricing plans, see Azure Monitor Logs cost calculations and options.
To set the pricing plan, run one of the following Azure CLI commands in your terminal and replace the placeholders <log-analytics-workspace-name>, <azure-region-name>, and (if applicable) <capacity-reservation-in-gb> with your specific values:
Pay-as-you-go
Azure CLI
az monitor log-analytics workspace update --resource-group<resource-group-name>--workspace-name<log-analytics-workspace-name>--set PerGB2018
Commitment tier
Azure CLI
az monitor log-analytics workspace update --resource-group<resource-group-name>--workspace-name<log-analytics-workspace-name>--set CapacityReservation --level<capacity-reservation-in-gb>
For more information about the az monitor log-analytics workspace update command, see the Azure CLI documentation.
To set the pricing plan, run one of the following Azure PowerShell commands in your terminal and replace the placeholders <log-analytics-workspace-name>, <azure-region-name>, and (if applicable) <capacity-reservation-in-gb> with your specific values:
To set the pricing plan using the REST API, use one of the following requests and replace the placeholders <subscription-id>, <resource-group-name>, <log-analytics-workspace-name>, <access-token>, and (if applicable) <capacity-reservation-in-gb> with your specific values:
For more information about setting the pricing plan using the REST API, see the REST API documentation.
To set the pricing plan using Bicep, paste the following code into your template and replace the placeholders <log-analytics-workspace-name>, <azure-region-name>, and for the commitment tier also <capacity-reservation-in-gb> with your specific values:
For more information about updating the Microsoft.OperationalInsights/workspaces resource using Bicep, see the templates documentation.
To set the pricing plan using an ARM template, paste the following code into your template and replace the placeholders <log-analytics-workspace-name>, <azure-region-name>, and for the commitment tier also <capacity-reservation-in-gb> with your specific values:
To create a standard availability test with default settings, run the following Azure CLI command in your terminal and replace the placeholders <resource-group-name>, <azure-region-name>, <web-test-name>, <url>, <subscription-id>, and <application-insights-resource-name> with your specific values:
For more information about the az monitor app-insights web-test create command, see the Azure CLI documentation.
Note
The web test region (-location) is different from geographic location (-locations, notice the plural form) of which multiple can be selected. The -location refers to the Azure region where the web test is created and hosted, while -locations refers to geographic location or locations from which the web test is executed. For a full list of all geographic locations, see Application Insights availability tests.
To create a standard availability test with default settings, run the following Azure PowerShell command in your terminal and replace the placeholders <resource-group-name>, <azure-region-name>, <web-test-name>, <url>, <subscription-id>, and <application-insights-resource-name> with your specific values:
The web test region (-Location) is different from the geographic location (-GeoLocation) of which multiple can be selected. -Location refers to the Azure region where the web test is created and hosted, while -GeoLocation refers to the geographic location or locations from which the web test is executed. For a full list of all geographic locations, see Application Insights availability tests.
To create a standard availability test with default settings using the REST API, use the following request and replace the placeholders <subscription-id>, <resource-group-name>, <application-insights-resource-name>, <web-test-name>, <access-token>, <azure-region-name>, and <url> with your specific values:
The web test region (-location) is different from the geographic location (-Locations, notice the plural form) of which multiple can be selected. -location refers to the Azure region where the web test is created and hosted, while -Locations refers to the geographic location or locations from which the web test is executed. For a full list of all geographic locations, see Application Insights availability tests.
To learn more about creating and configuring web tests using the REST API, see our REST API documentation.
To create a standard availability test with default settings using Bicep, add the following code to your template and replace the placeholders <web-test-name>, <azure-region-name>, <subscription-id>, <resource-group-name>, <application-insights-resource-name>, and <url> with your specific values:
The web test region (location) is different from the geographic location (Locations) of which multiple can be selected. location refers to the Azure region where the web test is created and hosted, while Locations refers to the geographic location or locations from which the web test is executed. For a full list of all geographic locations, see Application Insights availability tests.
To create a standard availability test with default settings using an ARM template, add the following code to your template and replace the placeholders <web-test-name>, <azure-region-name>, <subscription-id>, <resource-group-name>, <application-insights-resource-name>, and <url> with your specific values:
The web test region (location) is different from the geographic location (Locations) of which multiple can be selected. location refers to the Azure region where the web test is created and hosted, while Locations refers to the geographic location or locations from which the web test is executed. For a full list of all geographic locations, see Application Insights availability tests.
For more information about creating availability tests using an ARM template, see Microsoft.Insights webtests.
Add a metric alert
Tip
Each Application Insights resource comes with metrics that are available out of the box. If separate components report to the same Application Insights resource, it might not make sense to alert on these metrics.
How many Application Insights resources should I deploy?
When you're developing the next version of a web application, you don't want to mix up the Application Insights telemetry from the new version and the already released version.
To avoid confusion, send the telemetry from different development stages to separate Application Insights resources with separate connection strings.
When to use a single Application Insights resource
Use a single Application Insights resource for:
Streamlining DevOps/ITOps management for applications deployed together, typically developed and managed by the same team.
Centralizing key performance indicators, such as response times and failure rates, in a dashboard by default. Segment by role name in the metrics explorer if necessary.
When there's no need for different Azure role-based access control management between application components.
When identical metrics alert criteria, continuous exports, and billing/quotas management across components suffice.
When it's acceptable for an API key to access data from all components equally, and 10 API keys meet the needs across all components.
When the same smart detection and work item integration settings are suitable across all roles.
Note
If you want to consolidate multiple Application Insights resources, you can point your existing application components to a new, consolidated Application Insights resource. The telemetry stored in your old resource doesn't transfer to the new resource. Only delete the old resource when you have enough telemetry in the new resource for business continuity.
Other considerations
To activate portal experiences, add custom code to assign meaningful values to the Cloud_RoleName attribute. Without these values, portal features don't function.
For Azure Service Fabric applications and classic cloud services, the SDK automatically configures services by reading from the Azure Role Environment. For other app types, you typically need to set it explicitly.
Live Metrics can't split data by role name.
Version and release tracking
When you publish a new version of your application, you want to be able to separate the telemetry from different builds. You can set the Application Version property so you can filter search and metric explorer results.
There are several different methods of setting the Application Version property.
Option 1: Set the version directly
Add the line telemetryClient.Context.Component.Version = typeof(MyProject.MyClass).Assembly.GetName().Version; to the initialization code of your application.
To ensure that all TelemetryClient instances are set consistently, wrap that line in a telemetry initializer.
Option 2: Set the version in BuildInfo.config (ASP.NET only)
The Application Insights web module picks up the version from the BuildLabel node. Include this file in your project and remember to set the Copy Always property in Solution Explorer.
This step generates a file called yourProjectName.BuildInfo.config. The Publish process renames it to BuildInfo.config.
The build label contains a placeholder (*AutoGen_...*) when you build with Visual Studio. When built with the Microsoft Build Engine, the placeholder is populated with the correct version number.
To allow the Microsoft Build Engine to generate version numbers, set the version like 1.0.* in AssemblyReference.cs.
Release annotations
If you use Azure DevOps, you can get an annotation marker added to your charts whenever you release a new version.
Automate the resource creation process
The resource creation process can be automated by using Bicep or JSON templates with Azure Resource Manager. You can package several resources together to create them in one deployment. For example, you can create an Application Insights resource with availability tests, metric alerts, and a diagnostic setting to send telemetry to an Azure Storage account.
Generate a template in the Azure portal
You can generate a template from existing resources.
Application Insights only
Go to the Application Insights resource in the Azure portal.
Open Export template listed under Automation in the left-hand navigation bar.
(Optional): To use your own parameters, uncheck Include parameters.
Download the template file or Deploy it directly in the Azure portal.
Multiple resources
Go to the resource group of your Application Insights resource.
On the Overview pane, mark all resources you want to be included in the template, then select Export template in the top navigation bar.
(Optional): To use your own parameters, uncheck Include parameters.
Download the template file or Deploy it directly in the Azure portal.
You can also use quickstart templates, available towards the bottom of each Azure resource reference documentation page linked in this section. To learn how to use templates, visit Tutorial: Use Azure Quickstart Templates.
Frequently asked questions
This section provides answers to common questions.
How do I move an Application Insights resource to a new region?
Transferring existing Application Insights resources between regions isn't supported, and you can't migrate historical data to a new region. The workaround involves:
Creating a new Application Insights resource in the desired region.
Re-creating any unique customizations from the original resource in the new one.
Updating your application with the new region resource's connection string.
Testing to ensure everything works as expected with the new Application Insights resource.
Decide to either keep or delete the original Application Insights resource. Deleting a classic resource means to lose all historical data. If the resource is workspace-based, the data remains in Log Analytics, enabling access to historical data until the retention period expires.
Unique customizations that commonly need to be manually re-created or updated for the resource in the new region include but aren't limited to:
Re-create custom dashboards and workbooks.
Re-create or update the scope of any custom log/metric alerts.
Re-create availability alerts.
Re-create any custom Azure role-based access control settings that are required for your users to access the new resource.
Replicate settings involving ingestion sampling, data retention, daily cap, and custom metrics enablement. These settings are controlled via the Usage and estimated costs pane.
Continuous export in classic resources must be configured again.
Diagnostic settings in workspace-based resources must be configured again.
Can I use providers('Microsoft.Insights', 'components').apiVersions[0] in my Azure Resource Manager deployments?
We don't recommend using this method of populating the API version. The newest version can represent preview releases, which might contain breaking changes. Even with newer non-preview releases, the API versions aren't always backward compatible with existing templates. In some cases, the API version might not be available to all subscriptions.
In this module, you learn how to create and configure access to a Log Analytics workspace. You also learn how to configure data retention and to enable health status alerts for a Log Analytics workspace.
This article describes the Application Insights telemetry data model including request, dependency, exception, trace, event, metric, PageView, and context.