Skip to content
Open
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
21 changes: 7 additions & 14 deletions src/Sentry.OpenTelemetry.Exporter/SentryOptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;
using Sentry.OpenTelemetry.Exporter;

Expand All @@ -21,24 +20,18 @@ public static class SentryOptionsExtensions
/// <param name="tracerProviderBuilder"><see cref="TracerProviderBuilder"/></param>
/// <param name="collectorUrl">A custom endpoint to export OLTP trace information to. If no url is provided, the
/// endpoint will be inferred automatically from the DSN.</param>
/// <param name="defaultTextMapPropagator">
/// <para>The default TextMapPropagator to be used by OpenTelemetry.</para>
/// <para>
/// If this parameter is not supplied, the <see cref="OpenTelemetry.SentryPropagator"/> will be used, which propagates the
/// baggage header as well as Sentry trace headers.
/// </para>
/// <para>
/// The <see cref="OpenTelemetry.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 tracerProviderBuilder, Uri? collectorUrl = null, TextMapPropagator? defaultTextMapPropagator = null)
/// <remarks>
/// This method does not configure an OpenTelemetry propagator.
/// Cross-service trace propagation should be enabled via the OpenTelemetry SDK (e.g. by calling
/// <c>Sdk.SetDefaultTextMapPropagator</c>).
/// </remarks>
Comment thread
jamescrosswell marked this conversation as resolved.
public static void UseOtlp(this SentryOptions options, TracerProviderBuilder tracerProviderBuilder, Uri? collectorUrl = null)
{
if (string.IsNullOrWhiteSpace(options.Dsn))
{
throw new ArgumentException("Sentry DSN must be set before calling `SentryOptions.UseOtlp`", nameof(options.Dsn));
}
tracerProviderBuilder.AddSentryOtlpExporter(options.Dsn, collectorUrl, defaultTextMapPropagator);
tracerProviderBuilder.AddSentryOtlpExporter(options.Dsn, collectorUrl);
options.UseOtlp();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Exporter;
using Sentry;
using Sentry.OpenTelemetry;

// ReSharper disable once CheckNamespace -- Discoverability
namespace OpenTelemetry.Trace;
Expand All @@ -27,20 +25,14 @@ public static class SentryTracerProviderBuilderExtensions
/// <param name="dsnString">The DSN for your Sentry project</param>
/// <param name="collectorUrl">A custom endpoint to export OLTP trace information to. If no url is provided, the
/// endpoint will be inferred automatically from the DSN.</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>
/// <remarks>
/// This method does not configure an OpenTelemetry propagator.
/// Cross-service trace propagation should be enabled via the OpenTelemetry SDK (e.g. by calling
/// <c>Sdk.SetDefaultTextMapPropagator</c>).
/// </remarks>
Comment thread
jamescrosswell marked this conversation as resolved.
public static TracerProviderBuilder AddSentryOtlpExporter(this TracerProviderBuilder tracerProviderBuilder,
string dsnString, Uri? collectorUrl = null, TextMapPropagator? defaultTextMapPropagator = null)
string dsnString, Uri? collectorUrl = null)
{
if (Dsn.IsDisabled(dsnString))
{
Expand All @@ -52,9 +44,6 @@ public static TracerProviderBuilder AddSentryOtlpExporter(this TracerProviderBui
throw new ArgumentException(MissingDsnWarning, nameof(dsnString));
}

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

collectorUrl ??= dsn.GetOtlpTracesEndpointUri();
tracerProviderBuilder.AddOtlpExporter(options => OtlpConfigurationCallback(options, collectorUrl, dsn.PublicKey));
return tracerProviderBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;

namespace Sentry.OpenTelemetry.Exporter.Tests;
Expand Down Expand Up @@ -119,19 +118,4 @@ public void UseOtlp_WithTracerProviderBuilder_WithCustomCollectorUrl_DoesNotThro
// Assert
act.Should().NotThrow();
}

[Fact]
public void UseOtlp_WithCustomPropagator_DoesNotThrow()
{
// Arrange
var options = new SentryOptions { Dsn = DsnSamples.ValidDsn };
var tracerProviderBuilder = Substitute.For<TracerProviderBuilder>();
var customPropagator = Substitute.For<TextMapPropagator>();

// Act
var act = () => options.UseOtlp(tracerProviderBuilder, defaultTextMapPropagator: customPropagator);

// Assert
act.Should().NotThrow();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using OpenTelemetry;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Exporter;
using OpenTelemetry.Trace;

Expand Down Expand Up @@ -35,6 +37,24 @@ public void AddSentryOtlpExporter_DisabledSdkDsn_ReturnsWithoutConfiguringExport
tracerProviderBuilder.DidNotReceive().AddInstrumentation(Arg.Any<Func<object>>());
}

[Fact]
public void AddSentryOtlpExporter_DoesNotConfigureDefaultPropagator()
{
// Arrange
// The OTLP integration spec requires that we MUST NOT set up an automatic propagator. Cross-service trace
// propagation is left to the propagateTraceparent option or to user-configured OTel propagators.
// See https://develop.sentry.dev/sdk/telemetry/traces/otlp/#integration-spec
var sentinel = new TraceContextPropagator();
Sdk.SetDefaultTextMapPropagator(sentinel);
var tracerProviderBuilder = Substitute.For<TracerProviderBuilder>();

// Act
tracerProviderBuilder.AddSentryOtlpExporter("https://examplePublicKey@o0.ingest.sentry.io/123456");

// Assert
Propagators.DefaultTextMapPropagator.Should().BeSameAs(sentinel);
}

[Fact]
public void OtlpConfigurationCallback_WithCustomCollectorUrl_SetsEndpointToCustomUrl()
{
Expand Down
Loading