Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: InvalidCastException in SentrySpanProcessor
Resolves #4235:
- #4235
  • Loading branch information
jamescrosswell committed Jun 3, 2025
commit 404a0b74c617aef93df14e0e8cbb4ee2b0f7a35c
10 changes: 6 additions & 4 deletions src/Sentry.OpenTelemetry/SentrySpanProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,19 @@ private void CreateRootSpan(Activity data)

var baggageHeader = data.Baggage.AsBaggageHeader();
var dynamicSamplingContext = baggageHeader.CreateDynamicSamplingContext(_replaySession);
var transaction = (TransactionTracer)_hub.StartTransaction(
var transaction = _hub.StartTransaction(
transactionContext, new Dictionary<string, object?>(), dynamicSamplingContext
);
transaction.Contexts.Trace.Origin = OpenTelemetryOrigin;
transaction.StartTimestamp = data.StartTimeUtc;
if (transaction is TransactionTracer tracer)
{
tracer.Contexts.Trace.Origin = OpenTelemetryOrigin;
tracer.StartTimestamp = data.StartTimeUtc;
}
_hub.ConfigureScope(scope => scope.Transaction = transaction);
transaction.SetFused(data);
_map[data.SpanId] = transaction;
}


/// <inheritdoc />
public override void OnEnd(Activity data)
{
Expand Down
49 changes: 35 additions & 14 deletions test/Sentry.OpenTelemetry.Tests/SentrySpanProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,14 @@ public void StartSpan_UsingSentryTracing_StartsChildSpan()
}
}

[Fact]
public void OnStart_WithoutParentSpanId_StartsNewTransaction()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void OnStart_WithoutParentSpanId_StartsNewTransaction(bool isSampled)
{
// Arrange
_fixture.Options.Instrumenter = Instrumenter.OpenTelemetry;
_fixture.Options.TracesSampleRate = isSampled ? 1.0 : 0.0;
_fixture.ScopeManager = Substitute.For<IInternalScopeManager>();
var scope = new Scope();
var clientScope = new KeyValuePair<Scope, ISentryClient>(scope, _fixture.Client);
Expand All @@ -274,21 +277,39 @@ public void OnStart_WithoutParentSpanId_StartsNewTransaction()

// Assert
Assert.True(sut._map.TryGetValue(data.SpanId, out var span));
if (span is not TransactionTracer transaction)
if (isSampled)
{
Assert.Fail("Span is not a transaction tracer");
return;
if (span is not TransactionTracer transaction)
{
Assert.Fail("Span is not a transaction tracer");
return;
}

using (new AssertionScope())
{
transaction.SpanId.Should().Be(data.SpanId.AsSentrySpanId());
transaction.ParentSpanId.Should().Be(new ActivitySpanId().AsSentrySpanId());
transaction.TraceId.Should().Be(data.TraceId.AsSentryId());
transaction.Name.Should().Be(data.DisplayName);
transaction.Operation.Should().Be(data.OperationName);
transaction.Description.Should().Be(data.DisplayName);
transaction.Status.Should().BeNull();
transaction.StartTimestamp.Should().Be(data.StartTimeUtc);
}
}
using (new AssertionScope())
else
{
transaction.SpanId.Should().Be(data.SpanId.AsSentrySpanId());
transaction.ParentSpanId.Should().Be(new ActivitySpanId().AsSentrySpanId());
transaction.TraceId.Should().Be(data.TraceId.AsSentryId());
transaction.Name.Should().Be(data.DisplayName);
transaction.Operation.Should().Be(data.OperationName);
transaction.Description.Should().Be(data.DisplayName);
transaction.Status.Should().BeNull();
transaction.StartTimestamp.Should().Be(data.StartTimeUtc);
if (span is not UnsampledTransaction transaction)
{
Assert.Fail("Span is not an unsampled transaction");
return;
}

using (new AssertionScope())
{
transaction.SpanId.Should().Be(data.SpanId.AsSentrySpanId());
transaction.TraceId.Should().Be(data.TraceId.AsSentryId());
}
}
}

Expand Down
Loading