0
0
Azurecloud~5 mins

Application Insights for apps in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you run an app, you want to know if it works well and if users have problems. Application Insights helps you see how your app behaves and find issues quickly.
When you want to track how many users visit your app and what they do.
When you want to find errors or slow parts in your app automatically.
When you want to see if your app is healthy and running smoothly.
When you want to get alerts if your app stops working or is too slow.
When you want to understand how your app performs over time.
Config File - azure-pipelines.yml
azure-pipelines.yml
trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: AzureCLI@2
  inputs:
    azureSubscription: 'example-subscription'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      az monitor app-insights component create --app my-app-insights --location eastus --resource-group example-rg --application-type web
      az monitor app-insights component update --app my-app-insights --resource-group example-rg --retention-time 90
  displayName: 'Create and configure Application Insights'

This YAML file is for Azure Pipelines to automate creating Application Insights.

trigger: Runs this pipeline when code is pushed to the main branch.

pool: Uses a Linux machine to run commands.

steps: Runs Azure CLI commands to create Application Insights resource named my-app-insights in eastus region inside resource group example-rg. It also sets data retention to 90 days.

Commands
This command creates a new Application Insights resource named 'my-app-insights' in the 'eastus' region inside the resource group 'example-rg'. It sets the type to monitor web applications.
Terminal
az monitor app-insights component create --app my-app-insights --location eastus --resource-group example-rg --application-type web
Expected OutputExpected
{ "appId": "12345678-90ab-cdef-1234-567890abcdef", "applicationType": "web", "connectionString": "InstrumentationKey=abcdef12-3456-7890-abcd-ef1234567890;IngestionEndpoint=https://eastus-0.in.applicationinsights.azure.com/", "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/microsoft.insights/components/my-app-insights", "location": "eastus", "name": "my-app-insights", "resourceGroup": "example-rg", "retentionInDays": 30, "status": "Healthy", "tags": {}, "type": "microsoft.insights/components" }
--app - Sets the name of the Application Insights resource.
--location - Specifies the Azure region where the resource is created.
--resource-group - Specifies the Azure resource group to contain the resource.
This command updates the data retention period of the Application Insights resource to 90 days so that telemetry data is kept longer.
Terminal
az monitor app-insights component update --app my-app-insights --resource-group example-rg --retention-time 90
Expected OutputExpected
{ "appId": "12345678-90ab-cdef-1234-567890abcdef", "retentionInDays": 90, "status": "Healthy" }
--retention-time - Sets how many days telemetry data is kept.
This command shows the current settings and status of the Application Insights resource to verify it was created and configured correctly.
Terminal
az monitor app-insights component show --app my-app-insights --resource-group example-rg
Expected OutputExpected
{ "appId": "12345678-90ab-cdef-1234-567890abcdef", "applicationType": "web", "location": "eastus", "name": "my-app-insights", "resourceGroup": "example-rg", "retentionInDays": 90, "status": "Healthy" }
Key Concept

If you remember nothing else from this pattern, remember: Application Insights helps you watch your app's health and usage by collecting data automatically.

Common Mistakes
Not specifying the correct resource group when creating Application Insights.
The resource will be created in the wrong group or the command will fail if the group doesn't exist.
Always use the --resource-group flag with an existing resource group name.
Forgetting to update the retention time, so data is kept only for the default 30 days.
You might lose important telemetry data too soon for analysis.
Use the --retention-time flag to set a longer retention period if needed.
Not verifying the resource after creation.
You might miss errors or misconfigurations that prevent data collection.
Run 'az monitor app-insights component show' to check the resource status.
Summary
Create Application Insights resource with az monitor app-insights component create to start monitoring your app.
Update the retention time to keep telemetry data longer using az monitor app-insights component update.
Verify the resource and its settings with az monitor app-insights component show.