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
10 changes: 9 additions & 1 deletion src/Sentry.OpenTelemetry/SentrySpanProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,15 @@ private void CreateRootSpan(Activity data)
tracer.Contexts.Trace.Origin = OpenTelemetryOrigin;
tracer.StartTimestamp = data.StartTimeUtc;
}
_hub.ConfigureScope(static (scope, transaction) => scope.Transaction = transaction, transaction);
// Only set Scope.Transaction when null to avoid clobbering an existing transaction
// when two root OTel activities overlap. See #5314.
_hub.ConfigureScope(static (scope, transaction) =>
{
if (scope.Transaction is null)
{
scope.Transaction = transaction;
}
Comment thread
jamescrosswell marked this conversation as resolved.
Outdated
}, transaction);
Comment thread
sentry[bot] marked this conversation as resolved.
transaction.SetFused(data);
_map[data.SpanId] = transaction;
}
Expand Down
25 changes: 25 additions & 0 deletions test/Sentry.OpenTelemetry.Tests/SentrySpanProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,4 +1012,29 @@ public void ParseOtelSpanDescription_HttpClient()
description.Should().Be("POST https://example.com/foo");
source.Should().Be(TransactionNameSource.Custom);
}

[Fact]
public void OnStart_WithExistingTransactionOnScope_DoesNotOverwriteExistingTransaction()
Comment thread
jamescrosswell marked this conversation as resolved.
{
// Arrange
_fixture.Options.Instrumenter = Instrumenter.OpenTelemetry;
_fixture.Options.TracesSampleRate = 1.0;
_fixture.ScopeManager = Substitute.For<IInternalScopeManager>();
var scope = new Scope();
var clientScope = new KeyValuePair<Scope, ISentryClient>(scope, _fixture.Client);
_fixture.ScopeManager.GetCurrent().Returns(clientScope);
var sut = _fixture.GetSut();

using var firstActivity = Tracer.StartActivity("Existing");
sut.OnStart(firstActivity!);
var existingTransaction = scope.Transaction;

// Act: second root activity starts while Existing is still on the scope
using var secondActivity = Tracer.StartActivity("NewRootActivity");
sut.OnStart(secondActivity!);

// Assert
scope.Transaction.Should().BeSameAs(existingTransaction,
"CreateRootSpan should not overwrite an already-set Scope.Transaction");
Comment thread
Flash0ver marked this conversation as resolved.
}
Comment thread
cursor[bot] marked this conversation as resolved.
Comment thread
cursor[bot] marked this conversation as resolved.
}
Loading