Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting the active span on the scope #2364

Merged
merged 7 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### Features

- 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).

- Add tag filters to `SentryOptions` ([#2367](https://github.com/getsentry/sentry-dotnet/pull/2367))

### Dependencies
Expand Down
8 changes: 2 additions & 6 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
}
Expand Down
30 changes: 27 additions & 3 deletions src/Sentry/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,34 @@ internal void Evaluate()
}

/// <summary>
/// Gets the currently ongoing (not finished) span or <c>null</c> if none available.
/// This relies on the transactions being manually set on the scope via <see cref="Transaction"/>.
/// Obsolete. Use the <see cref="Span"/> property instead.
/// </summary>
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;

/// <summary>
/// Gets or sets the active span, or <c>null</c> if none available.
/// </summary>
/// <remarks>
/// 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 <see cref="Transaction"/> property.
/// </remarks>
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Core3_1.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> Tags { get; }
public Sentry.ITransaction? Transaction { get; set; }
public string? TransactionName { get; set; }
Expand All @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> Tags { get; }
public Sentry.ITransaction? Transaction { get; set; }
public string? TransactionName { get; set; }
Expand All @@ -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) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> Tags { get; }
public Sentry.ITransaction? Transaction { get; set; }
public string? TransactionName { get; set; }
Expand All @@ -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) { }
Expand Down
2 changes: 2 additions & 0 deletions test/Sentry.Tests/ApiApprovalTests.Run.Net4_8.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> Tags { get; }
public Sentry.ITransaction? Transaction { get; set; }
public string? TransactionName { get; set; }
Expand All @@ -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) { }
Expand Down
73 changes: 67 additions & 6 deletions test/Sentry.Tests/ScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,22 @@ public void TransactionName_TransactionStarted_NameReturnsActualTransactionName(
}

[Fact]
public void GetSpan_NoSpans_ReturnsTransaction()
public void Span_NoSpans_ReturnsTransaction()
{
// Arrange
var scope = new Scope();
var transaction = new TransactionTracer(DisabledHub.Instance, "foo", "_");
scope.Transaction = transaction;

// Act
var span = scope.GetSpan();
var span = scope.Span;

// Assert
span.Should().Be(transaction);
}

[Fact]
public void GetSpan_FinishedSpans_ReturnsTransaction()
public void Span_FinishedSpans_ReturnsTransaction()
{
// Arrange
var scope = new Scope();
Expand All @@ -191,14 +191,14 @@ public void GetSpan_FinishedSpans_ReturnsTransaction()
scope.Transaction = transaction;

// Act
var span = scope.GetSpan();
var span = scope.Span;

// Assert
span.Should().Be(transaction);
}

[Fact]
public void GetSpan_ActiveSpans_ReturnsSpan()
public void Span_ActiveSpans_ReturnsSpan()
{
// Arrange
var scope = new Scope();
Expand All @@ -210,12 +210,73 @@ public void GetSpan_ActiveSpans_ReturnsSpan()
scope.Transaction = transaction;

// Act
var span = scope.GetSpan();
var span = scope.Span;

// Assert
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()
{
Expand Down