OpenTelemetry Support
You can configure your OpenTelemetry SDK to send traces and spans to Sentry.
Install
The Sentry.OpenTelemetry
package requires OpenTelemetry
package 1.5.0
or higher.
To install, add the Sentry
and Sentry.OpenTelemetry
NuGet packages to your
dotnet add package Sentry -v 3.41.3
dotnet add package Sentry.OpenTelemetry -v 3.41.3
If you're building an ASP.NET or ASP.NET Core application, add their respective packages (Sentry.AspNet
or Sentry.AspNetCore
) as well.
Usage
To learn how to start
Console Applications
To start
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(serviceName)
.ConfigureResource(resource =>
resource.AddService(
serviceName: serviceName,
serviceVersion: serviceVersion))
.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.
SentrySdk.Init(options =>
{
// options.Dsn = "... Your DSN ...";
options.TracesSampleRate = 1.0;
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
});
ASP.NET Core Applications
To start
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTelemetry()
.WithTracing(tracerProviderBuilder =>
tracerProviderBuilder
.AddSource(Telemetry.ActivitySource.Name)
.ConfigureResource(resource => resource.AddService(Telemetry.ServiceName))
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.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.
builder.WebHost.UseSentry(options =>
{
options.Dsn = "...Your DSN...";
options.TracesSampleRate = 1.0;
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
});
ASP.NET Applications
To start
var builder = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation()
.AddSource(Telemetry.ServiceName)
.SetResourceBuilder(
ResourceBuilder.CreateDefault()
.AddService(serviceName: Telemetry.ServiceName, serviceVersion: "1.0.0")
);
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.
_sentry = SentrySdk.Init(o =>
{
//o.Dsn = "...Your DSN...";
o.TracesSampleRate = 1.0;
o.AddAspNet(RequestSize.Always);
o.UseOpenTelemetry(builder);
});
Lastly, build the tracer provider.
_tracerProvider = builder.Build();
OpenTelemetry and Sentry
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.
Additional Configuration
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.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
- Package:
- nuget:Sentry
- Version:
- 3.41.3
- Repository:
- https://github.com/getsentry/sentry-dotnet