地理位置和 IP 地址处理

本文介绍了 Application Insights 中的地理位置查找和 IP 地址处理工作的方式,以及如何修改默认行为。

默认行为

默认情况下,IP 地址是暂时收集的,不会存储在 Application Insights 中。 此过程遵循一些基本步骤。

将遥测数据发送到 Azure 时,Application Insights 使用 IP 地址执行地理位置查找。 Application Insights 使用此查找结果填充字段 client_Cityclient_StateOrProvinceclient_CountryOrRegion。 然后放弃该地址,并将 0.0.0.0 写入 client_IP 字段。

若要删除地理位置数据,请参阅以下文章:

遥测类型包括:

  • 浏览器遥测:Application Insights 收集发送方的 IP 地址。 ingestion 终结点计算 IP 地址。
  • 服务器遥测:Application Insights 遥测模块暂时收集客户端 IP 地址。 设置 X-Forwarded-For 标头时,不会在本地收集 IP 地址。 当传入 IP 地址列表具有多个 IP 地址时,最后一个 IP 地址用于填充地理位置字段。

此行为是有意设计的,目的是帮助避免对个人数据和 IP 地址位置信息进行不必要的收集。 我们建议尽量避免收集个人数据。

注意

虽然默认不收集 IP 地址,但可以重写此行为。 但是,建议验证集合是否违反任何合规性要求或当地法规。

若要了解有关在 Application Insights 中处理个人数据的详细信息,请参阅个人数据指南

如果未收集 IP 地址,也不会收集管道使用 IP 地址填充的城市和其他地理位置属性。 可以在源中屏蔽 IP 收集。 可通过两种方式来执行此操作。 方法:

存储 IP 地址数据

若要启用 IP 收集和存储,必须将 Application Insights 组件的 DisableIpMasking 属性设置为 true。 可以通过 Azure 资源管理器模板(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": "chinaeast2",
       "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 模板中,找到 resources 中的 properties。 在最后一个 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.
    # Connect-AzAccount 
    $AppInsights = Get-AzResource -Name 'Fabrikam-dev' -ResourceType 'microsoft.insights/components' -ResourceGroupName 'Fabrikam-dev'
    $AppInsights.Properties
    

    最后返回一份属性列表。 其中一个属性显示为 DisableIpMasking: true。 如果在使用 Azure 资源管理器部署新属性之前运行 PowerShell 命令,该属性将不存在。

REST API

以下 REST API 有效负载进行相同的修改:

PATCH https://management.chinacloudapi.cn/subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/microsoft.insights/components/<resource-name>?api-version=2018-05-01-preview HTTP/1.1
Host: management.chinacloudapi.cn
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,请确保运行的是最新稳定版 Application 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 格式的环回地址。 它等效于 IPv4 中的 127.0.0.1

常见问题

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

如何计算国家/地区、市/县和其他地理位置数据?

查找 Web 客户端的 IP 地址(IPv4 或 IPv6):

  • 浏览器遥测:收集发送方的 IP 地址。
  • 服务器遥测:Application Insights 模块收集客户端 IP 地址。 如果设置了 X-Forwarded-For,则不会进行收集。
  • 若要详细了解如何在 Application Insights 中收集 IP 地址和地理位置数据,请参阅地理位置和 IP 地址处理

可以配置 ClientIpHeaderTelemetryInitializer,以从不同的标头获取 IP 地址。 例如,在某些系统中,代理、负载均衡器或 CDN 会将其移动到 X-Originating-IP了解详细信息

如果已迁移到基于工作区的资源,则可以使用 Power BI 在地图上显示请求遥测数据。

后续步骤

  • 详细了解 Application Insights 中的个人数据收集
  • 详细了解 Application Insights 中 IP 地址收集的工作原理。 本文是由我们的某位工程师早前撰写的一篇外部博客文章。 其中所述的机制不同于现在的默认行为(现在 IP 地址将记录为 0.0.0.0)。 本文更深入地介绍了内置遥测初始化表达式的机制。