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

Finding the same exception in different subsegments should reuse the same cause id #210

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public List<ExceptionDescriptor> DescribeException(Exception e, IEnumerable<Subs
if (existingDescriptor != null)
{
ex.Cause = existingDescriptor.Id != null ? existingDescriptor.Id : existingDescriptor.Cause;
ex.Exception = existingDescriptor.Exception; // pass the exception of the cause so that this reference can be found if the same exception is thrown again
ex.Id = null; // setting this to null since, cause is already populated with reference to downstream exception
result.Add(ex);
return result;
Expand Down
62 changes: 58 additions & 4 deletions sdk/test/UnitTests/AWSXRayRecorderBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public void Initialize()
_recorder.Dispose();
AWSXRayRecorder.Instance.Dispose();
_recorder = null;


}

[TestMethod]
Expand Down Expand Up @@ -292,7 +292,7 @@ public void TestExceptionStrategy5() // Test custom exception strategy
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);

var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();

try
{
recorder.BeginSubsegment("child1");
Expand Down Expand Up @@ -388,6 +388,60 @@ public void TestExceptionStrategy6() // Setting stack frame size to 0, so no sta
Assert.AreEqual("AmazonServiceException", segment.Subsegments[0].Subsegments[0].Cause.ExceptionDescriptors[0].Type);
}

[TestMethod]
public void TestExceptionStrategy7() // Test adding the same exception in different subsegments
{
List<Type> l = new List<Type>();
l.Add(typeof(ArgumentNullException));
var recorder = new AWSXRayRecorderBuilder().WithExceptionSerializationStrategy(new DefaultExceptionSerializationStrategy(10, l)).Build(); // set custom stackframe size
AWSXRayRecorder.InitializeInstance(recorder: recorder);
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);

var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();

try
{
recorder.BeginSubsegment("child1");
try
{
try
{
recorder.BeginSubsegment("child2");
throw new AmazonServiceException();
}
catch (AmazonServiceException e)
{
recorder.AddException(e);
recorder.EndSubsegment();
throw;
}
}
catch (AmazonServiceException e)
{
recorder.AddException(e);
recorder.EndSubsegment();
throw new EntityNotAvailableException("Dummy message", e);
}
}
catch (EntityNotAvailableException e)
{
recorder.AddException(e);
recorder.EndSegment();
}

Assert.AreEqual("Dummy message", segment.Cause.ExceptionDescriptors[0].Message);
Assert.AreEqual("EntityNotAvailableException", segment.Cause.ExceptionDescriptors[0].Type);
Assert.IsFalse(segment.Cause.ExceptionDescriptors[0].Remote); // default false
Assert.AreEqual(segment.Cause.ExceptionDescriptors[0].Cause, segment.Subsegments[0].Cause.ExceptionDescriptors[0].Id);
Assert.AreEqual(1, segment.Cause.ExceptionDescriptors.Count);

Assert.IsNull(segment.Subsegments[0].Cause.ExceptionDescriptors[0].Type);
Assert.IsFalse(segment.Subsegments[0].Cause.ExceptionDescriptors[0].Remote); // default false

Assert.AreEqual("AmazonServiceException", segment.Subsegments[0].Subsegments[0].Cause.ExceptionDescriptors[0].Type);
Assert.IsTrue(segment.Subsegments[0].Subsegments[0].Cause.ExceptionDescriptors[0].Remote); // set to true
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestSetNullExceptionSerializationStrategy()
Expand Down Expand Up @@ -457,7 +511,7 @@ public void SetDaemonAddress(string daemonAddress)
}
}

public class DummyTraceContext : ITraceContext
public class DummyTraceContext : ITraceContext
{
public void ClearEntity()
{
Expand Down