共用方式為


Azure App Service 和 Node.js 的應用程式監視

監視在 Azure App Services 上執行的 Node.js Web 應用程式,不需要對程式碼進行任何修改。 本文將逐步引導您啟用 Azure 監視器 Application Insights 監視功能,並提供自動化大規模部署程序的初步指引。

啟用 Application Insights

針對在 Azure App Services 上執行的 Node.js 應用程式啟用應用程式監視的最簡單方式是透過 Azure 入口網站。 在 Azure 入口網站中開啟應用程式監視,會自動使用 Application Insights 來檢測您的應用程式,而且不需要變更任何程式碼。

注意

您可以在 App Service 環境變數刀鋒視窗中,使用 APPLICATIONINSIGHTS_CONFIGURATION_CONTENT 環境變數來設定自動附加的代理程式。 如需可透過此環境變數傳遞之組態選項的詳細資訊,請參閱 Node.js 組態

注意

如果同時偵測到自動檢測和手動 SDK 型檢測,則只會接受手動檢測設定。 這是為了避免傳送重複的資料。 如需詳細資訊,請參閱本文中的疑難排解一節

透過 Azure 入口網站自動檢測

如需支援自動檢測案例的完整清單,請參閱支援的環境、語言和資源提供者

只要按一下即可開啟針對 Azure App Service 中執行的 Node.js 應用程式的監視,不需要變更程式碼。 適用於 Node.js 的 Application Insights 會與 Linux 上的 Azure App Service 整合 (程式碼型和自訂容器),以及與 Windows 上的 App Service 整合 (針對程式碼型應用程式)。 整合處於公開預覽狀態。 整合會新增 Node.js SDK,目前已加入 GA 版。

  1. 在 Azure 控制台中,選取您應用程式服務的 Application Insights,然後選取 [啟用]

    [Application Insights] 索引卷標的螢幕快照,其中已選取 [啟用]。

  2. 選擇建立新的資源,或選取此應用程式現有的 Application Insights 資源。

    注意

    當您選取 [確定] 來建立新資源時,系統會提示您 [套用監視設定]。 選取 [繼續] 會將新的 Application Insights 資源連結至您的應用程式服務,這麼做也會觸發應用程式服務的重新啟動

    [變更資源] 下拉式清單的螢幕快照。

  3. 指定要使用的資源之後,設定全部執行。

    檢測應用程式的螢幕快照。

組態

您可以使用 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 啟用遙測收集,只需要設定下列應用程式設定:

App Service Application 設定 的螢幕快照,其中包含可用的 Application Insights 設定。

應用程式設定定義

應用程式設定名稱 定義
ApplicationInsightsAgent_EXTENSION_VERSION 主要延伸模組,用於控制執行階段監視。 Windows 的 ~2 或 Linux 中 的 ~3
XDT_MicrosoftApplicationInsights_NodeJS 用來控制是否包含 Node.js 代理程式的旗標。 0 或 1 (僅適用 Windows)。

注意

分析工具和快照偵錯工具不適用 Node.js 應用程式

使用 Azure Resource Manager 的 App Service 應用程式設定

您可以使用 Azure Resource Manager 範本,來管理和設定 Azure App Service 的應用程式設定。 當您使用 Resource Manager 自動化部署新的 App Service 資源,或修改現有資源的設定時,可以使用此方法。

App Service 資源的應用程式設定 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 設定應用程式設定的 Resource Manager 範本範例,此範本可能會有所幫助。 具體而言,請查看第 238 行開始的區段。

若要使用預設 Application Insights 設定建立 Resource Manager 範本,請開始程式,就像您要建立已啟用 Application Insights 的新 Web 應用程式一樣。

  1. 使用您想要的 Web 應用程式資訊建立新的 App Service資源。 在 [監視] 索引標籤上啟用 Application Insights。

  2. 選取 [檢閱 + 建立]。 接著,選取 [下載自動化的範本]

    顯示 App Service Web 應用程式建立功能表的螢幕快照。

    此選項會產生已設定所有必要設定的最新 Resource Manager 範本。

    顯示 App Service 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": "West US 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

疑難排解

以下是延伸模組/代理程式型監視的逐步疑難排解指南,此指南適用於在 Azure 應用程式服務上執行的 Node.js 型的應用程式。

  1. 檢查 ApplicationInsightsAgent_EXTENSION_VERSION 應用程式的設定是否設為 "~2" 值。

  2. 瀏覽至 https://yoursitename.scm.azurewebsites.net/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 的某些層面已存在於應用程式中,而且會輪詢。

下列兩者間有何差異:Application Insights 標準計量與Azure App Service 計量?

Application Insights 會收集對應用程式提出要求的遙測。 如果 WebApps/WebServer 中發生失敗,且要求未到達使用者應用程式,則 Application Insights 不會對其進行任何遙測。

Application Insights 所計算的 serverresponsetime 持續時間不一定符合 Web Apps 觀察到的伺服器回應時間。 此行為是因為 Application Insights 僅會計算要求實際到達使用者應用程式的持續時間。 如果要求在 WebServer 中停滯或排入佇列,則等候時間將納入 Web Apps 計量中,但不會納入 Application Insights 計量中。

測試應用程式主機與擷取服務之間的連線

應用程式深入剖析 SDK 和代理程式會傳送遙測,以擷取為 REST 呼叫擷取到我們擷取的端點。 您可以使用來自 PowerShell 或 curl 命令的原始 REST 用戶端,測試從 Web 伺服器或應用程式主機電腦到擷取服務端點的連線。 請參閱針對 Azure 監視器 Application Insights 中遺失的應用程式遙測進行疑難排解

版本資訊

如需最新的更新和錯誤修正,請參閱版本資訊

下一步