Search code examples
asp.net-coresentry

Why isn't Sentry registering my ASP .NET Core API calls as Transactions?


I have an ASP .NET Core project integrated with Sentry:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
            webBuilder.UseSentry(options =>
            {
                options.Dsn = Environment.GetEnvironmentVariable("SENTRY_DSN");
                options.Debug = true;
            });
        });
}

I can see all exceptions being logged as Issues in Sentry. I cannot see however, any transactions logged, even if I make plenty of REST API calls.

According to Sentry's documentation, this setup should be sufficient.


Solution

  • Sentry's performance monitoring features are opt-in and need to be enabled separately. In addition to calling UseSentry as you showed, you should also do the following to capture transactions:

    • Configure the sample rate by setting the TracesSampleRate in either the call to UseSentry or in your appsettings.json file. For example, options.TracesSampleRate = 1.0 will send traces for 100% of your transactions. This is fine for dev/test, but in production you will likely want to use a much lower rate such as 0.2 (20%).

      • Optionally, you can also set a TracesSampler function that can be used if you have more complex logic for deciding whether to sample or not.
    • Configure automatic instrumentation by calling UseSentryTracing in the Configure section of your Startup class. Add this after UseRouting, which should already be there. This adds Sentry's tracing middleware to the ASP.NET Core pipeline to collect traces from each request and response.

    • If desired, you can add custom instrumentation around other parts of your code, by creating additional transactions and spans.

    Also, just FYI, if you're planning on using the SENTRY_DSN environment variable, then you don't need to also set options.Dsn in code. It will be picked up automatically.