Azure Cli Как включить Application Insights для веб-приложения

Рассмотрим следующий код. Он создает представление о приложении, затем извлекает инструментальный ключ и назначает его моему веб-приложению.

    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

Однако это не включает анализ приложений для веб-приложения, как показано на этом снимке экрана. Не могу понять, как включить с лазурного кли.

введите описание изображения здесь


person Sam    schedule 28.07.2020    source источник


Ответы (2)


Вам нужно установить еще пару параметров приложения, чтобы оно было точно таким, как если бы вы включили его на портале Azure. Я считаю, что второй важный ключ после ключа инструментария - это ApplicationInsightsAgent_EXTENSION_VERSION.

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

Пример Powershell, который вы можете адаптировать к 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
person Alex AIT    schedule 28.07.2020

Используя ссылку @Alex AIT при условии, что cli может быть следующим.

Обратите внимание, что вы также можете полагаться на тот факт, что, если вы не создадите экземпляр App Insights, будет создан и использован автоматический экземпляр.

# (...) 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
person tymtam    schedule 03.08.2020
comment
не могли бы вы объяснить, как будет создан автоэкземпляр? - person Safari137; 05.02.2021