Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
11 changes: 9 additions & 2 deletions samples/Sentry.Samples.OpenTelemetry.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

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 +27,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 to send traces to Sentry
.AddSentry(dsn)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is unfortunately a bit more complex for SDK users to initialise because the TracerProviderBuilder.AddOtlpExporter method has no override with an IServiceProvider parameter (so there's no way to read the DSN from the sentry options that get added to the service registry later on).

);

builder.WebHost.UseSentry(options =>
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.Instrumenter == Instrumenter.OpenTelemetry)
{
options.LogWarning("Skipping CommunityToolkit.Mvvm integration since OpenTelemetry instrumentation 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
24 changes: 24 additions & 0 deletions src/Sentry.OpenTelemetry/OtelPropagationContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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();

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" />
</ItemGroup>

<ItemGroup>
Expand Down
78 changes: 36 additions & 42 deletions src/Sentry.OpenTelemetry/SentryOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,44 @@ namespace Sentry.OpenTelemetry;
/// </summary>
public static class SentryOptionsExtensions
{
/// <summary>
/// Enables OpenTelemetry instrumentation with Sentry
/// </summary>
/// <param name="options"><see cref="SentryOptions"/> instance</param>
/// <param name="traceProviderBuilder"><see cref="TracerProviderBuilder"/></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>
public static void UseOpenTelemetry(
this SentryOptions options,
TracerProviderBuilder traceProviderBuilder,
TextMapPropagator? defaultTextMapPropagator = null
)
extension(SentryOptions options)
{
options.Instrumenter = Instrumenter.OpenTelemetry;
options.AddTransactionProcessor(
new OpenTelemetryTransactionProcessor()
);

traceProviderBuilder.AddSentry(defaultTextMapPropagator);
}
/// <summary>
/// Enables OpenTelemetry instrumentation with Sentry
/// </summary>
/// <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 void UseOpenTelemetry(TracerProviderBuilder builder, TextMapPropagator? textMapPropagator = null)
{
options.UseOpenTelemetry();
builder.AddSentry(textMapPropagator);
}

/// <summary>
/// <para>Configures Sentry to use OpenTelemetry for distributed tracing.</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.
/// </para>
/// </summary>
/// <param name="options"><see cref="SentryOptions"/> instance</param>
public static void UseOpenTelemetry(this SentryOptions options)
{
options.Instrumenter = Instrumenter.OpenTelemetry;
options.AddTransactionProcessor(
new OpenTelemetryTransactionProcessor()
/// <summary>
/// <para>Configures Sentry to use OpenTelemetry for distributed tracing.</para>
/// <para>
/// Note: if you are using this method 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>
public void UseOpenTelemetry()
{
options.Instrumenter = Instrumenter.OpenTelemetry;
options.PropagationContextFactory = _ => new OtelPropagationContext();
options.AddTransactionProcessor(
new OpenTelemetryTransactionProcessor()
);
}
}
}
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
88 changes: 68 additions & 20 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,27 +12,74 @@ namespace Sentry.OpenTelemetry;
/// </summary>
public static class TracerProviderBuilderExtensions
{
/// <summary>
/// Ensures OpenTelemetry trace information is sent to Sentry.
/// </summary>
/// <param name="tracerProviderBuilder"><see cref="TracerProviderBuilder"/>.</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 AddSentry(this TracerProviderBuilder tracerProviderBuilder, TextMapPropagator? defaultTextMapPropagator = null)
extension(TracerProviderBuilder tracerProviderBuilder)
{
defaultTextMapPropagator ??= new SentryPropagator();
Sdk.SetDefaultTextMapPropagator(defaultTextMapPropagator);
return tracerProviderBuilder.AddProcessor(ImplementationFactory);
/// <summary>
/// Ensures OpenTelemetry trace information is sent to Sentry.
/// </summary>
/// <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 TracerProviderBuilder AddSentry(TextMapPropagator? defaultTextMapPropagator = null)
{
defaultTextMapPropagator ??= new SentryPropagator();
Sdk.SetDefaultTextMapPropagator(defaultTextMapPropagator);
return tracerProviderBuilder.AddProcessor(ImplementationFactory);
}

/// <summary>
/// Ensures OpenTelemetry trace information is sent to the Sentry OTLP endpoint.
/// </summary>
/// <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 TracerProviderBuilder AddSentry(string dsnString, TextMapPropagator? defaultTextMapPropagator = null)
{
if (string.IsNullOrWhiteSpace(dsnString))
{
throw new ArgumentException("OTLP endpoint must be provided", nameof(dsnString));
}

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

if (Dsn.TryParse(dsnString) is not { } dsn)
{
return tracerProviderBuilder;
}

tracerProviderBuilder.AddOtlpExporter(options =>
{
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;
};
});
return tracerProviderBuilder.AddProcessor(ImplementationFactory);
}
}

internal static BaseProcessor<Activity> ImplementationFactory(IServiceProvider services)
Expand Down
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
5 changes: 2 additions & 3 deletions src/Sentry/DynamicSamplingContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Sentry.Internal;
using Sentry.Internal.Extensions;

namespace Sentry;

Expand Down Expand Up @@ -228,7 +227,7 @@ public static DynamicSamplingContext CreateFromUnsampledTransaction(UnsampledTra
replaySession);
}

public static DynamicSamplingContext CreateFromPropagationContext(SentryPropagationContext propagationContext, SentryOptions options, IReplaySession? replaySession)
public static DynamicSamplingContext CreateFromPropagationContext(IPropagationContext propagationContext, SentryOptions options, IReplaySession? replaySession)
{
var traceId = propagationContext.TraceId;
var publicKey = options.ParsedDsn.PublicKey;
Expand Down Expand Up @@ -257,6 +256,6 @@ public static DynamicSamplingContext CreateDynamicSamplingContext(this Transacti
public static DynamicSamplingContext CreateDynamicSamplingContext(this UnsampledTransaction transaction, SentryOptions options, IReplaySession? replaySession)
=> DynamicSamplingContext.CreateFromUnsampledTransaction(transaction, options, replaySession);

public static DynamicSamplingContext CreateDynamicSamplingContext(this SentryPropagationContext propagationContext, SentryOptions options, IReplaySession? replaySession)
public static DynamicSamplingContext CreateDynamicSamplingContext(this IPropagationContext propagationContext, SentryOptions options, IReplaySession? replaySession)
=> DynamicSamplingContext.CreateFromPropagationContext(propagationContext, options, replaySession);
}
8 changes: 8 additions & 0 deletions src/Sentry/Integrations/ISdkIntegration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@ public interface ISdkIntegration
/// <param name="options">The options.</param>
public void Register(IHub hub, SentryOptions options);
}

/// <summary>
/// Marker interface to indicate that an integration provides native Sentry tracing capabilities. We do NOT initialise
/// these integrations when using OTEL instrumentation.
/// </summary>
internal interface ISentryTracingIntegration
{
}
9 changes: 8 additions & 1 deletion src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ internal ITransactionTracer StartTransaction(
return NoOpTransaction.Instance;
}

if (_options.Instrumenter == Instrumenter.OpenTelemetry)
{
_options.LogWarning("This transaction will not be sent to Sentry. " +
"Please instrument traces using the OpenTelemetry APIs when using Sentry's OpenTelemetry integration.");
return NoOpTransaction.Instance;
}

bool? isSampled = null;
double? sampleRate = null;
DiscardReason? discardReason = null;
Expand Down Expand Up @@ -478,7 +485,7 @@ private void ApplyTraceContextToEvent(SentryEvent evt, ISpan span)
}
}

private void ApplyTraceContextToEvent(SentryEvent evt, SentryPropagationContext propagationContext)
private void ApplyTraceContextToEvent(SentryEvent evt, IPropagationContext propagationContext)
{
evt.Contexts.Trace.TraceId = propagationContext.TraceId;
evt.Contexts.Trace.SpanId = propagationContext.SpanId;
Expand Down
Loading
Loading