From 62246e6187d57e968d551984785909c3b49be145 Mon Sep 17 00:00:00 2001 From: James Crosswell Date: Wed, 15 Jan 2025 14:35:37 +1300 Subject: [PATCH 1/5] Don't send filtered OTel spans to Sentry --- .../SentrySpanProcessor.cs | 8 ++ .../SentrySpanProcessorTests.cs | 82 +++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/src/Sentry.OpenTelemetry/SentrySpanProcessor.cs b/src/Sentry.OpenTelemetry/SentrySpanProcessor.cs index 09172c0636..ce36fcd8f2 100644 --- a/src/Sentry.OpenTelemetry/SentrySpanProcessor.cs +++ b/src/Sentry.OpenTelemetry/SentrySpanProcessor.cs @@ -181,6 +181,14 @@ public override void OnEnd(Activity data) return; } + // Skip any activities that are not recorded. + if (data is { Recorded: false, IsAllDataRequested: false }) + { + _options?.DiagnosticLogger?.LogDebug($"Ignoring unrecorded Activity {data.SpanId}."); + _map.TryRemove(data.SpanId, out _); + return; + } + // Make a dictionary of the attributes (aka "tags") for faster lookup when used throughout the processor. var attributes = data.TagObjects.ToDict(); diff --git a/test/Sentry.OpenTelemetry.Tests/SentrySpanProcessorTests.cs b/test/Sentry.OpenTelemetry.Tests/SentrySpanProcessorTests.cs index 92839b2f56..1b199c9fc4 100644 --- a/test/Sentry.OpenTelemetry.Tests/SentrySpanProcessorTests.cs +++ b/test/Sentry.OpenTelemetry.Tests/SentrySpanProcessorTests.cs @@ -459,6 +459,88 @@ public void OnEnd_FinishesTransaction() } } + [Fact] + public void OnEnd_FilteredTransaction_DoesNotFinishTransaction() + { + // Arrange + _fixture.Options.Instrumenter = Instrumenter.OpenTelemetry; + var sut = _fixture.GetSut(); + + var parent = Tracer.StartActivity("transaction")!; + sut.OnStart(parent); + + var data = Tracer.StartActivity("test operation", kind: ActivityKind.Internal)!; + data.DisplayName = "test display name"; + sut.OnStart(data); + + FilterActivity(parent); + + sut._map.TryGetValue(parent.SpanId, out var span); + + // Act + sut.OnEnd(data); + sut.OnEnd(parent); + + // Assert + if (span is not TransactionTracer transaction) + { + Assert.Fail("Span is not a transaction tracer"); + return; + } + + using (new AssertionScope()) + { + transaction.EndTimestamp.Should().BeNull(); + transaction.Status.Should().BeNull(); + } + } + + [Fact] + public void OnEnd_FilteredSpan_RemovesSpan() + { + // Arrange + _fixture.Options.Instrumenter = Instrumenter.OpenTelemetry; + var sut = _fixture.GetSut(); + + var parent = Tracer.StartActivity("transaction")!; + sut.OnStart(parent); + + var data = Tracer.StartActivity("test operation", kind: ActivityKind.Internal)!; + data.DisplayName = "test display name"; + sut.OnStart(data); + + FilterActivity(data); + + sut._map.TryGetValue(parent.SpanId, out var parentSpan); + sut._map.TryGetValue(data.SpanId, out var childSpan); + + // Act + sut.OnEnd(data); + sut.OnEnd(parent); + + // Assert + if (parentSpan is not TransactionTracer transaction) + { + Assert.Fail("parentSpan is not a transaction tracer"); + return; + } + if (childSpan is not SpanTracer span) + { + Assert.Fail("span is not a span tracer"); + return; + } + + using (new AssertionScope()) + { + span.EndTimestamp.Should().BeNull(); + span.Status.Should().BeNull(); + + transaction.EndTimestamp.Should().NotBeNull(); + transaction.Status.Should().Be(SpanStatus.Ok); + transaction.Spans.Should().BeEmpty(); + } + } + [Theory] [InlineData(OtelSemanticConventions.AttributeUrlFull)] [InlineData(OtelSemanticConventions.AttributeHttpUrl)] From c08ee05f91c5e4acca6da19b0ef09c9dae9fa58b Mon Sep 17 00:00:00 2001 From: James Crosswell Date: Wed, 15 Jan 2025 14:43:21 +1300 Subject: [PATCH 2/5] Update CHANGELOG.md --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef60cb5644..12b04ff169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,14 @@ ## Unreleased +### Features + - Added `SentryOptions.DisableSentryHttpMessageHandler`. Useful if you're using `OpenTelemetry.Instrumentation.Http` and ending up with duplicate spans. ([#3879](https://github.com/getsentry/sentry-dotnet/pull/3879)) +### Fixes + +- OTel activities that are marked as not recorded are no longer sent to Sentry ([#3890](https://github.com/getsentry/sentry-dotnet/pull/3890)) + ## 5.0.1 ### Features From c517e4e3dde4da6ee5c4bdc793382167c71409fd Mon Sep 17 00:00:00 2001 From: James Crosswell Date: Wed, 15 Jan 2025 15:17:41 +1300 Subject: [PATCH 3/5] Fixed issue with package downgrades --- .../Sentry.Maui.Device.TestApp.csproj | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/Sentry.Maui.Device.TestApp/Sentry.Maui.Device.TestApp.csproj b/test/Sentry.Maui.Device.TestApp/Sentry.Maui.Device.TestApp.csproj index adfd1f11de..24a25093a6 100644 --- a/test/Sentry.Maui.Device.TestApp/Sentry.Maui.Device.TestApp.csproj +++ b/test/Sentry.Maui.Device.TestApp/Sentry.Maui.Device.TestApp.csproj @@ -4,6 +4,7 @@ $(TargetFrameworks);net8.0-android34.0 $(TargetFrameworks);net8.0-ios17.0 + true From 3ea209ae3d10e6276755b398df4a71ba6665fde1 Mon Sep 17 00:00:00 2001 From: James Crosswell Date: Thu, 16 Jan 2025 21:03:54 +1300 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2aa966aa97..55d97b9e55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Features +- .NET on iOS: Add experimental EnableAppHangTrackingV2 configuration flag to the options binding SDK ([#3877](https://github.com/getsentry/sentry-dotnet/pull/3877)) - Added `SentryOptions.DisableSentryHttpMessageHandler`. Useful if you're using `OpenTelemetry.Instrumentation.Http` and ending up with duplicate spans. ([#3879](https://github.com/getsentry/sentry-dotnet/pull/3879)) ### Fixes @@ -12,13 +13,6 @@ ## 5.0.1 -### Features - -- .NET on iOS: Add experimental EnableAppHangTrackingV2 configuration flag to the options binding SDK ([#3877](https://github.com/getsentry/sentry-dotnet/pull/3877)) -- Added `SentryOptions.DisableSentryHttpMessageHandler`. Useful if you're using `OpenTelemetry.Instrumentation.Http` and ending up with duplicate spans. ([#3879](https://github.com/getsentry/sentry-dotnet/pull/3879)) - -## 5.0.1 - ### Fixes - .NET Mobile: Disable and made obsolete the iOS Watchdog termination feature which is based on heuristics that don't work in .NET ([#3867](https://github.com/getsentry/sentry-dotnet/pull/3867)) From ab47bae14f738fd43f4845645ef9bbcfbb39de3e Mon Sep 17 00:00:00 2001 From: James Crosswell Date: Fri, 17 Jan 2025 10:35:20 +1300 Subject: [PATCH 5/5] Update SentrySpanProcessor.cs --- src/Sentry.OpenTelemetry/SentrySpanProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Sentry.OpenTelemetry/SentrySpanProcessor.cs b/src/Sentry.OpenTelemetry/SentrySpanProcessor.cs index ce36fcd8f2..dca597950a 100644 --- a/src/Sentry.OpenTelemetry/SentrySpanProcessor.cs +++ b/src/Sentry.OpenTelemetry/SentrySpanProcessor.cs @@ -182,7 +182,7 @@ public override void OnEnd(Activity data) } // Skip any activities that are not recorded. - if (data is { Recorded: false, IsAllDataRequested: false }) + if (data is { Recorded: false }) { _options?.DiagnosticLogger?.LogDebug($"Ignoring unrecorded Activity {data.SpanId}."); _map.TryRemove(data.SpanId, out _);