Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Cli How to enable Application Insights for webapp

Consider the following code. It creates an application insight, then it retrieves the instrumentationkey and assigns it to my webapp.

    az monitor app-insights component create -g $resourceGroup --app $webapp --application-type web --kind web --tags $defaultTags
    
    $instrumentationKey = az monitor app-insights component show -g $resourceGroup -a $webapp --query 'instrumentationKey' -o tsv
    az webapp config appsettings set -g $resourceGroup -n $webapp --settings APPINSIGHTS_INSTRUMENTATIONKEY=$instrumentationKey APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=$instrumentationKey

However, this does not turn on application insight for the webapp as shown in this screen capture. I cannot figure out how to turn it on from azure cli.

enter image description here

like image 823
Sam Avatar asked Jul 28 '20 18:07

Sam


Video Answer


3 Answers

Using the link @Alex AIT provided the cli could be as follows.

Please note that you could also rely on the fact that if you don't create an App Insights instance an auto instance will be created and used.

# (...) Set up $plan, $resourcegroup and $region

az appservice plan create --name $plan --resource-group $resourcegroup --location $region --sku FREE

[String]$webapp="myapp"
az webapp create --name $webapp --plan $plan --resource-group $resourcegroup

[String]$appinsights=$webapp
az monitor app-insights component create --app $appinsights --location $region  --resource-group $resourcegroup

# Get the instrumentation key 
# '--output tsv', which is 'Tab-separated values, with no keys'
# is used to obtain the unquoted value
# as shown in https://docs.microsoft.com/en-us/cli/azure/query-azure-cli?view=azure-cli-latest#get-a-single-value
[String]$instrumentationKey = (az monitor app-insights component show --app $appinsights --resource-group $resourcegroup --query  "instrumentationKey" --output tsv)

# Configure the app to use new app insights instance
# Based on https://docs.microsoft.com/en-us/azure/azure-monitor/app/azure-web-apps?tabs=net#enabling-through-powershell
az webapp config appsettings set --name $webapp --resource-group $resourcegroup --settings APPINSIGHTS_INSTRUMENTATIONKEY=$instrumentationKey APPLICATIONINSIGHTS_CONNECTION_STRING=InstrumentationKey=$instrumentationKey ApplicationInsightsAgent_EXTENSION_VERSION=~2
like image 89
tymtam Avatar answered Sep 28 '22 12:09

tymtam


You need to set a couple more app settings to make it exactly like if you enabled it from the Azure Portal. I believe the second important key after the instrumentation key is ApplicationInsightsAgent_EXTENSION_VERSION.

https://docs.microsoft.com/en-us/azure/azure-monitor/app/azure-web-apps?tabs=net#automate-monitoring

Powershell example which you can adapt to AzureCLI:

$app = Get-AzWebApp -ResourceGroupName "AppMonitoredRG" -Name "AppMonitoredSite" -ErrorAction Stop
$newAppSettings = @{} # case-insensitive hash map
$app.SiteConfig.AppSettings | %{$newAppSettings[$_.Name] = $_.Value} # preserve non Application Insights application settings.
$newAppSettings["APPINSIGHTS_INSTRUMENTATIONKEY"] = "012345678-abcd-ef01-2345-6789abcd"; # set the Application Insights instrumentation key
$newAppSettings["APPLICATIONINSIGHTS_CONNECTION_STRING"] = "InstrumentationKey=012345678-abcd-ef01-2345-6789abcd"; # set the Application Insights connection string
$newAppSettings["ApplicationInsightsAgent_EXTENSION_VERSION"] = "~2"; # enable the ApplicationInsightsAgent
$app = Set-AzWebApp -AppSettings $newAppSettings -ResourceGroupName $app.ResourceGroup -Name $app.Name -ErrorAction Stop
like image 42
Alex AIT Avatar answered Sep 28 '22 11:09

Alex AIT


There is a better approach than trying to manually set the app settings. Simply use this command (Link to docs provided):

az monitor app-insights component connect-webapp --app
                                             --resource-group
                                             --web-app
                                             [--enable-debugger {false, true}]
                                             [--enable-profiler {false, true}]

Connecting an application insights to a web app.

like image 37
Yaser Avatar answered Sep 28 '22 11:09

Yaser