Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sentry BeforeSend not being triggered in Sentry.AspNetCore (v3.22.0)

I'm trying to use Sentry.AspNetCore (3.22.0) in a AspNetCore Api, but need to filter certain endpoints and events.

I understand the way to do this is to return a null in BeforeSend, however I cannot get this to trigger at all.

I'm initialising Sentry as so:

        await Host.CreateDefaultBuilder(args)
                  .ConfigureWebHostDefaults(builder =>
                  {
                      builder.UseSentry(o =>
                      {
                          o.BeforeSend = sentryEvent =>
                          {
                              Console.WriteLine("SEND");
                              return sentryEvent;
                          };
                      });
                      builder.UseStartup<TStartup>();
                  })

and in the logging:

        services.AddLogging(builder =>
                {
                    builder.ClearProviders();
                    builder.AddNLogWeb(InitialiseNLog());
                    builder.AddSentry(options => {
                        options.BeforeSend = sentryEvent =>
                        {
                            Console.WriteLine("BEFORESEND");
                            return sentryEvent;
                        };
                        });
                    builder.SetMinimumLevel(LogLevel.Trace);
                })

I'm also calling

        app.UseSentryTracing();

Both the calls to .AddSentry are being hit. (They both seem to be required??) However the BeforeSend is always null in the call, so it doesn't appear to being 'persisted'.

Neither call back ever gets called when an endpoint is hit. The output shows sentry sending the envelope, so we know its working.

Any ideas?

like image 483
BJury Avatar asked Oct 19 '25 03:10

BJury


1 Answers

Currently, BeforeSend only fires for error events, not for transactions, sessions, or other envelope types. However, in version 3.21.0 and newer of the Sentry .NET SDK, you can use a transaction processor instead.

For example:

public class MyTransactionProcessor : ISentryTransactionProcessor
{
    public Transaction Process(Transaction transaction)
    {
        // You can access (or modify) the properties of the transaction object.
        Console.WriteLine($"Processing Transaction: {transaction.Name}");

        // Return the transaction if you want it to be sent to Sentry.
        // Return null if you want to filter out the transaction.
        return transaction;
    }
}

Add it to your Sentry options like this:

builder.UseSentry(o =>
{
    o.AddTransactionProcessor(new MyTransactionProcessor());
});

Unlike BeforeSend, you can add as many transaction processors as you like.

Also note that the sampling decision is made before running processors, so if the transaction has been sampled out then the processor will not run. You may want to set o.TracesSampleRate = 1.0 when debugging.

Also, you should only need UseSentry on the web host builder. It already adds the logging extension, so you don't need to call AddSentry on the logging builder. I would suggest deleting builder.ClearProviders(); though, as that would remove it.

like image 177
Matt Johnson-Pint Avatar answered Oct 20 '25 17:10

Matt Johnson-Pint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!