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

注意

对于.NET、Node.js和Python应用程序:

对于Java应用程序,请使用 Azure Monitor OpenTelemetry 发行版。

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

将 Microsoft OpenTelemetry 发行版用于 .NET、Node.js和 Python。 Java继续使用 Azure Monitor OpenTelemetry 发行版。 首先,为你的语言启用该发行版。 初始化 distro 的示例将替换基本初始化;不要为同一信号初始化第二个 distro 或提供方。

对于 Node.js,在加载应用程序库之前启用插桩。 ESM 应用程序必须预加载 Microsoft OpenTelemetry 发行版加载程序。 在运行未在代码中设置它的示例之前,先设置 APPLICATIONINSIGHTS_CONNECTION_STRING。

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

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

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

注意

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

使用检测库筛选 OpenTelemetry

对于每个发行版随附的插桩库,请查看 自动数据收集和资源检测器。

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

注意

适用于 .NET 的 Microsoft OpenTelemetry 发行版包含 SqlClient 检测源代码的副本,而上游库仍保持试验性。

单独安装的 SqlClient 检测程序包上的选项不会配置随附版本。 若要使用该包进行自定义筛选,请禁用 options.Instrumentation.EnableSqlClientInstrumentation = false捆绑的插桩,然后仅注册一次已配置的插桩。

请参阅 Microsoft OpenTelemetry 发行版插桩自定义。

使用范围处理器筛选遥测

在导出程序将其发送到 Application Insights 之前,使用 span 处理器筛选 span。

  1. 使用自定义处理器:

    提示

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

    使用 Span 处理器筛选遥测
    // Import the Microsoft OpenTelemetry Distro and supporting APIs.
    using Microsoft.OpenTelemetry;
    using OpenTelemetry.Trace;
    
    // Create the application builder.
    var builder = WebApplication.CreateBuilder(args);
    
    builder.Services.AddOpenTelemetry()
      .WithTracing(tracing => tracing
    
        // Collect spans from the application activity source.
        .AddSource("ActivitySourceName")
    
        // Run the custom processor before exporting spans.
        .AddProcessor(new ActivityFilteringProcessor()))
    
      // Configure the Microsoft OpenTelemetry Distro.
      .UseMicrosoftOpenTelemetry(options =>
      {
        options.Exporters = ExportTarget.AzureMonitor;
      });
    
    // Build the application with the configured telemetry services.
    var app = builder.Build();
    
    // Run the application and its telemetry providers.
    app.Run();
    
  2. 使用以下代码将 ActivityFilteringProcessor.cs 添加到你的项目:

    using System.Diagnostics;
    using OpenTelemetry;
    
    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 工作区中引入到所选表中的所有数据。 如果多个应用程序共享同一工作区,请使用筛选器来限定每个转换的范围,例如 AppRoleName 或 ResourceGUID。

使用工作区转换 DCR 示例

以下示例显示了将 设置为 的 资源的 JSON。 它们不是完整的 ARM 模板或独立部署命令。 若要创建并关联工作区,请遵循 工作区转换教程。

第一个示例演示 DCR 资源请求正文。 其余示例是用于其 properties.dataFlows 数组的对象或对象数组。 对每个表使用一个转换,并将每个 transformKql 值保持在同一行。

更新现有工作区转换 DCR 时:

  • 如果某个流已以该表为目标,请改为编辑该流的 transformKql,而不是为同一个表再添加第二个流。 将示例与任何现有筛选或删减要求相结合。
  • 仅为没有流的表添加数据流对象。 数组示例包含要添加或更新的单个流;它不会替换整个 properties.dataFlows 数组。
  • 保留其他数据流、目标和资源设置。 laDest 目标位置名称必须与 properties.destinations.logAnalytics 中的工作区目标匹配。

定义工作区转换 DCR 请求正文

此资源正文摘录定义了针对 AppRequests 的未更改数据转换。 它不可独立部署。 将其用作新工作区转换 DCR 的起始资源定义,而不是用作现有 DCR 的替代项。 在 ARM 模板中,将这些字段插入 resources 数组中的 Microsoft.Insights/dataCollectionRules 资源声明内。 保留其type、apiVersion和name字段和任何其他现有资源设置。

{
  "kind": "WorkspaceTransforms",
  "location": "<Location>",
  "properties": {
    "dataSources": {},
    "destinations": {
      "logAnalytics": [
        {
          "workspaceResourceId": "/subscriptions/<SubscriptionId>/resourceGroups/<ResourceGroupName>/providers/Microsoft.OperationalInsights/workspaces/<WorkspaceName>",
          "name": "laDest"
        }
      ]
    },
    "dataFlows": [
      {
        "streams": ["Microsoft-Table-AppRequests"],
        "destinations": ["laDest"],
        "transformKql": "source"
      }
    ]
  }
}
通过删除表来删除遥测类型

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

为要丢弃其传入记录的每个表添加或更新一个对象 properties.dataFlows 。 保留其他表的流程。 这些转换会丢弃传入记录;它们不会删除表或以前存储的数据。

[
  {
    "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"
}
仅保留警告及更高级别的日志

使用此示例可保留 SeverityLevel 为 Warning (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"
  }
]
将转换范围限定为单个服务

当多个应用程序共享工作区时,使用此示例筛选单个服务。 使其他服务的遥测数据保持不变。 将其 transformKql 应用到在 properties.dataFlows 中的 Microsoft-Table-AppRequests 流,保留该流的目标和任何其他筛选要求。

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

故障排除、反馈和支持

提示

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

故障排除

OpenTelemetry 反馈

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

支持

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

后续步骤