用于 .NET、Java、Node.js 和 Python 应用程序的 Azure Monitor OpenTelemetry 筛选

使用本指南筛选 Azure Monitor Application Insights 中的 OpenTelemetry (OTel) 数据。 筛选有助于排除不必要的遥测数据,并防止收集敏感数据以优化性能和支持合规性。

对遥测进行筛选的原因包括:

  • 筛出运行状况检查遥测,以减少噪音。
  • 确保不会收集个人数据和凭据。
  • 筛出低价值的遥测以优化性能。

若要了解有关 OpenTelemetry 概念的详细信息,请查看 OpenTelemetry 概述

注意

有关 Azure 函数应用程序,请参阅 在 Azure Functions 中使用 OpenTelemetry

使用检测库筛选 OpenTelemetry

要查看 Azure Monitor OpenTelemetry 发行版中包含的所有工具库列表,请参阅 配置 Azure Monitor OpenTelemetry 的自动数据收集和资源检测器

许多检测库都提供筛选选项。 有关指导,请查看相应的 README 文件:

注意

目前,Azure Monitor OpenTelemetry 发行版包含 SqlClient 检测源代码的副本,以确保在上游 OpenTelemetry SqlClient 库保持试验性的同时保持稳定性。

此类 SetDbStatementForStoredProcedure 选项在我们的分发中不可用,因为代码是嵌入的,而不是引用外部包。

SqlClient 监控工具达到稳定版本后,Azure Monitor 会切换至引用官方包,并通过 builder.AddSqlClientInstrumentation(options => { ... }) 进行自定义。

使用范围处理器筛选遥测

  1. 使用自定义处理器:

    提示

    添加此处显示的处理器之前添加Azure Monitor。

    // Create an ASP.NET Core application builder.
    var builder = WebApplication.CreateBuilder(args);
    
    // Configure the OpenTelemetry tracer provider to add a new processor named ActivityFilteringProcessor.
    builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.AddProcessor(new ActivityFilteringProcessor()));
    // Configure the OpenTelemetry tracer provider to add a new source named "ActivitySourceName".
    builder.Services.ConfigureOpenTelemetryTracerProvider((sp, builder) => builder.AddSource("ActivitySourceName"));
    // Add the Azure Monitor telemetry service to the application. This service will collect and send telemetry data to Azure Monitor.
    builder.Services.AddOpenTelemetry().UseAzureMonitor();
    
    // Build the ASP.NET Core application.
    var app = builder.Build();
    
    // Start the ASP.NET Core application.
    app.Run();
    
  2. 使用以下代码将 ActivityFilteringProcessor.cs 添加到你的项目:

    public class ActivityFilteringProcessor : BaseProcessor<Activity>
    {
        // The OnStart method is called when an activity is started. This is the ideal place to filter activities.
        public override void OnStart(Activity activity)
        {
            // prevents all exporters from exporting internal activities
            if (activity.Kind == ActivityKind.Internal)
            {
                activity.IsAllDataRequested = false;
            }
        }
    }
    

如果没有使用 AddSource("ActivitySourceName") 显式添加特定源,则不会导出使用该源创建的任何活动。

在引入时使用数据收集规则筛选遥测数据

在Azure Monitor将其存储在Log Analytics工作区之前,请减少干扰或标准化遥测数据。 若要实现这一目标,请使用数据收集规则(DCR)中的引入时间转换在 Azure Monitor收到遥测数据后筛选或修改遥测数据。

转换操作使用在每条引入记录上运行的 Kusto 查询语言(KQL)语句。

在 Application Insights 表中使用工作区转换 DCR。 Log Analytics工作区支持一个工作区转换 DCR。 将该工作区的所有转换置于同一 DCR 中。

更新后最多可能需要 60 分钟激活。

使用这些资源了解详细信息:

将 OpenTelemetry 信号映射到Log Analytics表

Application Insights 在这些表中存储常见的 OpenTelemetry (OTel) 信号:

  • AppTraces (日志)
  • AppRequests (传入请求)
  • AppDependencies (传出依赖项)
  • AppExceptions (例外)
  • AppMetrics (指标)

在工作区转换 DCR 中,对每个表使用流名称格式 Microsoft-Table-<TableName> 。 例如,可以使用Microsoft-Table-AppRequests用于AppRequests表。

注意

工作区转换 DCR 适用于引入到 Log Analytics 工作区所选表中的所有数据。 如果多个应用程序共享同一工作区,请使用筛选器来限定每个转换的范围,例如 AppRoleNameResourceGUID

使用工作区转换 DCR 示例

以下示例显示了工作区变换 DCR JSON。 使用 dataFlows 为每个表定义一个转换。 在 DCR 定义中保留 transformKql 一行。

创建工作区转换 DCR 模板

使用此模板创建工作区转换 DCR。 将 <workspace-location><workspace-resource-id> 替换为Log Analytics工作区中的值。

{
  "kind": "WorkspaceTransforms",
  "location": "<workspace-location>",
  "properties": {
    "dataSources": {},
    "destinations": {
      "logAnalytics": [
        {
          "workspaceResourceId": "<workspace-resource-id>",
          "name": "laDest"
        }
      ]
    },
    "dataFlows": [
      {
        "streams": ["Microsoft-Table-AppRequests"],
        "destinations": ["laDest"],
        "transformKql": "source"
      }
    ]
  }
}
通过删除表来删除遥测类型

使用此示例可屏蔽整个遥测类型,例如所有跟踪或所有请求。

为每个要删除的表添加一个数据流。

[
  {
    "streams": ["Microsoft-Table-AppTraces"],
    "destinations": ["laDest"],
    "transformKql": "source | where 1 == 0"
  },
  {
    "streams": ["Microsoft-Table-AppRequests"],
    "destinations": ["laDest"],
    "transformKql": "source | where 1 == 0"
  },
  {
    "streams": ["Microsoft-Table-AppDependencies"],
    "destinations": ["laDest"],
    "transformKql": "source | where 1 == 0"
  },
  {
    "streams": ["Microsoft-Table-AppExceptions"],
    "destinations": ["laDest"],
    "transformKql": "source | where 1 == 0"
  },
  {
    "streams": ["Microsoft-Table-AppMetrics"],
    "destinations": ["laDest"],
    "transformKql": "source | where 1 == 0"
  }
]
删除运行状况检查请求

使用此示例可删除常见运行状况、就绪性和实时性终结点。

{
  "streams": ["Microsoft-Table-AppRequests"],
  "destinations": ["laDest"],
  "transformKql": "source | extend url = tolower(tostring(Url)) | where not(url contains '/health' or url contains '/healthz' or url contains '/ready' or url contains '/readyz' or url contains '/live' or url contains '/livez') | project-away url"
}
仅保留失败的运行状况检查请求

使用此示例可保留失败的运行状况检查,并丢弃成功的运行状况检查。

{
  "streams": ["Microsoft-Table-AppRequests"],
  "destinations": ["laDest"],
  "transformKql": "source | extend url = tolower(tostring(Url)) | where not((url contains '/health' or url contains '/healthz' or url contains '/ready' or url contains '/readyz' or url contains '/live' or url contains '/livez') and Success == true) | project-away url"
}
仅保留失败或缓慢的请求

使用此示例来保留失败或超出延迟阈值的请求。

{
  "streams": ["Microsoft-Table-AppRequests"],
  "destinations": ["laDest"],
  "transformKql": "source | where Success == false or DurationMs >= 1000"
}
仅保留警告及更高级别的日志

使用此示例可保留 SeverityLevelWarning (2)、Error (3) 或 Critical (4) 的跟踪记录。

{
  "streams": ["Microsoft-Table-AppTraces"],
  "destinations": ["laDest"],
  "transformKql": "source | where SeverityLevel >= 2"
}
仅保留失败或缓慢的依赖项

使用此示例可以保留失败或超出延迟阈值的依赖项调用。

{
  "streams": ["Microsoft-Table-AppDependencies"],
  "destinations": ["laDest"],
  "transformKql": "source | where Success == false or DurationMs >= 500"
}
从依赖项数据中删除 SQL 语句

使用此示例删除存储在列中的 Data SQL 语句,同时保留依赖项计时和成功信息。

{
  "streams": ["Microsoft-Table-AppDependencies"],
  "destinations": ["laDest"],
  "transformKql": "source | extend dependencyType = tolower(tostring(DependencyType)) | extend Data = iff(dependencyType == 'sql', '', Data) | project-away dependencyType"
}
丢弃合成流量

使用此示例删除包含值的 SyntheticSource 记录。

{
  "streams": ["Microsoft-Table-AppRequests"],
  "destinations": ["laDest"],
  "transformKql": "source | where isempty(SyntheticSource)"
}
删除取消异常

使用此示例可删除常见的取消异常类型。

{
  "streams": ["Microsoft-Table-AppExceptions"],
  "destinations": ["laDest"],
  "transformKql": "source | where not(ExceptionType contains 'OperationCanceledException' or ExceptionType contains 'TaskCanceledException')"
}
删除客户端 IP 地址和查询字符串

使用此示例删除客户端 IP 地址,并存储不带查询字符串的 URL。

[
  {
    "streams": ["Microsoft-Table-AppRequests"],
    "destinations": ["laDest"],
    "transformKql": "source | extend Url = tostring(split(tostring(Url), '?')[0]) | project-away ClientIP"
  },
  {
    "streams": ["Microsoft-Table-AppDependencies"],
    "destinations": ["laDest"],
    "transformKql": "source | extend Data = tostring(split(tostring(Data), '?')[0]) | project-away ClientIP"
  }
]
将转换范围限定为单个服务

当多个应用程序共享工作区时,使用此示例可将转换范围限定为单个服务。

{
  "streams": ["Microsoft-Table-AppRequests"],
  "destinations": ["laDest"],
  "transformKql": "source | where AppRoleName == '<app-role-name>' | where Success == false or DurationMs >= 1000"
}

故障排除、反馈和支持

提示

以下部分适用于所有 OpenTelemetry 发行版文章。

故障排除

OpenTelemetry 反馈

若要提供反馈,请查看以下内容:

支持

选择所选语言的选项卡,以发现支持选项。

后续步骤