閱讀英文

共用方式為

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

本指南提供有关在 Azure Monitor Application Insights 中筛选 OpenTelemetry (OTel) 数据的说明。 通过实施筛选器,开发人员可以排除不必要的遥测数据,并防止收集敏感信息,确保优化性能和符合性。

你可能想要筛出遥测的原因包括:

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

若要详细了解 OpenTelemetry 概念,请参阅 OpenTelemetry 概述OpenTelemetry 常见问题解答

使用检测库筛选 OpenTelemetry

如需 Azure Monitor OpenTelemetry 发行版中包含的所有检测库的列表,请参阅添加和修改适用于 .NET、Java、Node.js 和 Python 应用程序的 Azure Monitor OpenTelemetry

许多检测库都提供筛选选项。 有关指南,请参阅对应的README文件:

1 在包中提供 SqlClient 检测,尽管它仍处于 beta 版。 当它成为稳定版时,我们会将其作为标准包引用而添加。 在此期间,若要自定义 SQLClient 仪表,请将 OpenTelemetry.Instrumentation.SqlClient 包引用添加到项目中并使用其公共 API。

dotnet add package --prerelease OpenTelemetry.Instrumentation.SqlClient

builder.Services.AddOpenTelemetry().UseAzureMonitor().WithTracing(builder =>
{
    builder.AddSqlClientInstrumentation(options =>
    {
        options.SetDbStatementForStoredProcedure = false;
    });
});

使用范围处理器筛选遥测

  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") 显式添加特定源,则不会导出使用该源创建的任何活动。

后续步骤