Skip to content

Commit

Permalink
Fixes error where empty Transaction has throws exception when no Stpa…
Browse files Browse the repository at this point in the history
…ns. (#21)
  • Loading branch information
lucas-zimerman authored Nov 11, 2020
1 parent eccb3f8 commit 32d8c38
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 5 deletions.
6 changes: 2 additions & 4 deletions ContribSentry/.Transaction/SentryTracing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public SentryTracing(string name, int trackerId)
}

public ISpanBase GetSpan(string op)
{
return Spans.FirstOrDefault(s => s.Op == op);
}
=> Spans.FirstOrDefault(s => s.Op == op) ?? DisabledSpan.Instance;

public ISpanBase GetCurrentSpan()
{
Expand All @@ -68,7 +66,7 @@ public void Finish()
if (ContribSentrySdk.IsTracingSdkEnabled && new Random().NextDouble() <= ContribSentrySdk.Options.TracesSampleRate)
{
var hasError = Spans.Any(p => p.Error);
Trace.SetStatus(Spans.Last().Status);
Trace.SetStatus(Spans.LastOrDefault()?.Status);

var @event = new SentryTracingEvent(this, hasError);
if (ContribSentrySdk.Options.RegisterTracingBreadcrumb)
Expand Down
2 changes: 1 addition & 1 deletion ContribSentry/ContribSentry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageId>ContribSentry</PackageId>
<Product>ContribSentry.SessionSdk</Product>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.1.6</Version>
<Version>3.1.7</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
122 changes: 122 additions & 0 deletions Testing/SentryTracingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using ContribSentry.Extensibility;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace ContribSentry.TracingTest
{
public class SentryTracingTests
{
[Fact]
public void Ctor_ValidSentryTracing()
{
//Act
var sentryTracing = new SentryTracing(null, 0);

//Assert
Assert.True(sentryTracing != null);
}

[Fact]
public void StartChild_AddsChild()
{
//Arrange
var sentryTracing = new SentryTracing(null, 0);

//Act
var span = sentryTracing.StartChild("name","name");

//Assert
Assert.Contains(span, sentryTracing.Spans);

}

[Fact]
public void GetSpan_EmptySpan_DisabledSpan()
{
//Arrange
var sentryTracing = new SentryTracing(null, 0);

//Act
var span = sentryTracing.GetSpan("name");

//Assert
Assert.IsType<DisabledSpan>(span);
}

[Fact]
public void GetSpan_OpExist_SpanWithOp()
{
//Arrange
var sentryTracing = new SentryTracing(null, 0);
sentryTracing.StartChild("name", "name");

//Act
var span = sentryTracing.GetSpan("name");

//Assert
Assert.IsType<Span>(span);
Assert.Equal("name", span.Op);
}

[Fact]
public void GetCurrentSpan_NoSpan_DisabledSpan()
{
//Arrange
var sentryTracing = new SentryTracing(null, 0);

//Act
var span = sentryTracing.GetCurrentSpan();

//Assert
Assert.IsType<DisabledSpan>(span);
}

[Fact]
public void GetCurrentSpan_FirstSpanOpen_FirstSpan()
{
//Arrange
var sentryTracing = new SentryTracing(null, 0);
sentryTracing.StartChild("name", "name");

//Act
var span = sentryTracing.GetCurrentSpan();

//Assert
Assert.IsType<Span>(span);
Assert.Equal("name", span.Op);
}

[Fact]
public void GetCurrentSpan_FirstSpanOpenAndSecondIsChildofChild_FirstSpan()
{
//Arrange
var sentryTracing = new SentryTracing(null, 0);
var parent = sentryTracing.StartChild("name", "name");
parent.StartChild("aaa");

//Act
var span = sentryTracing.GetCurrentSpan();

//Assert
Assert.IsType<Span>(span);
Assert.Equal("name", span.Op);
}

[Fact]
public void Finish_NoSpan_CapturesEvent()
{
//Arrange
ContribSentrySdk.Init(new ContribSentryOptions());
ContribSentrySdk.Options.TracesSampleRate = 1.0;

var sentryTracing = new SentryTracing(null, 0);

//Act
sentryTracing.Finish();

ContribSentrySdk.Close();
}
}
}

0 comments on commit 32d8c38

Please sign in to comment.