Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

var builder = WebApplication.CreateBuilder(args);

// Read the Sentry DSN from the environment variable if it's not already set in code.
#if SENTRY_DSN_DEFINED_IN_ENV
var dsn = Environment.GetEnvironmentVariable("SENTRY_DSN")
?? throw new InvalidOperationException("SENTRY_DSN environment variable is not set");
#else
var dsn = SamplesShared.Dsn;
#endif

// OpenTelemetry Configuration
// See https://opentelemetry.io/docs/instrumentation/net/getting-started/
builder.Services.AddOpenTelemetry()
Expand All @@ -20,8 +28,8 @@
// The two lines below take care of configuring sources for ASP.NET Core and HttpClient
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
// Finally we configure OpenTelemetry to send traces to Sentry
.AddSentry()
// Finally, we configure OpenTelemetry over OTLP to send traces to Sentry
.AddSentryOtlp(dsn)
);

builder.WebHost.UseSentry(options =>
Expand All @@ -35,7 +43,7 @@
options.Debug = builder.Environment.IsDevelopment();
options.SendDefaultPii = true;
options.TracesSampleRate = 1.0;
options.UseOpenTelemetry(); // <-- Configure Sentry to use OpenTelemetry trace information
options.UseOtlp(); // <-- Configure Sentry to use OpenTelemetry trace information
});

builder.Services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without adding an explicit reference to this package, we get an error because the other packages try to load types from a different assembly version. It's the workaround for this problem basically.

</ItemGroup>

<PropertyGroup Condition="'$(Configuration)' == 'Release'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Sentry.Internal.DiagnosticSource;

internal class SentryDiagnosticListenerIntegration : ISdkIntegration
internal class SentryDiagnosticListenerIntegration : ISdkIntegration, ISentryTracingIntegration
{
public void Register(IHub hub, SentryOptions options)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry.EntityFramework/DbInterceptionIntegration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Sentry.EntityFramework;

internal class DbInterceptionIntegration : ISdkIntegration
internal class DbInterceptionIntegration : ISdkIntegration, ISentryTracingIntegration
{
// Internal for testing.
internal IDbInterceptor? SqlInterceptor { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Sentry.Extensibility;
using Sentry.Maui.CommunityToolkit.Mvvm;

namespace Sentry.Maui;
Expand All @@ -12,7 +13,14 @@ public static class SentryOptionsExtensions
/// </summary>
public static SentryMauiOptions AddCommunityToolkitIntegration(this SentryMauiOptions options)
{
options.AddIntegrationEventBinder<MauiCommunityToolkitMvvmEventsBinder>();
if (options.DisableSentryTracing)
{
options.LogWarning("Skipping CommunityToolkit.Mvvm integration because OpenTelemetry is enabled.");
}
else
{
options.AddIntegrationEventBinder<MauiCommunityToolkitMvvmEventsBinder>();
}
return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Sentry.Extensibility;
using Sentry.Internal.OpenTelemetry;

namespace Sentry.OpenTelemetry;

Expand Down
28 changes: 28 additions & 0 deletions src/Sentry.OpenTelemetry/OtelPropagationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Sentry.Extensibility;
using Sentry.Internal;

namespace Sentry.OpenTelemetry;

internal class OtelPropagationContext : IPropagationContext
{
public DynamicSamplingContext? DynamicSamplingContext { get; private set; }

public SentryId TraceId => Activity.Current?.TraceId.AsSentryId() ?? default;
public SpanId SpanId => Activity.Current?.SpanId.AsSentrySpanId() ?? default;
public SpanId? ParentSpanId => Activity.Current?.ParentSpanId.AsSentrySpanId();

/// <summary>
/// Warning: this method may throw an exception if Activity.Current is null.
/// This method should not be used when instrumenting with OTEL.
/// </summary>
public DynamicSamplingContext GetOrCreateDynamicSamplingContext(SentryOptions options, IReplaySession replaySession)
{
if (DynamicSamplingContext is null)
{
options.LogDebug("Creating the Dynamic Sampling Context from the Propagation Context.");
DynamicSamplingContext = this.CreateDynamicSamplingContext(options, replaySession);
}

return DynamicSamplingContext;
}
}
4 changes: 3 additions & 1 deletion src/Sentry.OpenTelemetry/Sentry.OpenTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<PackageTags>$(PackageTags);OpenTelemetry</PackageTags>
<TargetFrameworks>$(CurrentTfms);netstandard2.1;netstandard2.0;net462</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Todo: work out a proper solution to this bug in the Roslyn analysers -->
<NoWarn>$(NoWarn);AD0001</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(EnableAot)' == 'true'">
Expand All @@ -17,7 +19,7 @@

<ItemGroup>
<!-- Version 1.6.0 is the minimum version that does not have trim warnings -->
<PackageReference Include="OpenTelemetry" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.6.0" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next major: consider bumping to >= 1.11.0, as the dependency graph looks cleaner

</ItemGroup>

<ItemGroup>
Expand Down
104 changes: 84 additions & 20 deletions src/Sentry.OpenTelemetry/SentryOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace Sentry.OpenTelemetry;
public static class SentryOptionsExtensions
{
/// <summary>
/// Enables OpenTelemetry instrumentation with Sentry
/// Configures Sentry to use OpenTelemetry for distributed tracing.
/// </summary>
/// <param name="options"><see cref="SentryOptions"/> instance</param>
/// <param name="traceProviderBuilder"><see cref="TracerProviderBuilder"/></param>
/// <param name="defaultTextMapPropagator">
/// <param name="options">The <see cref="SentryOptions"/> instance.</param>
/// <param name="builder"><see cref="TracerProviderBuilder"/></param>
/// <param name="textMapPropagator">
/// <para>The default TextMapPropagator to be used by OpenTelemetry.</para>
/// <para>
/// If this parameter is not supplied, the <see cref="SentryPropagator"/> will be used, which propagates the
Expand All @@ -24,34 +24,98 @@ public static class SentryOptionsExtensions
/// could wrap this in a <see cref="CompositeTextMapPropagator"/> if you needed other propagators as well.
/// </para>
/// </param>
public static void UseOpenTelemetry(
this SentryOptions options,
TracerProviderBuilder traceProviderBuilder,
TextMapPropagator? defaultTextMapPropagator = null
)
/// <param name="disableSentryTracing">Whether to disable traces created using Sentry's tracing instrumentation.
/// It's recommended that you set this to <c>true</c> since mixing OpenTelemetry and Sentry traces may yield
/// unexpected results. It is <c>false</c> by default for backward compatibility only.
/// </param>
/// <remarks>
/// This method of initialising the Sentry OpenTelemetry integration will be depricated in a future major release.
/// We recommend you use <see cref="UseOtlp(Sentry.SentryOptions,TracerProviderBuilder,TextMapPropagator?)"/> instead.
/// </remarks>
public static void UseOpenTelemetry(this SentryOptions options, TracerProviderBuilder builder,
TextMapPropagator? textMapPropagator = null, bool disableSentryTracing = false)
{
options.UseOpenTelemetry(disableSentryTracing);
builder.AddSentry(textMapPropagator);
}

/// <summary>
/// <para>Configures Sentry to use OpenTelemetry for distributed tracing.
/// </para>
/// <para>
/// Note: if you are using this overload to configure Sentry to work with OpenTelemetry, you will also have to call
/// <see cref="O:TracerProviderBuilderExtensions.AddSentry"/> when building your <see cref="TracerProviderBuilder"/>
/// to ensure OpenTelemetry sends trace information to Sentry.
/// </para>
/// </summary>
/// <param name="options">The <see cref="SentryOptions"/> instance.</param>
/// <param name="disableSentryTracing">Whether to disable traces created using Sentry's tracing instrumentation.
/// It's recommended that you set this to <c>true</c> since mixing OpenTelemetry and Sentry traces may yield
/// unexpected results. It is <c>false</c> by default for backward compatibility only.
/// </param>
/// <remarks>
/// This method of initialising the Sentry OpenTelemetry integration will be depricated in a future major release.
/// We recommend you use <see cref="UseOtlp(Sentry.SentryOptions,TracerProviderBuilder,TextMapPropagator?)"/> instead.
/// </remarks>
public static void UseOpenTelemetry(this SentryOptions options, bool disableSentryTracing = false)
{
options.Instrumenter = Instrumenter.OpenTelemetry;
options.DisableSentryTracing = disableSentryTracing;
options.PropagationContextFactory = _ => new OtelPropagationContext();
options.AddTransactionProcessor(
new OpenTelemetryTransactionProcessor()
);
);
}

traceProviderBuilder.AddSentry(defaultTextMapPropagator);
/// <summary>
/// <para>Configures Sentry to use OpenTelemetry for distributed tracing. Sentry instrumented traces will be
/// disabled (so all tracing instrumentation must be done using the OpenTelemetry <see cref="Activity"/> classes).
/// </para>
/// <para>
/// This is the recommended way to set up Sentry's OpenTelemetry integration.
/// </para>
/// </summary>
/// <param name="options">The <see cref="SentryOptions"/> instance.</param>
/// <param name="builder"><see cref="TracerProviderBuilder"/></param>
/// <param name="textMapPropagator">
/// <para>The default TextMapPropagator to be used by OpenTelemetry.</para>
/// <para>
/// If this parameter is not supplied, the <see cref="SentryPropagator"/> will be used, which propagates the
/// baggage header as well as Sentry trace headers.
/// </para>
/// <para>
/// The <see cref="SentryPropagator"/> is required for Sentry's OpenTelemetry integration to work but you
/// could wrap this in a <see cref="CompositeTextMapPropagator"/> if you needed other propagators as well.
/// </para>
/// </param>
public static void UseOtlp(this SentryOptions options, TracerProviderBuilder builder, TextMapPropagator? textMapPropagator = null)
{
if (string.IsNullOrWhiteSpace(options.Dsn))
{
throw new ArgumentException("Sentry DSN must be set before calling `SentryOptions.UseOTLP`", nameof(options.Dsn));
}
builder.AddSentryOtlp(options.Dsn, textMapPropagator);
options.UseOtlp();
}

/// <summary>
/// <para>Configures Sentry to use OpenTelemetry for distributed tracing.</para>
/// <para>Configures Sentry to use OpenTelemetry for distributed tracing. Sentry instrumented traces will be
/// disabled (so all tracing instrumentation must be done using the OpenTelemetry <see cref="Activity"/> classes).
/// </para>
/// <para>
/// Note: if you are using this method to configure Sentry to work with OpenTelemetry you will also have to call
/// <see cref="TracerProviderBuilderExtensions.AddSentry"/> when building your <see cref="TracerProviderBuilder"/>
/// to ensure OpenTelemetry sends trace information to Sentry.
/// This is the recommended way to set up Sentry's OpenTelemetry integration.
/// </para>
/// </summary>
/// <param name="options"><see cref="SentryOptions"/> instance</param>
public static void UseOpenTelemetry(this SentryOptions options)
/// <remarks>
/// Note: if you are using this overload to configure Sentry to work with OpenTelemetry, you will also have to call
/// <see cref="TracerProviderBuilderExtensions.AddSentryOtlp"/>, when building your <see cref="TracerProviderBuilder"/>
/// to ensure OpenTelemetry sends trace information to Sentry.
/// </remarks>
/// <param name="options">The <see cref="SentryOptions"/> instance.</param>
public static void UseOtlp(this SentryOptions options)
{
options.Instrumenter = Instrumenter.OpenTelemetry;
options.AddTransactionProcessor(
new OpenTelemetryTransactionProcessor()
);
options.DisableSentryTracing = true;
options.PropagationContextFactory = _ => new OtelPropagationContext();
}
}
3 changes: 2 additions & 1 deletion src/Sentry.OpenTelemetry/SentryPropagator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OpenTelemetry;
using OpenTelemetry.Context.Propagation;
using Sentry.Extensibility;
using Sentry.Internal.OpenTelemetry;

namespace Sentry.OpenTelemetry;

Expand Down Expand Up @@ -54,7 +55,7 @@ public override PropagationContext Extract<T>(PropagationContext context, T carr
Options?.LogDebug("SentryPropagator.Extract");

var result = base.Extract(context, carrier, getter);
var baggage = result.Baggage; // The Otel .NET SDK takes care of baggage headers alread
var baggage = result.Baggage; // The Otel .NET SDK takes care of baggage headers already

Options?.LogDebug("Baggage");
foreach (var entry in baggage)
Expand Down
74 changes: 70 additions & 4 deletions src/Sentry.OpenTelemetry/TracerProviderBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Exporter;
using OpenTelemetry.Trace;
using Sentry.Extensibility;

Expand All @@ -11,10 +12,22 @@ namespace Sentry.OpenTelemetry;
/// </summary>
public static class TracerProviderBuilderExtensions
{
internal const string MissingDsnWarning = "Invalid DSN passed to AddSentryOTLP";

/// <summary>
/// Ensures OpenTelemetry trace information is sent to Sentry.
/// <para>
/// Ensures OpenTelemetry trace information is sent to Sentry. OpenTelemetry spans will be converted to Sentry spans
/// using a span processor. This is no longer recommended. SDK users should consider using
/// <see cref="AddSentryOtlp"/> instead, which is the recommended
/// way to send OpenTelemetry trace information to Sentry moving forward.
/// </para>
/// <para>
/// Note that if you use this method to configure the trace builder, you will also need to call
/// <see cref="SentryOptionsExtensions.UseOpenTelemetry(SentryOptions, bool)"/> when initialising Sentry, for Sentry
/// to work properly with OpenTelemetry.
/// </para>
/// </summary>
/// <param name="tracerProviderBuilder"><see cref="TracerProviderBuilder"/>.</param>
/// <param name="tracerProviderBuilder">The <see cref="TracerProviderBuilder"/>.</param>
/// <param name="defaultTextMapPropagator">
/// <para>The default TextMapPropagator to be used by OpenTelemetry.</para>
/// <para>
Expand All @@ -27,7 +40,8 @@ public static class TracerProviderBuilderExtensions
/// </para>
/// </param>
/// <returns>The supplied <see cref="TracerProviderBuilder"/> for chaining.</returns>
public static TracerProviderBuilder AddSentry(this TracerProviderBuilder tracerProviderBuilder, TextMapPropagator? defaultTextMapPropagator = null)
public static TracerProviderBuilder AddSentry(this TracerProviderBuilder tracerProviderBuilder,
TextMapPropagator? defaultTextMapPropagator = null)
{
defaultTextMapPropagator ??= new SentryPropagator();
Sdk.SetDefaultTextMapPropagator(defaultTextMapPropagator);
Expand All @@ -36,7 +50,7 @@ public static TracerProviderBuilder AddSentry(this TracerProviderBuilder tracerP

internal static BaseProcessor<Activity> ImplementationFactory(IServiceProvider services)
{
List<IOpenTelemetryEnricher> enrichers = new();
List<IOpenTelemetryEnricher> enrichers = [];

// AspNetCoreEnricher
var userFactory = services.GetService<ISentryUserFactory>();
Expand All @@ -55,4 +69,56 @@ internal static BaseProcessor<Activity> ImplementationFactory(IServiceProvider s
logger?.LogWarning("Sentry is disabled so no OpenTelemetry spans will be sent to Sentry.");
return DisabledSpanProcessor.Instance;
}

/// <summary>
/// <para>
/// Ensures OpenTelemetry trace information is sent to the Sentry OTLP endpoint.
/// </para>
/// <para>
/// Note that if you use this method to configure the trace builder, you will also need to call
/// <see cref="SentryOptionsExtensions.UseOtlp(Sentry.SentryOptions)"/> when initialising Sentry, for Sentry to work
/// properly with OpenTelemetry.
/// </para>
/// </summary>
/// <param name="tracerProviderBuilder">The <see cref="TracerProviderBuilder"/>.</param>
/// <param name="dsnString">The DSN for your Sentry project</param>
/// <param name="defaultTextMapPropagator">
/// <para>The default TextMapPropagator to be used by OpenTelemetry.</para>
/// <para>
/// If this parameter is not supplied, the <see cref="SentryPropagator"/> will be used, which propagates the
/// baggage header as well as Sentry trace headers.
/// </para>
/// <para>
/// The <see cref="SentryPropagator"/> is required for Sentry's OpenTelemetry integration to work but you
/// could wrap this in a <see cref="CompositeTextMapPropagator"/> if you needed other propagators as well.
/// </para>
/// </param>
/// <returns>The supplied <see cref="TracerProviderBuilder"/> for chaining.</returns>
public static TracerProviderBuilder AddSentryOtlp(this TracerProviderBuilder tracerProviderBuilder, string dsnString,
TextMapPropagator? defaultTextMapPropagator = null)
{
if (Dsn.TryParse(dsnString) is not { } dsn)
{
throw new ArgumentException("Invalid DSN passed to AddSentryOTLP", nameof(dsnString));
}

defaultTextMapPropagator ??= new SentryPropagator();
Sdk.SetDefaultTextMapPropagator(defaultTextMapPropagator);

tracerProviderBuilder.AddOtlpExporter(options => OtlpConfigurationCallback(options, dsn));
return tracerProviderBuilder;
}

// Internal helper method for testing purposes
internal static void OtlpConfigurationCallback(OtlpExporterOptions options, Dsn dsn)
{
options.Endpoint = dsn.GetOtlpTracesEndpointUri();
options.Protocol = OtlpExportProtocol.HttpProtobuf;
options.HttpClientFactory = () =>
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Sentry-Auth", $"sentry sentry_key={dsn.PublicKey}");
return client;
};
}
}
2 changes: 2 additions & 0 deletions src/Sentry/Dsn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ private Dsn(

public Uri GetEnvelopeEndpointUri() => new(ApiBaseUri, "envelope/");

public Uri GetOtlpTracesEndpointUri() => new(ApiBaseUri, "integration/otlp/v1/traces");

public override string ToString() => Source;

public static bool IsDisabled(string? dsn) =>
Expand Down
Loading
Loading