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

Fixes error where empty Transaction has throws exception when no Spans. #21

Merged
merged 1 commit into from
Nov 11, 2020
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
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();
}
}
}