使用 Azure CLI 管理 Application Insights 组件

在 Azure Monitor 中,组件是分布式或微服务应用程序的可独立部署的部分。 使用以下 Azure CLI 命令来管理 Application Insights 中的组件。

本文中的示例执行以下管理任务:

  • 创建组件。
  • 将组件连接到 webapp。
  • 将组件链接到包含组件的存储帐户。
  • 为组件创建连续导出配置。

先决条件

可以使用本地 Azure CLI。

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。

  • 本地 Azure CLI,请了解如何安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI

    • 通过使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展

    • 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade

创建组件

如果还没有资源组和工作区,请使用 az group createaz monitor log-analytics workspace create 创建它们:

az group create --name ContosoAppInsightRG --location chinanorth2
az monitor log-analytics workspace create --resource-group ContosoAppInsightRG \
   --workspace-name AppInWorkspace

若要创建组件,请运行 az monitor app-insights component create 命令。 az monitor app-insights component show 命令可显示组件。

az monitor app-insights component create --resource-group ContosoAppInsightRG \
   --app ContosoApp --location chinanorth2 --kind web --application-type web \
   --retention-time 120
az monitor app-insights component show --resource-group ContosoAppInsightRG --app ContosoApp

连接 webapp

本示例将组件连接到 webapp。 你可以使用 az appservice plan createaz webapp create 命令创建 webapp:

az appservice plan create --resource-group ContosoAppInsightRG --name ContosoAppService
az webapp create --resource-group ContosoAppInsightRG --name ContosoApp \
   --plan ContosoAppService --name ContosoApp8765

运行 az monitor app-insights component connect-webapp 命令,将组件连接到 webapp:

az monitor app-insights component connect-webapp --resource-group ContosoAppInsightRG \
   --app ContosoApp --web-app ContosoApp8765 --enable-debugger false --enable-profiler false

可以改为使用 az monitor app-insights component connect-function 命令连接到 Azure 函数。

你可以将组件链接到存储帐户。 如需创建存储帐户,可使用 az storage account create 命令:

az storage account create --resource-group ContosoAppInsightRG \
   --name contosolinkedstorage --location chinanorth2 --sku Standard_LRS

若要将组件链接到存储帐户,请运行 az monitor app-insights component links-storage link 命令。 可以使用 az monitor app-insights component links-storage show 命令查看现有链接:

az monitor app-insights component linked-storage link  --resource-group ContosoAppInsightRG \
   --app ContosoApp --storage-account contosolinkedstorage
az monitor app-insights component linked-storage show --resource-group ContosoAppInsightRG \
   --app ContosoApp

若要取消链接存储,请运行 az monitor app-insights component links-storage unlink 命令:

az monitor app-insights component linked-storage unlink  \
   --resource-group ContosoAppInsightRG --app ContosoApp

设置连续导出

连续导出可将来自 Application Insights 门户的事件以 JSON 格式保存在存储容器中。

备注

只有经典 Application Insights 资源支持连续导出。 基于工作区的 Application Insights 资源必须使用诊断设置

若要创建存储容器,请运行 az storage container create 命令。

az storage container create --name contosostoragecontainer --account-name contosolinkedstorage \
   --public-access blob 

你需要将对容器的访问权限设置为只写。 运行 az storage container policy create cmdlet:

az storage container policy create --container-name contosostoragecontainer \
   --account-name contosolinkedstorage --name WAccessPolicy --permissions w

使用 az storage container generate-sas 命令创建 SAS 密钥。 请确保使用 --output tsv 参数值来保存密钥,以免出现不需要的格式(如引号)。 有关详细信息,请参阅高效使用 Azure CLI

containersas=$(az storage container generate-sas --name contosostoragecontainer \
   --account-name contosolinkedstorage --permissions w --output tsv)

若要创建连续导出,请运行 az monitor app-insights component continues-export create 命令:

az monitor app-insights component continues-export create --resource-group ContosoAppInsightRG \
   --app ContosoApp --record-types Event --dest-account contosolinkedstorage \
   --dest-container contosostoragecontainer --dest-sub-id 00000000-0000-0000-0000-000000000000 \
   --dest-sas $containersas

你可以使用 az monitor app-insights component continues-export delete 命令删除已配置的连续导出:

az monitor app-insights component continues-export list \
   --resource-group ContosoAppInsightRG --app ContosoApp
az monitor app-insights component continues-export delete \
   --resource-group ContosoAppInsightRG --app ContosoApp --id abcdefghijklmnopqrstuvwxyz=

清理部署

如果创建了一个资源组来测试这些命令,则可以使用 az group delete 命令删除该资源组及其所有内容:

az group delete --name ContosoAppInsightRG 

本文中使用的 Azure CLI 命令

后续步骤

Azure Monitor CLI 示例

查找和诊断性能问题