地理位置和 IP 位址處理

本文說明地理位置查閱和 IP 位址處理如何在 Application Insights 中運作,以及如何修改預設行為。

預設行為

根據預設,IP 位址會暫時收集,但不會儲存在 Application Insights中。 此程序會遵循一些基本步驟。

當遙測傳送至 Azure 時,Application Insights 會使用 IP 位址來執行地理位置查閱。 Application Insights 使用此查閱的結果來填入 、client_Cityclient_StateOrProvince 欄位 client_CountryOrRegion 。 接著會捨棄位址,並 0.0.0.0 寫入 client_IP 欄位。

若要移除地理位置資料,請參閱下列文章:

遙測類型為:

  • 瀏覽器遙測:Application Insights 會收集傳送者的 IP 位址。 擷取端點會計算 IP 位址。
  • 伺服器遙測:Application Insights 遙測模組會暫時收集用戶端 IP 位址。 設定標頭時 X-Forwarded-For ,不會在本機收集 IP 位址。 當傳入的 IP 位址清單有多個項目時,會使用最後一個 IP 位址來填入地理位置欄位。

此行為依設計是來協助避免收集不必要的個人資料和 IP 位址位置資訊。 如果可以,我們會盡可能避免收集個人資料。

注意

雖然預設值不是收集 IP 位址,但您可以覆寫此行為。 建議您確認集合不會破壞任何合規性需求或當地法規。

若要深入了解如何在 Application Insights 中處理個人資料,請參閱個人資料的指引

若未收集 IP 位址,也不會收集透過使用 IP 位址由我們的管線填入的城市和其他地理位置屬性。 您可以在來源處遮罩 IP 收集資料。 有兩種方式可以達成。 您可以:

IP 位址資料的儲存體

若要啟用 IP 集合和儲存體, DisableIpMasking Application Insights 元件的屬性必須設定為 true 。 您可以透過 Azure Resource Manager 範本 (ARM 範本) 或呼叫 REST API 來設定此屬性。

ARM 範本

{
       "id": "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/microsoft.insights/components/<resource-name>",
       "name": "<resource-name>",
       "type": "microsoft.insights/components",
       "location": "westcentralus",
       "tags": {
              
       },
       "kind": "web",
       "properties": {
              "Application_Type": "web",
              "Flow_Type": "Redfield",
              "Request_Source": "IbizaAIExtension",
              // ...
              "DisableIpMasking": true
       }
}

入口網站

如果您只需要修改單一 Application Insights 資源的行為,請使用 Azure 入口網站。

  1. 移至您的 Application Insights 資源,然後選取自動化>匯出範本

  2. 選取部署

    Screenshot that shows the Deploy button.

  3. 選取 [編輯範本]

    Screenshot that shows the Edit button, along with a warning about the resource group.

    注意

    如果您遇到前面螢幕擷取畫面中所顯示的錯誤,您可以加以解決。 其指出:資源群組所在的位置不受範本中一個或多個資源的支援。 請選擇不同的資源群組。」暫時從下拉式清單中選取不同的資源群組,然後重新選取原始資源群組。

  4. 在 JSON 範本中,properties在內部resources尋找。 將逗號新增至最後一個 JSON 欄位,然後新增下列的新行:"DisableIpMasking": true。 然後選取儲存

    Screenshot that shows the addition of a comma and a new line after the property for request source.

  5. 選取 [檢閱 + 建立]>[建立]

    注意

    如果您看到「您的部署失敗」,請查看您的部署詳細資料,以瞭解類型 microsoft.insights/components 為的部署詳細資料,並檢查狀態。 如果成功,則已部署對 DisableIpMasking 的變更。

  6. 部署完成之後,將會記錄新的遙測資料。

    如果您再次選取並編輯範本,則只會看到預設範本,而不需要新增的屬性。 如果您沒有看到 IP 位址資料,而且想要確認 "DisableIpMasking": true 是否已設定,請執行下列 PowerShell 命令:

    # Replace `Fabrikam-dev` with the appropriate resource and resource group name.
    # If you aren't using Azure Cloud Shell, you need to connect to your Azure account
    # Connect-AzAccount 
    $AppInsights = Get-AzResource -Name 'Fabrikam-dev' -ResourceType 'microsoft.insights/components' -ResourceGroupName 'Fabrikam-dev'
    $AppInsights.Properties
    

    結果會傳回屬性清單。 其中一個屬性應該讀取 DisableIpMasking: true 。 如果您在使用 Azure Resource Manager 部署新屬性之前執行 PowerShell 命令,則該屬性將不存在。

REST API

下列的 REST API 承載會進行相同的修改:

PATCH https://management.azure.com/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/microsoft.insights/components/<resource-name>?api-version=2018-05-01-preview HTTP/1.1
Host: management.azure.com
Authorization: AUTH_TOKEN
Content-Type: application/json
Content-Length: 54

{
       "location": "<resource location>",
       "kind": "web",
       "properties": {
              "Application_Type": "web",
              "DisableIpMasking": true
       }
}

PowerShell

PoweShell 'Update-AzApplicationInsights' Cmdlet 可以使用 DisableIPMasking 參數停用 IP 遮罩。

Update-AzApplicationInsights -Name "aiName" -ResourceGroupName "rgName" -DisableIPMasking:$true

如需 'Update-AzApplicationInsights' Cmdlet 的詳細資訊,請參閱 Update-AzApplicationInsights

遙測初始設定式

如果您需要比 DisableIpMasking 更有彈性的替代方案,您可以使用 遙測初始化運算式 ,將 IP 位址的所有或部分複製到自訂欄位。 此類別的程式碼在 .NET 版本中完全相同。

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

namespace MyWebApp
{
    public class CloneIPAddress : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            ISupportProperties propTelemetry = telemetry as ISupportProperties;

            if (propTelemetry !=null && !propTelemetry.Properties.ContainsKey("client-ip"))
            {
                string clientIPValue = telemetry.Context.Location.Ip;
                propTelemetry.Properties.Add("client-ip", clientIPValue);
            }
        }
    } 
}

注意

如果您無法存取 ISupportProperties,請確定您執行的是應用程式Insights SDK 的最新穩定版本。 ISupportProperties適用於高基數值。 GlobalProperties更適合低基數值,例如區功能變數名稱稱和環境名稱。

 using Microsoft.ApplicationInsights.Extensibility;
 using CustomInitializer.Telemetry;

builder.services.AddSingleton<ITelemetryInitializer, CloneIPAddress>();

Node.js

appInsights.defaultClient.addTelemetryProcessor((envelope) => {
    const baseData = envelope.data.baseData;
    if (appInsights.Contracts.domainSupportsProperties(baseData)) {
        const ipAddress = envelope.tags[appInsights.defaultClient.context.keys.locationIp];
        if (ipAddress) {
            baseData.properties["client-ip"] = ipAddress;
        }
    }
});

檢視遙測初始化運算式的結果

如果您將新的流量傳送至您的網站並等候幾分鐘,您可以接著執行查詢以確認集合正常運作:

requests
| where timestamp > ago(1h) 
| project appName, operation_Name, url, resultCode, client_IP, customDimensions.["client-ip"]

新收集的 IP 位址會出現在資料行中 customDimensions_client-ip 。 預設資料 client-ip 行仍會全部四個八位零。

如果您要從 localhost 進行測試,而值是 customDimensions_client-ip,這個值 ::1 是預期的行為。 值 ::1 代表 IPv6 中的回送位址。 它相當於 127.0.0.1 在 IPv4 中。

常見問題集

本節提供常見問題的答案。

如何計算城市、國家/地區及其他地理位置的資料?

我們會查詢 Web 用戶端的 IP 位址 (IPv4 或 IPv6):

  • 瀏覽器遙測:我們會收集傳送者的 IP 位址。
  • 伺服器遙測:Application Insights 模組會收集用戶端 IP 位址。 如果已設定 X-Forwarded-For,則不會收集該位址。
  • 若要深入了解如何在 Application Insights 中收集 IP 位址和地理位置資料,請參閱地理位置和 IP 位址處理

您可以設定 ClientIpHeaderTelemetryInitializer 從不同的標頭取得 IP 位址。 例如,在某些系統中,Proxy、負載平衡器或 CDN 會將它移至 X-Originating-IP深入了解

如果您已移轉至工作區型資源,您可以使用 Power BI 在地圖上顯示您的要求遙測。

下一步

  • 深入瞭解 Application Insights 中的個人資料收集
  • 深入了解 Application Insights 中的 IP 位址收集運作方式。 本文是我們其中一位工程師所撰寫較舊的外部部落格文章。 其日期早於 IP 位址記錄為 0.0.0.0 的目前預設行為。 本文會更深入探討內建遙測初始設定式的機制。