From 0a8abaaaefefb62fa178b2dee9f5c40a451f462d Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Wed, 10 May 2023 20:26:55 -0700 Subject: [PATCH 1/6] Move GetScope to Scope property and implement setter. --- src/Sentry/Scope.cs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Sentry/Scope.cs b/src/Sentry/Scope.cs index bd4db02382..3cc559e747 100644 --- a/src/Sentry/Scope.cs +++ b/src/Sentry/Scope.cs @@ -516,10 +516,34 @@ internal void Evaluate() } /// - /// Gets the currently ongoing (not finished) span or null if none available. - /// This relies on the transactions being manually set on the scope via . + /// Obsolete. Use the property instead. /// - public ISpan? GetSpan() => Transaction?.GetLastActiveSpan() ?? Transaction; + [Obsolete("Use the Span property instead. This method will be removed in a future release.")] + public ISpan? GetSpan() => Span; + + private ISpan? _span; + + /// + /// Gets or sets the active span, or null if none available. + /// + /// + /// If a span has been set on this property, it will become the active span until it is finished. + /// Otherwise, the active span is the latest unfinished span on the transaction, presuming a transaction + /// was set on the scope via the property. + /// + public ISpan? Span + { + get + { + if (_span?.IsFinished is false) + { + return _span; + } + + return Transaction?.GetLastActiveSpan() ?? Transaction; + } + set => _span = value; + } internal void ResetTransaction(ITransaction? expectedCurrentTransaction) => Interlocked.CompareExchange(ref _transaction, null, expectedCurrentTransaction); From 90924c1d76dc9a9afebd78fda5711aba0cfbd8db Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Wed, 10 May 2023 20:27:45 -0700 Subject: [PATCH 2/6] Update GetSpan usages --- src/Sentry/Internal/Hub.cs | 8 ++------ .../Integration/SQLite/SentryDiagnosticListenerTests.cs | 6 +----- test/Sentry.Tests/ScopeTests.cs | 6 +++--- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/Sentry/Internal/Hub.cs b/src/Sentry/Internal/Hub.cs index 81b21c7cf5..5e8f38c6d4 100644 --- a/src/Sentry/Internal/Hub.cs +++ b/src/Sentry/Internal/Hub.cs @@ -182,11 +182,7 @@ public void BindException(Exception exception, ISpan span) _ = ExceptionToSpanMap.GetValue(exception, _ => span); } - public ISpan? GetSpan() - { - var (currentScope, _) = ScopeManager.GetCurrent(); - return currentScope.GetSpan(); - } + public ISpan? GetSpan() => ScopeManager.GetCurrent().Key.Span; public SentryTraceHeader? GetTraceHeader() => GetSpan()?.GetTraceHeader(); @@ -286,7 +282,7 @@ public void EndSession(SessionEndStatus status = SessionEndStatus.Exited) => } // Otherwise just get the currently active span on the scope (unless it's sampled out) - if (scope.GetSpan() is { IsSampled: not false } span) + if (scope.Span is { IsSampled: not false } span) { return span; } diff --git a/test/Sentry.DiagnosticSource.Tests/Integration/SQLite/SentryDiagnosticListenerTests.cs b/test/Sentry.DiagnosticSource.Tests/Integration/SQLite/SentryDiagnosticListenerTests.cs index 6727b72310..1a51c0ba22 100644 --- a/test/Sentry.DiagnosticSource.Tests/Integration/SQLite/SentryDiagnosticListenerTests.cs +++ b/test/Sentry.DiagnosticSource.Tests/Integration/SQLite/SentryDiagnosticListenerTests.cs @@ -38,11 +38,7 @@ public Fixture() } public ItemsContext NewContext() => new(_database.ContextOptions); - public ISpan GetSpan() - { - var (currentScope, _) = ScopeManager.GetCurrent(); - return currentScope.GetSpan(); - } + public ISpan GetSpan() => ScopeManager.GetCurrent().Key.Span; public ITransaction StartTransaction(IHub hub, ITransactionContext context) { diff --git a/test/Sentry.Tests/ScopeTests.cs b/test/Sentry.Tests/ScopeTests.cs index d7de09a391..58b7fc8315 100644 --- a/test/Sentry.Tests/ScopeTests.cs +++ b/test/Sentry.Tests/ScopeTests.cs @@ -172,7 +172,7 @@ public void GetSpan_NoSpans_ReturnsTransaction() scope.Transaction = transaction; // Act - var span = scope.GetSpan(); + var span = scope.Span; // Assert span.Should().Be(transaction); @@ -191,7 +191,7 @@ public void GetSpan_FinishedSpans_ReturnsTransaction() scope.Transaction = transaction; // Act - var span = scope.GetSpan(); + var span = scope.Span; // Assert span.Should().Be(transaction); @@ -210,7 +210,7 @@ public void GetSpan_ActiveSpans_ReturnsSpan() scope.Transaction = transaction; // Act - var span = scope.GetSpan(); + var span = scope.Span; // Assert span.Should().Be(activeSpan); From 9cc8b776b06fe71363e8fafcb68f4b33ad324861 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Wed, 10 May 2023 20:29:58 -0700 Subject: [PATCH 3/6] Update API snapshots --- test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt | 2 ++ test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt | 2 ++ test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt | 2 ++ test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt | 2 ++ 4 files changed, 8 insertions(+) diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt index bc4762d774..9fd5a0dad7 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt @@ -404,6 +404,7 @@ namespace Sentry public string? Release { get; set; } public Sentry.Request Request { get; set; } public Sentry.SdkVersion Sdk { get; } + public Sentry.ISpan? Span { get; set; } public System.Collections.Generic.IReadOnlyDictionary Tags { get; } public Sentry.ITransaction? Transaction { get; set; } public string? TransactionName { get; set; } @@ -417,6 +418,7 @@ namespace Sentry public void ClearAttachments() { } public void ClearBreadcrumbs() { } public Sentry.Scope Clone() { } + [System.Obsolete("Use the Span property instead. This method will be removed in a future release.")] public Sentry.ISpan? GetSpan() { } public void SetExtra(string key, object? value) { } public void SetTag(string key, string value) { } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt index 9816e4fab2..fc955378d3 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet6_0.verified.txt @@ -404,6 +404,7 @@ namespace Sentry public string? Release { get; set; } public Sentry.Request Request { get; set; } public Sentry.SdkVersion Sdk { get; } + public Sentry.ISpan? Span { get; set; } public System.Collections.Generic.IReadOnlyDictionary Tags { get; } public Sentry.ITransaction? Transaction { get; set; } public string? TransactionName { get; set; } @@ -417,6 +418,7 @@ namespace Sentry public void ClearAttachments() { } public void ClearBreadcrumbs() { } public Sentry.Scope Clone() { } + [System.Obsolete("Use the Span property instead. This method will be removed in a future release.")] public Sentry.ISpan? GetSpan() { } public void SetExtra(string key, object? value) { } public void SetTag(string key, string value) { } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt index 9816e4fab2..fc955378d3 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.DotNet7_0.verified.txt @@ -404,6 +404,7 @@ namespace Sentry public string? Release { get; set; } public Sentry.Request Request { get; set; } public Sentry.SdkVersion Sdk { get; } + public Sentry.ISpan? Span { get; set; } public System.Collections.Generic.IReadOnlyDictionary Tags { get; } public Sentry.ITransaction? Transaction { get; set; } public string? TransactionName { get; set; } @@ -417,6 +418,7 @@ namespace Sentry public void ClearAttachments() { } public void ClearBreadcrumbs() { } public Sentry.Scope Clone() { } + [System.Obsolete("Use the Span property instead. This method will be removed in a future release.")] public Sentry.ISpan? GetSpan() { } public void SetExtra(string key, object? value) { } public void SetTag(string key, string value) { } diff --git a/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt b/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt index fa1b223ac0..4a9f32ac3a 100644 --- a/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt +++ b/test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt @@ -403,6 +403,7 @@ namespace Sentry public string? Release { get; set; } public Sentry.Request Request { get; set; } public Sentry.SdkVersion Sdk { get; } + public Sentry.ISpan? Span { get; set; } public System.Collections.Generic.IReadOnlyDictionary Tags { get; } public Sentry.ITransaction? Transaction { get; set; } public string? TransactionName { get; set; } @@ -416,6 +417,7 @@ namespace Sentry public void ClearAttachments() { } public void ClearBreadcrumbs() { } public Sentry.Scope Clone() { } + [System.Obsolete("Use the Span property instead. This method will be removed in a future release.")] public Sentry.ISpan? GetSpan() { } public void SetExtra(string key, object? value) { } public void SetTag(string key, string value) { } From f0b9b8c0841f1babab65772b01b0d76f935bd241 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Wed, 10 May 2023 20:46:49 -0700 Subject: [PATCH 4/6] Rename tests --- test/Sentry.Tests/ScopeTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Sentry.Tests/ScopeTests.cs b/test/Sentry.Tests/ScopeTests.cs index 58b7fc8315..9e5381a7e1 100644 --- a/test/Sentry.Tests/ScopeTests.cs +++ b/test/Sentry.Tests/ScopeTests.cs @@ -164,7 +164,7 @@ public void TransactionName_TransactionStarted_NameReturnsActualTransactionName( } [Fact] - public void GetSpan_NoSpans_ReturnsTransaction() + public void Span_NoSpans_ReturnsTransaction() { // Arrange var scope = new Scope(); @@ -179,7 +179,7 @@ public void GetSpan_NoSpans_ReturnsTransaction() } [Fact] - public void GetSpan_FinishedSpans_ReturnsTransaction() + public void Span_FinishedSpans_ReturnsTransaction() { // Arrange var scope = new Scope(); @@ -198,7 +198,7 @@ public void GetSpan_FinishedSpans_ReturnsTransaction() } [Fact] - public void GetSpan_ActiveSpans_ReturnsSpan() + public void Span_ActiveSpans_ReturnsSpan() { // Arrange var scope = new Scope(); From e390d27d5661dc425f19d8315ae7837f4b1fe2f2 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Wed, 10 May 2023 20:57:00 -0700 Subject: [PATCH 5/6] Add Tests --- test/Sentry.Tests/ScopeTests.cs | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/test/Sentry.Tests/ScopeTests.cs b/test/Sentry.Tests/ScopeTests.cs index 9e5381a7e1..9cfdd70cfc 100644 --- a/test/Sentry.Tests/ScopeTests.cs +++ b/test/Sentry.Tests/ScopeTests.cs @@ -216,6 +216,67 @@ public void Span_ActiveSpans_ReturnsSpan() span.Should().Be(activeSpan); } + [Fact] + public void Span_SetSpan_ReturnsValue() + { + // Arrange + var scope = new Scope(); + + var transaction = new TransactionTracer(DisabledHub.Instance, "foo", "_"); + var firstSpan = transaction.StartChild("123"); + var secondSpan = firstSpan.StartChild("456"); + + scope.Transaction = transaction; + + // Assert Default + scope.Span.Should().Be(secondSpan); + + // Act + scope.Span = firstSpan; + + // Assert + scope.Span.Should().Be(firstSpan); + } + + [Fact] + public void Span_SetSpanNull_ReturnsLatestOpen() + { + // Arrange + var scope = new Scope(); + + var transaction = new TransactionTracer(DisabledHub.Instance, "foo", "_"); + var firstSpan = transaction.StartChild("123"); + var secondSpan = firstSpan.StartChild("456"); + + scope.Transaction = transaction; + + // Act + scope.Span = null; + + // Assert + scope.Span.Should().Be(secondSpan); + } + + [Fact] + public void Span_SetSpanThenCloseIt_ReturnsLatestOpen() + { + // Arrange + var scope = new Scope(); + + var transaction = new TransactionTracer(DisabledHub.Instance, "foo", "_"); + var firstSpan = transaction.StartChild("123"); + var secondSpan = firstSpan.StartChild("456"); + + scope.Transaction = transaction; + + // Act + scope.Span = firstSpan; + firstSpan.Finish(); + + // Assert + scope.Span.Should().Be(secondSpan); + } + [Fact] public void AddAttachment_AddAttachments() { From 85fdcd28f152b8c908fa50c8d7192f59ce45e631 Mon Sep 17 00:00:00 2001 From: Matt Johnson-Pint Date: Wed, 10 May 2023 20:59:41 -0700 Subject: [PATCH 6/6] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb18126764..34d0a13b17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ ### Features - Add tag filters to SentryLoggingOptions ([#2360](https://github.com/getsentry/sentry-dotnet/pull/2360)) +- Allow setting the active span on the scope ([#2364](https://github.com/getsentry/sentry-dotnet/pull/2364)) + - Note: Obsoletes the `Scope.GetSpan` method in favor of a `Scope.Span` property (which now has a setter as well). ### Dependencies