OpenTelemetry Support

You can configure your OpenTelemetry SDK to send traces and spans to Sentry.

To install, add the Sentry and Sentry.OpenTelemetry NuGet packages to your project:

Copied
dotnet add package Sentry -v 4.4.0
dotnet add package Sentry.OpenTelemetry -v 4.4.0

If you're building an ASP.NET or ASP.NET Core application, add their respective packages (Sentry.AspNet or Sentry.AspNetCore) as well.

To learn how to start tracing based on your application kind, read the instructions below.

To start tracing in a console application, you'll need to add Sentry to the tracer provider. This will make it possible for OpenTelemetry spans to be captured by Sentry.

Copied
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .AddSource(serviceName) // <-- The name of an activity sources you care about
    .AddSentry() // <-- Configure OpenTelemetry to send traces to Sentry
    .Build();

Next, initialize Sentry and opt into the use of OpenTelemetry. This allows the SDK to send OpenTelemetry spans to Sentry.

Copied
SentrySdk.Init(options =>
{
    // options.Dsn = "... Your DSN ...";
    options.TracesSampleRate = 1.0;
    options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
});

To start tracing in an ASP.NET Core app, add OpenTelemetry with tracing and add Sentry to the tracer provider.

Copied
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddOpenTelemetry()
    .WithTracing(tracerProviderBuilder =>
        tracerProviderBuilder
            .AddAspNetCoreInstrumentation() // <-- Adds ASP.NET Core telemetry sources
            .AddHttpClientInstrumentation() // <-- Adds HttpClient telemetry sources
            .AddSentry() // <-- Configure OpenTelemetry to send trace information to Sentry
    );

Next, initialize Sentry and opt into the use of OpenTelemetry. This allows the SDK to send OpenTelemetry spans to Sentry.

Copied
builder.WebHost.UseSentry(options =>
{
    options.Dsn = "...Your DSN...";
    options.TracesSampleRate = 1.0;
    options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
});

To start tracing in an ASP.NET application, you'll need to create a tracer provider.

Copied
var builder = Sdk.CreateTracerProviderBuilder()
    .AddAspNetInstrumentation(); // <-- Adds ASP.NET telemetry sources

Next, initialize Sentry and opt into the use of OpenTelemetry. Provide the SDK with the builder for OpenTelemetry's tracer provider to allow sending spans to Sentry.

Copied
_sentry = SentrySdk.Init(o =>
{
    //o.Dsn = "...Your DSN...";
    o.TracesSampleRate = 1.0;
    o.AddAspNet(RequestSize.Always);
    o.UseOpenTelemetry(builder);
});

Lastly, build the tracer provider.

Copied
_tracerProvider = builder.Build();

With Sentry’s OpenTelemetry SDK, an OpenTelemetry Span becomes a Sentry Transaction or Span. The first Span sent through the Sentry SpanProcessor is a Transaction, and any child Span gets attached to the first Transaction upon checking the parent Span context. This is true for the OpenTelemetry root Span and any top level Span in the system. For example, a request sent from frontend to backend will create an OpenTelemetry root Span with a corresponding Sentry Transaction. The backend request will create a new Sentry Transaction for the OpenTelemetry Span. The Sentry Transaction and Span are linked as a trace for navigation and error tracking purposes.

If you need more fine grained control over Sentry, take a look at the Configuration page. In case you'd like to filter out transactions before sending them to Sentry (to get rid of health checks, for example), you may find the Filtering page helpful.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").