OpenTelemetry (OTLP) for MAUI
Learn about using OTLP to automatically send OpenTelemetry Traces to Sentry using the .NET SDK.
The OTLP exporter configures the Sentry SDK to automatically send trace data instrumented by an OpenTelemetry SDK to Sentry's OpenTelemetry Protocol ingestion endpoint.
Add the Sentry.OpenTelemetry.Exporter NuGet package to your project:
dotnet add package Sentry.OpenTelemetry.Exporter -v 7.0.1
dotnet add package Sentry.OpenTelemetry.Exporter -v 7.0.1
Install-Package Sentry.OpenTelemetry.Exporter -Version 7.0.1
paket add Sentry.OpenTelemetry.Exporter --version 7.0.1
This package includes the standard OpenTelemetry OTLP exporter as a transitive dependency.
To instrument outgoing HTTP requests using OpenTelemetry, also install OpenTelemetry.Instrumentation.Http:
dotnet add package OpenTelemetry.Instrumentation.Http
dotnet add package OpenTelemetry.Instrumentation.Http
Install-Package OpenTelemetry.Instrumentation.Http
You need to configure both the OpenTelemetry and Sentry SDKs to get trace data.
Set up a TracerProvider using AddSentryOtlpExporter with your DSN. This automatically configures the OTLP endpoint and authentication headers derived from the DSN.
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyApp") // the name of your ActivitySource
.AddHttpClientInstrumentation()
.AddSentryOtlpExporter("___PUBLIC_DSN___")
.Build();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyApp") // the name of your ActivitySource
.AddHttpClientInstrumentation()
.AddSentryOtlpExporter("___PUBLIC_DSN___")
.Build();
Initialize Sentry and call UseOtlp() to disable Sentry's built-in span creation in favor of OpenTelemetry tracing.
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.UseOtlp();
});
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.UseOtlp();
});
Under the hood, the OTLP exporter sets up the following parts:
- An OTLP exporter that automatically configures the ingestion endpoint from your Sentry DSN. This enables tracing in Sentry.
- Trace/Span linking for all other Sentry events such as Errors, Logs, and Metrics
Calling UseOtlp() on SentryOptions turns off Sentry's automatic span creation from SentryHttpMessageHandler, DiagnosticSource listener, and Entity Framework interception to avoid duplicate spans.
Because Sentry doesn't create transactions of its own when you call UseOtlp(), TracesSampleRate and TracesSampler have no effect. OpenTelemetry decides which spans are exported, so configure sampling on the TracerProvider instead:
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyApp")
.SetSampler(new ParentBasedSampler(new TraceIdRatioBasedSampler(0.25)))
.AddSentryOtlpExporter("___PUBLIC_DSN___")
.Build();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyApp")
.SetSampler(new ParentBasedSampler(new TraceIdRatioBasedSampler(0.25)))
.AddSentryOtlpExporter("___PUBLIC_DSN___")
.Build();
The OTLP exporter doesn't configure an OpenTelemetry propagator, so OpenTelemetry's default applies: it sends and reads W3C traceparent, tracestate, and baggage headers. Traces connect across services that use OpenTelemetry without additional configuration. See Exporters, Event Linking, and Propagation for details.
Changed in Version 7.0.0
Earlier versions of AddSentryOtlpExporter set SentryPropagator as OpenTelemetry's default propagator, and accepted a defaultTextMapPropagator parameter (as did UseOtlp) to use a different one. Version 7.0.0 removed the automatic setup and the parameter. To keep propagating sentry-trace headers, set the propagator yourself as shown below.
Requests sent through SentryHttpMessageHandler also get a sentry-trace header. UseOtlp() stops the handler from creating spans, but not from adding headers. Sentry adds this handler to IHttpClientFactory clients automatically when you initialize Sentry through dependency injection, for example with UseSentry() in ASP.NET Core.
Services that use a Sentry SDK's own tracing, rather than OpenTelemetry, continue traces from sentry-trace and baggage headers. When they receive a request that didn't go through SentryHttpMessageHandler, they start a new trace.
To send a sentry-trace header on every request OpenTelemetry instruments, and to continue traces from incoming sentry-trace headers, add SentryPropagator (included in Sentry.OpenTelemetry.Exporter) next to the W3C propagator when configuring the OpenTelemetry SDK at startup:
using OpenTelemetry;
using OpenTelemetry.Context.Propagation;
using Sentry.OpenTelemetry;
Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new SentryPropagator(),
}));
using OpenTelemetry;
using OpenTelemetry.Context.Propagation;
using Sentry.OpenTelemetry;
Sdk.SetDefaultTextMapPropagator(new CompositeTextMapPropagator(new TextMapPropagator[]
{
new TraceContextPropagator(),
new SentryPropagator(),
}));
If you only need to continue traces from services that call this one, enable propagateTraceparent in their Sentry SDKs instead. They then also send a traceparent header, which OpenTelemetry reads by default. In .NET, that's the PropagateTraceparent option. You don't need it in the service that uses the OTLP exporter, because OpenTelemetry already sends traceparent from there.
If the calling service hasn't made a sampling decision, for example because tracing is turned off there, its traceparent header is marked as not sampled (-00), since W3C trace context can't express an undecided state. OpenTelemetry's parent-based sampler then doesn't record this service's spans for those requests.
AddSentryOtlpExporter accepts the following parameters:
dsnString(string):Your Sentry DSN. The OTLP endpoint and authentication headers are derived from this automatically.
collectorUrl(Uri, optional):URL of your own OpenTelemetry collector. When set, the exporter will send traces to this URL instead of the Sentry OTLP endpoint derived from the DSN.
- .NET: 8.0+ / .NET Standard 2.0+ / .NET Framework 4.6.2+
- Sentry: 6.5.0+
- OpenTelemetry: 1.12.0+
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").