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

Fix Tracing when exception is thrown. Prevent Tracing from throwing InvalidOperationException #223

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

using System;
using System.Text;
using AWS.Lambda.Powertools.Common;

namespace AWS.Lambda.Powertools.Tracing.Internal;
Expand Down Expand Up @@ -163,12 +164,26 @@ public T OnException<T>(AspectEventArgs eventArgs, Exception exception)
if (CaptureError())
{
var nameSpace = GetNamespace();

var sb = new StringBuilder();
sb.AppendLine($"Exception type: {exception.GetType()}");
sb.AppendLine($"Exception message: {exception.Message}");
sb.AppendLine($"Stack trace: {exception.StackTrace}");

if (exception.InnerException != null)
{
sb.AppendLine("---BEGIN InnerException--- ");
sb.AppendLine($"Exception type {exception.InnerException.GetType()}");
sb.AppendLine($"Exception message: {exception.InnerException.Message}");
sb.AppendLine($"Stack trace: {exception.InnerException.StackTrace}");
sb.AppendLine("---END Inner Exception");
}

_xRayRecorder.AddMetadata
(
nameSpace,
$"{eventArgs.Name} error",
exception
sb.ToString()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

using System;
using System.Linq;
using System.Text;
using AWS.Lambda.Powertools.Common;
using AWS.Lambda.Powertools.Tracing.Internal;
using Moq;
Expand Down Expand Up @@ -590,7 +591,8 @@ public void OnException_WhenTracerCaptureErrorEnvironmentVariableIsTrue_Captures
configurations.Setup(c => c.TracerCaptureError).Returns(true);
var recorder = new Mock<IXRayRecorder>();
var exception = new Exception("Test Exception");

var message = GetException(exception);

var handler = new TracingAspectHandler(null, nameSpace, TracingCaptureMode.EnvironmentVariable,
configurations.Object, recorder.Object);
var eventArgs = new AspectEventArgs {Name = methodName};
Expand All @@ -604,7 +606,7 @@ public void OnException_WhenTracerCaptureErrorEnvironmentVariableIsTrue_Captures
v.AddMetadata(
It.Is<string>(i => i == nameSpace),
It.Is<string>(i => i == $"{methodName} error"),
It.Is<Exception>(i => i == exception
It.Is<string>(i => i == message
)
), Times.Once);
}
Expand Down Expand Up @@ -650,7 +652,8 @@ public void OnException_WhenTracerCaptureModeIsError_CapturesError()
configurations.Setup(c => c.TracingDisabled).Returns(false);
var recorder = new Mock<IXRayRecorder>();
var exception = new Exception("Test Exception");

var message = GetException(exception);

var handler = new TracingAspectHandler(null, nameSpace, TracingCaptureMode.Error,
configurations.Object, recorder.Object);
var eventArgs = new AspectEventArgs {Name = methodName};
Expand All @@ -664,7 +667,7 @@ public void OnException_WhenTracerCaptureModeIsError_CapturesError()
v.AddMetadata(
It.Is<string>(i => i == nameSpace),
It.Is<string>(i => i == $"{methodName} error"),
It.Is<Exception>(i => i == exception
It.Is<string>(i => i == message
)
), Times.Once);
}
Expand All @@ -680,7 +683,8 @@ public void OnException_WhenTracerCaptureModeIsResponseAndError_CapturesError()
configurations.Setup(c => c.TracingDisabled).Returns(false);
var recorder = new Mock<IXRayRecorder>();
var exception = new Exception("Test Exception");

var message = GetException(exception);

var handler = new TracingAspectHandler(null, nameSpace, TracingCaptureMode.ResponseAndError,
configurations.Object, recorder.Object);
var eventArgs = new AspectEventArgs {Name = methodName};
Expand All @@ -694,7 +698,7 @@ public void OnException_WhenTracerCaptureModeIsResponseAndError_CapturesError()
v.AddMetadata(
It.Is<string>(i => i == nameSpace),
It.Is<string>(i => i == $"{methodName} error"),
It.Is<Exception>(i => i == exception
It.Is<string>(i => i == message
)
), Times.Once);
}
Expand Down Expand Up @@ -761,6 +765,28 @@ public void OnException_WhenTracerCaptureModeIsDisabled_DoesNotCaptureError()

#endregion

#region Utilities

static string GetException(Exception exception)
{
var sb = new StringBuilder();
sb.AppendLine($"Exception type: {exception.GetType()}");
sb.AppendLine($"Exception message: {exception.Message}");
sb.AppendLine($"Stack trace: {exception.StackTrace}");

if (exception.InnerException != null)
{
sb.AppendLine("---BEGIN InnerException--- ");
sb.AppendLine($"Exception type {exception.InnerException.GetType()}");
sb.AppendLine($"Exception message: {exception.InnerException.Message}");
sb.AppendLine($"Stack trace: {exception.InnerException.StackTrace}");
sb.AppendLine("---END Inner Exception");
}
return sb.ToString();
}

#endregion

#region OnExit Tests

[Fact]
Expand Down