Azure CLI 如何为 webapp 启用 Application Insights

Azure Cli How to enable Application Insights for webapp

考虑以下代码。它创建一个应用程序洞察力,然后它检索 instrumentationkey 并将其分配给我的 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

但是,如屏幕截图所示,这不会为 Web 应用程序启用应用程序洞察力。我不知道如何从 azure cli 打开它。

您需要设置更多应用程序设置,使其与从 Azure 门户启用它时完全一样。我相信检测键之后的第二个重要键是 ApplicationInsightsAgent_EXTENSION_VERSION.

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

您可以适配 AzureCLI 的 Powershell 示例:

$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

使用 link @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

有比尝试手动设置应用设置更好的方法。只需使用此命令(Link 提供的文档):

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.