diff --git a/src/Sentry.OpenTelemetry.Exporter/SentryOptionsExtensions.cs b/src/Sentry.OpenTelemetry.Exporter/SentryOptionsExtensions.cs index 83a4703498..9e7130eb24 100644 --- a/src/Sentry.OpenTelemetry.Exporter/SentryOptionsExtensions.cs +++ b/src/Sentry.OpenTelemetry.Exporter/SentryOptionsExtensions.cs @@ -24,12 +24,14 @@ public static class SentryOptionsExtensions /// /// The default TextMapPropagator to be used by OpenTelemetry. /// - /// If this parameter is not supplied, the will be used, which propagates the - /// baggage header as well as Sentry trace headers. + /// If this parameter is not supplied, a containing both + /// W3C traceparent/tracestate and + /// (sentry-trace and baggage) will be used. + /// This allows Sentry to interoperate with services that use W3C trace context headers. /// /// - /// The is required for Sentry's OpenTelemetry integration to work, but you - /// could wrap this in a if you needed other propagators as well. + /// The is required for Sentry's OpenTelemetry integration to work. Supply + /// a custom propagator only if you need to replace the defaults entirely. /// /// public static void UseOtlp(this SentryOptions options, TracerProviderBuilder tracerProviderBuilder, Uri? collectorUrl = null, TextMapPropagator? defaultTextMapPropagator = null) diff --git a/src/Sentry.OpenTelemetry.Exporter/SentryTracerProviderBuilderExtensions.cs b/src/Sentry.OpenTelemetry.Exporter/SentryTracerProviderBuilderExtensions.cs index 0bd9e03130..6f8b7b6245 100644 --- a/src/Sentry.OpenTelemetry.Exporter/SentryTracerProviderBuilderExtensions.cs +++ b/src/Sentry.OpenTelemetry.Exporter/SentryTracerProviderBuilderExtensions.cs @@ -30,12 +30,14 @@ public static class SentryTracerProviderBuilderExtensions /// /// The default TextMapPropagator to be used by OpenTelemetry. /// - /// If this parameter is not supplied, the will be used, which propagates the - /// baggage header as well as Sentry trace headers. + /// If this parameter is not supplied, a containing both + /// (W3C traceparent/tracestate) and + /// (sentry-trace and baggage) will be used. + /// This allows Sentry to interoperate with services that use W3C trace context headers. /// /// - /// The is required for Sentry's OpenTelemetry integration to work, but you - /// could wrap this in a if you needed other propagators as well. + /// The is required for Sentry's OpenTelemetry integration to work. Supply + /// a custom propagator only if you need to replace the defaults entirely. /// /// /// The supplied for chaining. @@ -47,7 +49,11 @@ public static TracerProviderBuilder AddSentryOtlpExporter(this TracerProviderBui throw new ArgumentException(MissingDsnWarning, nameof(dsnString)); } - defaultTextMapPropagator ??= new SentryPropagator(); + defaultTextMapPropagator ??= new CompositeTextMapPropagator(new TextMapPropagator[] + { + new TraceContextPropagator(), + new SentryPropagator(), + }); Sdk.SetDefaultTextMapPropagator(defaultTextMapPropagator); collectorUrl ??= dsn.GetOtlpTracesEndpointUri(); diff --git a/test/Sentry.OpenTelemetry.Exporter.Tests/SentryTracerProviderBuilderExtensionsTests.cs b/test/Sentry.OpenTelemetry.Exporter.Tests/SentryTracerProviderBuilderExtensionsTests.cs index dc9ad16be0..57f14d9e1b 100644 --- a/test/Sentry.OpenTelemetry.Exporter.Tests/SentryTracerProviderBuilderExtensionsTests.cs +++ b/test/Sentry.OpenTelemetry.Exporter.Tests/SentryTracerProviderBuilderExtensionsTests.cs @@ -1,3 +1,5 @@ +using OpenTelemetry; +using OpenTelemetry.Context.Propagation; using OpenTelemetry.Exporter; using OpenTelemetry.Trace; @@ -22,6 +24,37 @@ public void AddSentryOtlpExporter_InvalidDsn_ThrowsArgumentException(string dsn) .WithMessage($"{SentryTracerProviderBuilderExtensions.MissingDsnWarning}*"); } + [Fact] + public void AddSentryOtlpExporter_NoCustomPropagator_SetsCompositeDefaultPropagatorWithW3CAndSentry() + { + // Arrange + var tracerProviderBuilder = Substitute.For(); + + // Act + tracerProviderBuilder.AddSentryOtlpExporter(ValidDsn); + var propagator = Propagators.DefaultTextMapPropagator; + + // Assert + propagator.Should().BeOfType(); + propagator.Fields.Should().Contain("traceparent", "W3C traceparent header should be propagated by default"); + propagator.Fields.Should().Contain("sentry-trace", "Sentry trace header should be propagated by default"); + } + + [Fact] + public void AddSentryOtlpExporter_WithCustomPropagator_SetsCustomPropagatorAsDefault() + { + // Arrange + var tracerProviderBuilder = Substitute.For(); + var customPropagator = Substitute.For(); + + // Act + tracerProviderBuilder.AddSentryOtlpExporter(ValidDsn, defaultTextMapPropagator: customPropagator); + var propagator = Propagators.DefaultTextMapPropagator; + + // Assert + propagator.Should().BeSameAs(customPropagator); + } + [Fact] public void OtlpConfigurationCallback_WithCustomCollectorUrl_SetsEndpointToCustomUrl() {