Skip to content

Commit

Permalink
Add ISpan.GetTransaction convenience method (#2014)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattjohnsonpint authored Oct 26, 2022
1 parent 478f450 commit 8d4e116
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Support DI for custom transaction processors ([#1993](https://github.com/getsentry/sentry-dotnet/pull/1993))
- Mark Transaction as aborted when unhandled exception occurs ([#1996](https://github.com/getsentry/sentry-dotnet/pull/1996))
- Build Windows and Tizen targets for `Sentry.Maui` ([#2005](https://github.com/getsentry/sentry-dotnet/pull/2005))
- Add `ISpan.GetTransaction` convenience method ([#2014](https://github.com/getsentry/sentry-dotnet/pull/2014))

### Fixes

Expand Down
11 changes: 11 additions & 0 deletions src/Sentry/ISpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,16 @@ public static ISpan StartChild(this ISpan span, string operation, string? descri

return child;
}

/// <summary>
/// Gets the transaction that this span belongs to.
/// </summary>
public static ITransaction GetTransaction(this ISpan span) =>
span switch
{
ITransaction transaction => transaction,
SpanTracer tracer => tracer.Transaction,
_ => throw new ArgumentOutOfRangeException(nameof(span), span, null)
};
}
}
22 changes: 8 additions & 14 deletions src/Sentry/SpanTracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ namespace Sentry
public class SpanTracer : ISpan
{
private readonly IHub _hub;
private readonly TransactionTracer _transaction;
private readonly SentryStopwatch _stopwatch = SentryStopwatch.StartNew();

internal TransactionTracer Transaction { get; }

/// <inheritdoc />
public SpanId SpanId { get; }

Expand Down Expand Up @@ -57,14 +58,13 @@ public void SetTag(string key, string value) =>
public void UnsetTag(string key) =>
(_tags ??= new ConcurrentDictionary<string, string>()).TryRemove(key, out _);

private ConcurrentDictionary<string, object?> _data = new();
private readonly ConcurrentDictionary<string, object?> _data = new();

/// <inheritdoc />
public IReadOnlyDictionary<string, object?> Extra => _data;

/// <inheritdoc />
public void SetExtra(string key, object? value) =>
_data[key] = value;
public void SetExtra(string key, object? value) => _data[key] = value;

/// <summary>
/// Initializes an instance of <see cref="SpanTracer"/>.
Expand All @@ -77,17 +77,15 @@ public SpanTracer(
string operation)
{
_hub = hub;
_transaction = transaction;

Transaction = transaction;
SpanId = SpanId.Create();
ParentSpanId = parentSpanId;
TraceId = traceId;
Operation = operation;
}

/// <inheritdoc />
public ISpan StartChild(string operation) =>
_transaction.StartChild(SpanId, operation);
public ISpan StartChild(string operation) => Transaction.StartChild(SpanId, operation);

/// <inheritdoc />
public void Finish()
Expand All @@ -111,13 +109,9 @@ public void Finish(Exception exception, SpanStatus status)
}

/// <inheritdoc />
public void Finish(Exception exception) =>
Finish(exception, SpanStatusConverter.FromException(exception));
public void Finish(Exception exception) => Finish(exception, SpanStatusConverter.FromException(exception));

/// <inheritdoc />
public SentryTraceHeader GetTraceHeader() => new(
TraceId,
SpanId,
IsSampled);
public SentryTraceHeader GetTraceHeader() => new(TraceId, SpanId, IsSampled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ namespace Sentry
}
public static class SpanExtensions
{
public static Sentry.ITransaction GetTransaction(this Sentry.ISpan span) { }
public static Sentry.ISpan StartChild(this Sentry.ISpan span, string operation, string? description) { }
}
public readonly struct SpanId : Sentry.IJsonSerializable, System.IEquatable<Sentry.SpanId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ namespace Sentry
}
public static class SpanExtensions
{
public static Sentry.ITransaction GetTransaction(this Sentry.ISpan span) { }
public static Sentry.ISpan StartChild(this Sentry.ISpan span, string operation, string? description) { }
}
public readonly struct SpanId : Sentry.IJsonSerializable, System.IEquatable<Sentry.SpanId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ namespace Sentry
}
public static class SpanExtensions
{
public static Sentry.ITransaction GetTransaction(this Sentry.ISpan span) { }
public static Sentry.ISpan StartChild(this Sentry.ISpan span, string operation, string? description) { }
}
public readonly struct SpanId : Sentry.IJsonSerializable, System.IEquatable<Sentry.SpanId>
Expand Down
29 changes: 29 additions & 0 deletions test/Sentry.Tests/Protocol/TransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,33 @@ public void Finish_ChildSpan_StatusSet_DoesNotOverride()
// Assert
span.Status.Should().Be(SpanStatus.DataLoss);
}

[Fact]
public void ISpan_GetTransaction_FromTransaction()
{
// Arrange
var hub = Substitute.For<IHub>();
ISpan transaction = new TransactionTracer(hub, "my name", "my op");

// Act
var result = transaction.GetTransaction();

// Assert
Assert.Same(transaction, result);
}

[Fact]
public void ISpan_GetTransaction_FromSpan()
{
// Arrange
var hub = Substitute.For<IHub>();
var transaction = new TransactionTracer(hub, "my name", "my op");
var span = transaction.StartChild("child op");

// Act
var result = span.GetTransaction();

// Assert
Assert.Same(transaction, result);
}
}

0 comments on commit 8d4e116

Please sign in to comment.