Skip to content

Commit 9b29d1f

Browse files
Filtered OTel activities should not be sent to Sentry (#3890)
1 parent bcfde36 commit 9b29d1f

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- OTel activities that are marked as not recorded are no longer sent to Sentry ([#3890](https://github.com/getsentry/sentry-dotnet/pull/3890))
8+
39
## 5.1.0
410

511
### Significant change in behavior

src/Sentry.OpenTelemetry/SentrySpanProcessor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ public override void OnEnd(Activity data)
181181
return;
182182
}
183183

184+
// Skip any activities that are not recorded.
185+
if (data is { Recorded: false })
186+
{
187+
_options?.DiagnosticLogger?.LogDebug("Ignoring unrecorded Activity {0}.", data.SpanId);
188+
_map.TryRemove(data.SpanId, out _);
189+
return;
190+
}
191+
184192
// Make a dictionary of the attributes (aka "tags") for faster lookup when used throughout the processor.
185193
var attributes = data.TagObjects.ToDict();
186194

test/Sentry.OpenTelemetry.Tests/SentrySpanProcessorTests.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,88 @@ public void OnEnd_FinishesTransaction()
459459
}
460460
}
461461

462+
[Fact]
463+
public void OnEnd_FilteredTransaction_DoesNotFinishTransaction()
464+
{
465+
// Arrange
466+
_fixture.Options.Instrumenter = Instrumenter.OpenTelemetry;
467+
var sut = _fixture.GetSut();
468+
469+
var parent = Tracer.StartActivity("transaction")!;
470+
sut.OnStart(parent);
471+
472+
var data = Tracer.StartActivity("test operation", kind: ActivityKind.Internal)!;
473+
data.DisplayName = "test display name";
474+
sut.OnStart(data);
475+
476+
FilterActivity(parent);
477+
478+
sut._map.TryGetValue(parent.SpanId, out var span);
479+
480+
// Act
481+
sut.OnEnd(data);
482+
sut.OnEnd(parent);
483+
484+
// Assert
485+
if (span is not TransactionTracer transaction)
486+
{
487+
Assert.Fail("Span is not a transaction tracer");
488+
return;
489+
}
490+
491+
using (new AssertionScope())
492+
{
493+
transaction.EndTimestamp.Should().BeNull();
494+
transaction.Status.Should().BeNull();
495+
}
496+
}
497+
498+
[Fact]
499+
public void OnEnd_FilteredSpan_RemovesSpan()
500+
{
501+
// Arrange
502+
_fixture.Options.Instrumenter = Instrumenter.OpenTelemetry;
503+
var sut = _fixture.GetSut();
504+
505+
var parent = Tracer.StartActivity("transaction")!;
506+
sut.OnStart(parent);
507+
508+
var data = Tracer.StartActivity("test operation", kind: ActivityKind.Internal)!;
509+
data.DisplayName = "test display name";
510+
sut.OnStart(data);
511+
512+
FilterActivity(data);
513+
514+
sut._map.TryGetValue(parent.SpanId, out var parentSpan);
515+
sut._map.TryGetValue(data.SpanId, out var childSpan);
516+
517+
// Act
518+
sut.OnEnd(data);
519+
sut.OnEnd(parent);
520+
521+
// Assert
522+
if (parentSpan is not TransactionTracer transaction)
523+
{
524+
Assert.Fail("parentSpan is not a transaction tracer");
525+
return;
526+
}
527+
if (childSpan is not SpanTracer span)
528+
{
529+
Assert.Fail("span is not a span tracer");
530+
return;
531+
}
532+
533+
using (new AssertionScope())
534+
{
535+
span.EndTimestamp.Should().BeNull();
536+
span.Status.Should().BeNull();
537+
538+
transaction.EndTimestamp.Should().NotBeNull();
539+
transaction.Status.Should().Be(SpanStatus.Ok);
540+
transaction.Spans.Should().BeEmpty();
541+
}
542+
}
543+
462544
[Theory]
463545
[InlineData(OtelSemanticConventions.AttributeUrlFull)]
464546
[InlineData(OtelSemanticConventions.AttributeHttpUrl)]

0 commit comments

Comments
 (0)