Filter Azure Monitor OpenTelemetry for .NET, Java, Node.js, and Python applications

This guide provides instructions on filtering OpenTelemetry (OTel) data within Azure Monitor Application Insights. Implementing filters allows developers to exclude unnecessary telemetry and prevent the collection of sensitive information, ensuring optimized performance and compliance.

Reasons why you might want to filter out telemetry include:

  • Filtering out health check telemetry to reduce noise.
  • Ensuring PII and credentials aren't collected.
  • Filtering out low-value telemetry to optimize performance.

To learn more about OpenTelemetry concepts, see the OpenTelemetry overview or OpenTelemetry FAQ.

Note

For Azure Function Apps, see Use OpenTelemetry with Azure Functions.

Filter OpenTelemetry using instrumentation libraries

For a list of all instrumentation libraries included with the Azure Monitor OpenTelemetry Distro, see Add and modify Azure Monitor OpenTelemetry for .NET, Java, Node.js, and Python applications.

Many instrumentation libraries provide a filter option. For guidance, see the corresponding readme files:

Note

Currently, the Azure Monitor OpenTelemetry Distro includes a copy of the SqlClient instrumentation source code to ensure stability while the upstream OpenTelemetry SqlClient library remains experimental.

Options such as SetDbStatementForStoredProcedure aren't usable in our distribution because the code is embedded and not referencing the external package.

Once the SqlClient instrumentation reaches a stable release, Azure Monitor will switch to referencing the official package and customization via builder.AddSqlClientInstrumentation(options => { ... }) will be available.

Filter telemetry using span processors

  1. Use a custom processor:

    Tip

    Add the processor shown here before adding 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. Add ActivityFilteringProcessor.cs to your project with the following code:

    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;
            }
        }
    }
    

If a particular source isn't explicitly added by using AddSource("ActivitySourceName"), then none of the activities created by using that source are exported.

Next steps