Azure 应用服务的应用程序监视和 Node.js

监视运行在 Azure 应用服务上的 Node.js web 应用程序无需对代码进行任何修改。 本文将逐步讲解如何启用 Azure Monitor Application Insights 监视,并提供有关如何自动完成大规模部署过程的初步指导。

启用 Application Insights

若要为在 Azure 应用服务上运行的 Node.js 应用程序启用应用程序监视,最容易的方法是利用 Azure 门户。 在 Azure 门户中启用应用程序监视会自动使用 Application Insights 来检测应用程序,并且不需要更改任何代码。

注意

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

注意

如果同时检测到了自动检测和基于手动 SDK 的检测,则只会采用手动检测设置。 这是为了防止发送重复数据。 有关详细信息,请参阅本文中的故障排除部分

通过 Azure 门户进行自动检测

有关支持的自动检测方案的完整列表,请参阅支持的环境、语言和资源提供程序

只需单击一下,就可以为 Azure 应用服务中运行的 Node.js 应用启用监视,而无需更改代码。 适用于 Node.js 的 Application Insights 与 Linux 上的 Azure 应用服务集成(基于代码的容器和自定义容器),并且针对基于代码的应用与 Windows 上的应用服务集成。 集成处于公开预览阶段。 集成添加了 Node.js SDK(正式版)。

  1. 在应用服务的 Azure 控制面板中,选择“Application Insights”,然后选择“启用”。

    Screenshot of Application Insights tab with enable selected.

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

    注意

    选择“确定”来创建新资源时,系统将提示你“应用监视设置” 。 选择“继续”会将新的 Application Insights 资源链接到你的应用服务,这样做还会触发应用服务的重新启动

    Screenshot of Change your resource dropdown.

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

    Screenshot of instrument your application.

配置

可以使用 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 启用遥测数据收集,只需设置以下应用程序设置:

Screenshot of App Service Application Settings with available Application Insights settings.

应用程序设置定义

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

注意

探查器和快照调试器不适用于 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 配置了应用程序设置的资源管理器模板的示例,可参阅此模板。 具体请参阅从第 238 行开始的部分。

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

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

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

    Screenshot that shows the App Service web app creation menu.

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

    Screenshot that shows an App Service web app template.

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

{
    "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 East 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

$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

故障排除

下面是我们针对 Azure 应用服务中运行的基于 Node.js 的应用程序的基于扩展/代理的监视提供的分步故障排除指南。

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

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

    Screenshot of the link above results page.

    • 确认 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 的某一方面已在应用程序中存在,并将退出。

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 中缺失应用程序遥测的问题

发行说明

有关最新的更新和 bug 修复,请参阅发行说明

后续步骤