OpenTelemetry Support

Using OpenTelemetry with Sentry Performance.

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 5.12.0
dotnet add package Sentry.OpenTelemetry -v 5.12.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 start tracing, 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.SendDefaultPii = true;
    options.TracesSampleRate = 1.0;
    options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
});

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.

OpenTelemetry in .NET is implemented via the System.Diagnostics.Activity namespace. However, not all of the functionality in that namespace is supported by OpenTelemetry. In particular, it is not recommended that you use the Activity.RecordException or Activity.AddException methods. Using either of these methods will result in valuable information being removed from exceptions before these get captured by Sentry.

Instead you should either log the exceptions (using ILogger) or capture the exceptions directly using SentrySdk.CaptureException.

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.

Was this 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").