Included Instrumentation

Server Integration

ASP.NET Core integration, once enabled, captures each incoming HTTP request and turns it into a transaction with the help of a custom middleware. To enable tracing on ASP.NET Core, you need to update your pipeline configuration to include a call to UseSentryTracing():

Copied
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Sentry.AspNetCore;

public class Startup
{
    /* ... */

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        // Enable automatic tracing integration.
        // Make sure to put this middleware right after `UseRouting()`.
        app.UseSentryTracing();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

Transaction names follow the pattern <HTTP method> <Route>; for example, a request to the following action will create a transaction named GET /person/{id}:

Copied
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

public class HomeController : Controller
{
    [HttpGet("/person/{id}")]
    public IActionResult Person(string id) { /* ... */ }
}

Transactions created by this integration are automatically added to the current scope. If you want to start your own spans from the automatically created transaction, you can use the following approach:

Copied
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Sentry;

public class HomeController : Controller
{
    private readonly IHub _sentryHub;

    public HomeController(IHub sentryHub) => _sentryHub = sentryHub;

    [HttpGet("/person/{id}")]
    public IActionResult Person(string id)
    {
        var childSpan = _sentryHub.GetSpan()?.StartChild("additional-work");
        try
        {
            // Do the work that gets measured.
            
            childSpan?.Finish(SpanStatus.Ok);
        }
        catch (Exception e)
        {
            childSpan?.Finish(SpanStatus.InternalError);
            throw;
        }
    }
}

HTTP Client Integration

Sentry also provides a custom filter for HTTP client factory commonly used in ASP.NET Core applications. This filter adds an additional message handler that automatically injects Sentry's trace header and tracks outgoing HTTP requests in separate spans attached to the current transaction.

To enable this integration, you need to have HTTP client factory enabled within your application:

Copied
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        /* ... */

        services.AddRouting();

        // This is required for HTTP client integration
        services.AddHttpClient();
    }
}

If you already have AddHttpClient() in your service collection (which is likely the case), then no action is required.

You can edit this page on GitHub.