创建 Application Insights 资源

注意

本文适用于不再推荐使用的 Application Insights 经典资源。

本文中的信息已过时,不会再更新。

转换到基于工作区的 Application Insights,以利用新功能

Application Insights 在 Azure 资源中显示有关应用程序的数据。 创建新资源是设置 Application Insights 以监视新应用程序中的一个环节。 创建新资源后,可以获取其检测密钥并使用它来配置 Application Insights SDK。 检测密钥会将遥测链接到资源。

重要

对经典 Application Insights 的支持将于 2024 年 2 月 29 日结束转换到基于工作区的 Application Insights,以利用新功能。 2021 年 2 月之后引入的新区域不支持创建经典 Application Insights 资源。

注意

对检测密钥引入的支持将于 2025 年 3 月 31 日结束。 检测密钥引入功能将会继续工作,但我们将不再为该功能提供更新或支持。 转换为连接字符串,以利用新功能

登录 Azure

如果没有 Azure 订阅,请在开始之前创建一个免费帐户。

创建 Application Insights 资源

登录 Azure 门户,并创建 Application Insights 资源。

一个屏幕截图,其中显示了选择左上角的“+”登录、“开发人员工具”和“Application Insights”。

设置 说明
名称 Unique value 标识所监视的应用的名称。
资源组 myResourceGroup 用于托管 Application Insights 数据的新资源组或现有资源组的名称。
区域 East US 选择离你或者离托管应用的位置较近的一个位置。
资源模式 ClassicWorkspace-based 基于工作区的资源可用于将 Application Insights 遥测发送到常用 Log Analytics 工作区。 有关详细信息,请参阅基于工作区的 Application Insights 资源

注意

你可以在不同资源组中使用相同的资源名称,但使用全局唯一名称会有好处。 如果计划执行跨资源查询,则使用全局唯一名称可简化所需的语法。

在必填字段中输入合适的值。 选择“查看 + 创建” 。

一个屏幕截图,其中显示了在必填字段中输入值和“查看 + 创建”按钮。

创建应用后,一个新窗格会显示有关受监视应用程序的性能和使用情况数据。

复制检测密钥

检测密钥标识你要将遥测数据与之关联的资源。 你需要复制检测密钥并将其添加到应用程序的代码中。

在应用中安装 SDK

在应用中安装 Application Insights SDK。 此步骤在很大程度上依赖于应用程序的类型。

使用检测密钥来配置在应用程序中安装的 SDK

SDK 包含可发送遥测数据的标准模块,因此你无需编写任何代码。 若要跟踪用户操作或更细致地诊断问题,请使用 API 发送自己的遥测数据。

自动创建资源

使用 PowerShell 或 Azure CLI 自动创建资源。

PowerShell

新建 Application Insights 资源。

New-AzApplicationInsights [-ResourceGroupName] <String> [-Name] <String> [-Location] <String> [-Kind <String>]
 [-Tag <Hashtable>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]

示例

New-AzApplicationInsights -Kind java -ResourceGroupName testgroup -Name test1027 -location eastus

结果

Id                 : /subscriptions/{subid}/resourceGroups/testgroup/providers/microsoft.insights/components/test1027
ResourceGroupName  : testgroup
Name               : test1027
Kind               : web
Location           : eastus
Type               : microsoft.insights/components
AppId              : 8323fb13-32aa-46af-b467-8355cf4f8f98
ApplicationType    : web
Tags               : {}
CreationDate       : 10/27/2017 4:56:40 PM
FlowType           :
HockeyAppId        :
HockeyAppToken     :
InstrumentationKey : 00000000-aaaa-bbbb-cccc-dddddddddddd
ProvisioningState  : Succeeded
RequestSource      : AzurePowerShell
SamplingPercentage :
TenantId           : {subid}

如需此 cmdlet 的完整 PowerShell 文档,以及了解如何检索检测密钥,请参阅 Azure PowerShell 文档

Azure CLI(预览版)

若要访问预览版 Application Insights Azure CLI 命令,首先需要运行以下命令:

 az extension add -n application-insights

如果不运行 az extension add 命令,则会看到一条错误消息,指出:az : ERROR: az monitor: 'app-insights' is not in the 'az monitor' command group. See 'az monitor --help'.

运行以下命令来创建 Application Insights 资源:

az monitor app-insights component create --app
                                         --location
                                         --resource-group
                                         [--application-type]
                                         [--kind]
                                         [--tags]

示例

az monitor app-insights component create --app demoApp --location westus2 --kind web --resource-group demoRg --application-type web

结果

az monitor app-insights component create --app demoApp --location eastus --kind web --resource-group demoApp --application-type web
{
  "appId": "87ba512c-e8c9-48d7-b6eb-118d4aee2697",
  "applicationId": "demoApp",
  "applicationType": "web",
  "creationDate": "2019-08-16T18:15:59.740014+00:00",
  "etag": "\"0300edb9-0000-0100-0000-5d56f2e00000\"",
  "flowType": "Bluefield",
  "hockeyAppId": null,
  "hockeyAppToken": null,
  "id": "/subscriptions/{subid}/resourceGroups/demoApp/providers/microsoft.insights/components/demoApp",
  "instrumentationKey": "00000000-aaaa-bbbb-cccc-dddddddddddd",
  "kind": "web",
  "location": "eastus",
  "name": "demoApp",
  "provisioningState": "Succeeded",
  "requestSource": "rest",
  "resourceGroup": "demoApp",
  "samplingPercentage": null,
  "tags": {},
  "tenantId": {tenantID},
  "type": "microsoft.insights/components"
}

要获取此命令的完整 Azure CLI 文档并且要了解如何检索检测密钥,请参阅 Azure CLI 文档

替代默认终结点

警告

不要修改终结点。 转换到连接字符串可简化配置,并无需进行终结点修改。

若要将 Application Insights 中的数据发送到特定区域,你需要替代默认终结点地址。 每个 SDK 都需要进行稍有不同的修改,本文将对所有这些修改进行说明。

要执行这些更改,你需要调整示例代码,并将 QuickPulse_Endpoint_AddressTelemetryChannel_Endpoint_AddressProfile_Query_Endpoint_address 的占位符值替换为你的具体区域的实际终结点地址。 本文末尾包含指向需要此配置的区域的终结点地址的链接。

注意

对检测密钥引入的支持将于 2025 年 3 月 31 日结束。 检测密钥引入功能将会继续工作,但我们将不再为该功能提供更新或支持。 转换为连接字符串,以利用新功能


SDK 代码更改

注意

每次执行 SDK 升级时,都会自动重写 applicationinsights.config 文件。 执行 SDK 升级后,请确保重新输入区域特定的终结点值。

<ApplicationInsights>
  ...
  <TelemetryModules>
    <Add Type="Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse.QuickPulseTelemetryModule, Microsoft.AI.PerfCounterCollector">
      <QuickPulseServiceEndpoint>Custom_QuickPulse_Endpoint_Address</QuickPulseServiceEndpoint>
    </Add>
  </TelemetryModules>
   ...
  <TelemetrySinks>
    <Add Name = "default">
      <TelemetryChannel>
         <EndpointAddress>TelemetryChannel_Endpoint_Address</EndpointAddress>
      </TelemetryChannel>
    </Add>
  </TelemetrySinks>
  ...
  <ApplicationIdProvider Type="Microsoft.ApplicationInsights.Extensibility.Implementation.ApplicationId.ApplicationInsightsApplicationIdProvider, Microsoft.ApplicationInsights">
    <ProfileQueryEndpoint>Profile_Query_Endpoint_address</ProfileQueryEndpoint>
  </ApplicationIdProvider>
  ...
</ApplicationInsights>

需要修改终结点的区域

目前唯一需要修改终结点的区域是 Azure 政府Azure 中国

区域 终结点名称 Value
Azure 中国 遥测通道 https://dc.applicationinsights.azure.cn/v2/track
Azure 中国 QuickPulse(实时指标) https://live.applicationinsights.azure.cn/QuickPulseService.svc
Azure 中国 配置文件查询 https://dc.applicationinsights.azure.cn/api/profiles/{0}/appId
Azure Government 遥测通道 https://dc.applicationinsights.us/v2/track
Azure Government QuickPulse(实时指标) https://quickpulse.applicationinsights.us/QuickPulseService.svc
Azure Government 配置文件查询 https://dc.applicationinsights.us/api/profiles/{0}/appId

如果当前使用的是 Application Insights REST API(通常通过 api.applicationinsights.io 访问),则需要使用你所在区域的本地终结点。

区域 终结点名称 Value
Azure 中国 REST API api.applicationinsights.azure.cn
Azure Government REST API api.applicationinsights.us

后续步骤