Skip to content

Filtering topmost Activities via Processor is not respected by Sentry Exporter #3887

Open
@CezaryKlus

Description

Package

Sentry

.NET Flavor

.NET

.NET Version

8.0.0

OS

Linux

SDK Version

5.0.1

Self-Hosted Sentry Version

No response

Steps to Reproduce

  1. Add filtering Processor, here an example of filtering out activities that do not belong to an ancestor that is an incoming Request or Message Consumer, or Hangfire Job (most often topmost)
internal sealed class FilteringProcessor : BaseProcessor<Activity>
{
    private static bool HasParent(Activity activity, Func<Activity, bool> condition)
    {
        Activity? current = activity;

        while (current != null)
        {
            if (condition(current))
            {
                return true;
            }
            current = current.Parent;
        }

        return false;
    }

    // Called when an activity ends
    // Filters out activities that are not coming from ASP.NET Core request, MassTransit or Hangfire job
    public override void OnEnd(Activity activity)
    {
        var activityKinds = new ActivityKind[] { ActivityKind.Server, ActivityKind.Consumer };

        // If the activity does not have a parent that matches the specified kinds or tags, mark it as not recorded
        if (!HasParent(activity, a =>
            activityKinds.Any(y => a.Kind == y)
            || a.Tags.Any(y => y.Key == "job.id")))
        {
            Console.WriteLine($"Filtering out activity: {activity.OperationName} {activity.DisplayName}");
            activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
            activity.IsAllDataRequested = false;
        }
    }
}
  1. Register filtering Processor
 services.AddOpenTelemetry()
     .ConfigureResource(resource => resource
         .AddService(serviceName: ctx.HostingEnvironment.ApplicationName, serviceVersion: ServiceVersion.FromTypeAssembly<TStartup>())
         .AddHostDetector()
         )
     .WithTracing(tracerProviderBuilder =>
         tracerProviderBuilder
             .AddAspNetCoreInstrumentation(o =>
             {
                 // filter out health checks
                 o.Filter = (httpContext) => !httpContext.Request.Path.ToString().Contains(".probe", StringComparison.InvariantCultureIgnoreCase);
                 o.RecordException = true;
             }) // <-- Adds ASP.NET Core telemetry sources
             .AddHttpClientInstrumentation(
                 // https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md#filtering-out-duplicated-http-client-activities
                 o =>
                 {
                     o.FilterHttpRequestMessage = (_) => Activity.Current?.Parent?.Source?.Name != "Azure.Core.Http";
                     o.RecordException = true;
                 }) // <-- Adds HttpClient telemetry sources
             .AddSqlClientInstrumentation(o =>
             {
                 o.SetDbStatementForText = true;
                 o.RecordException = true;
             }) // <-- Adds SQL Client telemetry sources
             .AddHangfireInstrumentation(o =>
             {
                 o.RecordException = true;
             }) // https://github.com/open-telemetry/opentelemetry-dotnet-contrib/tree/main/src/OpenTelemetry.Instrumentation.Hangfire
             .AddSource("Azure.*") // <-- Adds Azure telemetry sources https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/Azure.Core/samples/Diagnostics.md#opentelemetry-configuration
             .AddSource(MassTransit.Logging.DiagnosticHeaders.DefaultListenerName) // https://masstransit.io/documentation/configuration/observability#aspnet-core-application


             .AddProcessor(new FilteringProcessor()) // <-- Adds a processor to filter out unwanted activities
             .AddSentry() // <-- Configure OpenTelemetry to send trace information to Sentry
     );
  1. Observe Filtering out activity debug messages in the console
  2. Observe Sentry still reports these topmost Activities

Expected Result

Sentry Exporter respects the ActivityTraceFlags and is not transmitting topmost Activities that are flagged as not Recorded

Actual Result

Sentry still reports these filtered Activities

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't working

    Type

    No type

    Projects

    • Status

      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions