在 Azure 应用程序服务中为 .NET、Node.js、Python 和 Java 应用程序启用应用程序监视

自动检测(也称为运行时监视)是启用适用于 Azure 应用程序服务的 Application Insights 的最简单方法,无需任何代码更改或高级配置。 根据具体的应用场景,通过手动检测评估是否需要更高级的监视。

备注

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

启用 Application Insights

重要

如果同时检测到自动检测监视和手动基于 SDK 的检测,则只会采用手动检测设置。 这样安排可防止发送重复数据。 若要了解详细信息,请参阅故障排除部分

备注

可以使用应用服务环境变量边栏选项卡中的 APPLICATIONINSIGHTS_CONFIGURATION_CONTENT 环境变量来配置自动附加的代理。 有关可通过此环境变量传递的配置选项的详细信息,请参阅 Node.js 配置

适用于 Node.js 的 Application Insights 与 Linux 上的 Azure 应用程序服务集成(基于代码的容器和自定义容器),并且针对基于代码的应用与 Windows 上的应用程序服务集成。 集成处于公开预览阶段。

在 Azure 门户中进行自动检测

  1. 在应用程序服务的左侧导航菜单中选择“Application Insights”,然后选择“启用”。

    显示“Application Insights”选项卡的屏幕截图,其中选择了“启用”。

  2. 创建新资源,或为此应用程序选择现有 Application Insights 资源。

    备注

    选择“确定”以创建新资源时,系统将提示你“应用监视设置”。 选择“继续”会将新的 Application Insights 资源链接到应用程序服务。 然后,应用程序服务将重启。

    显示“更改资源”下拉列表的屏幕截图。

  3. 指定要使用的资源后,一切已就绪。

    “检测应用程序”的屏幕截图。

手动升级监视扩展/代理

Application Insights Node.js 版本会在应用程序服务更新中自动更新,无法手动更新

如果遇到已在最新版本的 Application Insights SDK 中修复的问题,可以移除自动检测,并使用最新的 SDK 版本手动检测应用程序。

配置监视扩展/代理

可以使用 JSON 配置 Node.js 代理。 将 APPLICATIONINSIGHTS_CONFIGURATION_CONTENT 环境变量设置为 JSON 字符串,或将 APPLICATIONINSIGHTS_CONFIGURATION_FILE 环境变量设置为包含 JSON 的文件路径。

"samplingPercentage": 80,
"enableAutoCollectExternalLoggers": true,
"enableAutoCollectExceptions": true,
"enableAutoCollectHeartbeat": true,
"enableSendLiveMetrics": true,
...

有完整的配置集可用。 只需使用有效的 json 文件即可。

启用客户端监视

若要针对 Node.js 应用程序启用客户端监视,需手动将客户端 JavaScript SDK 添加到应用程序

自动监视

若要为 Application Insights 启用遥测数据收集,只需设置以下应用程序设置:

应用程序服务应用程序设置的屏幕截图,其中显示了可用的 Application Insights 设置。

应用程序设置定义

应用设置名称 定义 Value
ApplicationInsightsAgent_EXTENSION_VERSION 用于控制运行时监视的主扩展。 在 Windows 中为 ~2,或者,在 Linux 中为 ~3
XDT_MicrosoftApplicationInsights_NodeJS 标记,用于控制是否包含 Node.js 代理。 01(只在 Windows 中适用)。

备注

Snapshot Debugger 不适用于 Node.js 应用程序。

使用 Azure 资源管理器配置应用程序服务应用程序设置

可以使用 Azure 资源管理器模板来管理和配置 Azure 应用程序服务的应用程序设置。 在使用资源管理器自动化部署新的应用程序服务资源或修改现有资源的设置时,可以使用此方法。

下面是应用程序服务资源的应用程序设置 JSON 的基本结构:

      "resources": [
        {
          "name": "appsettings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
            "[resourceId('Microsoft.Web/sites', variables('webSiteName'))]"
          ],
          "tags": {
            "displayName": "Application Insights Settings"
          },
          "properties": {
            "key1": "value1",
            "key2": "value2"
          }
        }
      ]

若要创建使用默认 Application Insights 设置的资源管理器模板,请像创建启用了 Application Insights 的新 Web 应用程序一样开始该过程。

  1. 使用所需的 Web 应用信息创建新的应用程序服务资源。 在“监视”选项卡上启用 Application Insights。

  2. 选择“查看 + 创建”。 然后,选择“下载自动化模板”。

    显示应用程序服务 Web 应用创建菜单的屏幕截图。

    此选项将生成配置了全部所需设置的最新资源管理器模板。

    显示应用程序服务 Web 应用模板的屏幕截图。

在以下示例中,将 AppMonitoredSite 的所有实例替换为你的站点名称:

备注

如果使用 Windows,请将 ApplicationInsightsAgent_EXTENSION_VERSION 设置为 ~2。 如果使用 Linux,请将 ApplicationInsightsAgent_EXTENSION_VERSION 设置为 ~3

{
    "resources": [
        {
            "name": "[parameters('name')]",
            "type": "Microsoft.Web/sites",
            "properties": {
                "siteConfig": {
                    "appSettings": [
                        {
                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                            "value": "[reference('microsoft.insights/components/AppMonitoredSite', '2015-05-01').InstrumentationKey]"
                        },
                        {
                            "name": "APPLICATIONINSIGHTS_CONNECTION_STRING",
                            "value": "[reference('microsoft.insights/components/AppMonitoredSite', '2015-05-01').ConnectionString]"
                        },
                        {
                            "name": "ApplicationInsightsAgent_EXTENSION_VERSION",
                            "value": "~2"
                        }
                    ]
                },
                "name": "[parameters('name')]",
                "serverFarmId": "[concat('/subscriptions/', parameters('subscriptionId'),'/resourcegroups/', parameters('serverFarmResourceGroup'), '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
                "hostingEnvironment": "[parameters('hostingEnvironment')]"
            },
            "dependsOn": [
                "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]",
                "microsoft.insights/components/AppMonitoredSite"
            ],
            "apiVersion": "2016-03-01",
            "location": "[parameters('location')]"
        },
        {
            "apiVersion": "2016-09-01",
            "name": "[parameters('hostingPlanName')]",
            "type": "Microsoft.Web/serverfarms",
            "location": "[parameters('location')]",
            "properties": {
                "name": "[parameters('hostingPlanName')]",
                "workerSizeId": "[parameters('workerSize')]",
                "numberOfWorkers": "1",
                "hostingEnvironment": "[parameters('hostingEnvironment')]"
            },
            "sku": {
                "Tier": "[parameters('sku')]",
                "Name": "[parameters('skuCode')]"
            }
        },
        {
            "apiVersion": "2015-05-01",
            "name": "AppMonitoredSite",
            "type": "microsoft.insights/components",
            "location": "China North 2",
            "properties": {
                "ApplicationId": "[parameters('name')]",
                "Request_Source": "IbizaWebAppExtensionCreate"
            }
        }
    ],
    "parameters": {
        "name": {
            "type": "string"
        },
        "hostingPlanName": {
            "type": "string"
        },
        "hostingEnvironment": {
            "type": "string"
        },
        "location": {
            "type": "string"
        },
        "sku": {
            "type": "string"
        },
        "skuCode": {
            "type": "string"
        },
        "workerSize": {
            "type": "string"
        },
        "serverFarmResourceGroup": {
            "type": "string"
        },
        "subscriptionId": {
            "type": "string"
        }
    },
    "$schema": "https://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0"
}

通过 PowerShell 启用

若要通过 PowerShell 启用应用程序监视,仅须更改基础的应用程序设置。 以下示例为资源组 AppMonitoredRG 中名为 AppMonitoredSite 的网站启用应用程序监视。 它配置要发送到 012345678-abcd-ef01-2345-6789abcd 检测密钥的数据。

备注

建议使用 Azure Az PowerShell 模块与 Azure 交互。 若要开始,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

备注

如果使用 Windows,请将 ApplicationInsightsAgent_EXTENSION_VERSION 设置为 ~2。 如果使用 Linux,请将 ApplicationInsightsAgent_EXTENSION_VERSION 设置为 ~3

$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

常见问题解答

本部分提供常见问题的解答。

Application Insights 在我的项目中修改哪些内容?

详细信息取决于项目类型。 以下列表是 Web 应用程序的示例。

  • 将文件添加到项目:

    • ApplicationInsights.config
    • ai.js
  • 安装 NuGet 包:

    • Application Insights API:核心 API
    • 适用于 Web 应用程序的 Application Insights API:用于从服务器发送遥测
    • 适用于 JavaScript 应用程序的 Application Insights API:用于从客户端发送遥测
  • 包括包中的程序集:

    • Microsoft.ApplicationInsights
    • Microsoft.ApplicationInsights.Platform
  • 将项目插入:

    • Web.config
    • packages.config
  • 在客户端和服务器代码中插入代码片段,以使用 Application Insights 资源 ID 初始化它们。 例如,在 MVC 应用中,代码插入到主页 Views/Shared/_Layout.cshtml 中。 仅对于新项目,可手动将 Application Insights 添加到现有项目

Application Insights 中的标准指标与 Azure 应用程序服务指标之间有何区别?

Application Insights 为向应用程序发出的请求收集遥测数据。 如果在 WebApps/WebServer 中发生故障,并且请求未到达用户应用程序,则 Application Insights 不会有任何有关它的遥测数据。

Application Insights 算出的 serverresponsetime 持续时间不一定与 Web 应用观察到的服务器响应时间匹配。 此行为是因为 Application Insights 仅计算实际到达用户应用程序的持续时间。 如果请求在 WebServer 中停滞或排队,则该等待时间将包含在 Web 应用指标中,但不会包含在 Application Insights 指标中。

疑难解答

测试应用程序主机与引入服务之间的连接性

Application Insights SDK 和代理发送遥测,将其作为 REST 调用引入到引入终结点。 可以使用原始 REST 客户端通过 PowerShell 或使用 curl 命令,测试从 Web 服务器或应用程序主机计算机到引入服务终结点的连接。 请参阅排查 Azure Monitor Application Insights 中缺失应用程序遥测的问题

缺少遥测

Windows

  1. 检查 ApplicationInsightsAgent_EXTENSION_VERSION 应用设置是否设置为值 ~2

  2. 浏览到 https://yoursitename.scm.chinacloudsites.cn/ApplicationInsights

    结果页上方的链接的屏幕截图。

    • 确认 Application Insights Extension StatusPre-Installed Site Extension, version 2.8.x.xxxx, is running.

      如果它未运行,请按照启用 Application Insights 监视的说明进行操作。

    • 导航到 D:\local\Temp\status.json 并打开 status.json

    确认 SDKPresent 设置为“false”,AgentInitializedSuccessfully 设置为“true”,IKey 设置为有效的 iKey。

    JSON 文件的示例:

        "AppType":"node.js",
    
        "MachineName":"c89d3a6d0357",
    
        "PID":"47",
    
        "AgentInitializedSuccessfully":true,
    
        "SDKPresent":false,
    
        "IKey":"00000000-0000-0000-0000-000000000000",
    
        "SdkVersion":"1.8.10"
    
    

    如果 SDKPresent 为 true,则表示扩展检测到 SDK 的某一方面已在应用程序中存在,并将退出。

Linux

  1. 检查 ApplicationInsightsAgent_EXTENSION_VERSION 应用设置是否设置为值 ~3

  2. 导航到 /var/log/applicationinsights/ 并打开 status.json

    确认 SDKPresent 设置为“false”,AgentInitializedSuccessfully 设置为“true”,IKey 设置为有效的 iKey。

    JSON 文件的示例:

        "AppType":"node.js",
    
        "MachineName":"c89d3a6d0357",
    
        "PID":"47",
    
        "AgentInitializedSuccessfully":true,
    
        "SDKPresent":false,
    
        "IKey":"00000000-0000-0000-0000-000000000000",
    
        "SdkVersion":"1.8.10"
    
    

    如果 SDKPresent 为 true,则表示扩展检测到 SDK 的某一方面已在应用程序中存在,并将退出。