Skip to content

Commit 865979a

Browse files
authored
Merge pull request #223 from hjgraca/fix_tracing_on_exception_thrown
Fix Tracing when exception is thrown. Prevent Tracing from throwing InvalidOperationException
2 parents 263c170 + 2e23dec commit 865979a

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

libraries/src/AWS.Lambda.Powertools.Tracing/Internal/TracingAspectHandler.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
using System;
17+
using System.Text;
1718
using AWS.Lambda.Powertools.Common;
1819

1920
namespace AWS.Lambda.Powertools.Tracing.Internal;
@@ -163,12 +164,26 @@ public T OnException<T>(AspectEventArgs eventArgs, Exception exception)
163164
if (CaptureError())
164165
{
165166
var nameSpace = GetNamespace();
167+
168+
var sb = new StringBuilder();
169+
sb.AppendLine($"Exception type: {exception.GetType()}");
170+
sb.AppendLine($"Exception message: {exception.Message}");
171+
sb.AppendLine($"Stack trace: {exception.StackTrace}");
166172

173+
if (exception.InnerException != null)
174+
{
175+
sb.AppendLine("---BEGIN InnerException--- ");
176+
sb.AppendLine($"Exception type {exception.InnerException.GetType()}");
177+
sb.AppendLine($"Exception message: {exception.InnerException.Message}");
178+
sb.AppendLine($"Stack trace: {exception.InnerException.StackTrace}");
179+
sb.AppendLine("---END Inner Exception");
180+
}
181+
167182
_xRayRecorder.AddMetadata
168183
(
169184
nameSpace,
170185
$"{eventArgs.Name} error",
171-
exception
186+
sb.ToString()
172187
);
173188
}
174189

libraries/tests/AWS.Lambda.Powertools.Tracing.Tests/TracingAttributeTest.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
using System;
1717
using System.Linq;
18+
using System.Text;
1819
using AWS.Lambda.Powertools.Common;
1920
using AWS.Lambda.Powertools.Tracing.Internal;
2021
using Moq;
@@ -590,7 +591,8 @@ public void OnException_WhenTracerCaptureErrorEnvironmentVariableIsTrue_Captures
590591
configurations.Setup(c => c.TracerCaptureError).Returns(true);
591592
var recorder = new Mock<IXRayRecorder>();
592593
var exception = new Exception("Test Exception");
593-
594+
var message = GetException(exception);
595+
594596
var handler = new TracingAspectHandler(null, nameSpace, TracingCaptureMode.EnvironmentVariable,
595597
configurations.Object, recorder.Object);
596598
var eventArgs = new AspectEventArgs {Name = methodName};
@@ -604,7 +606,7 @@ public void OnException_WhenTracerCaptureErrorEnvironmentVariableIsTrue_Captures
604606
v.AddMetadata(
605607
It.Is<string>(i => i == nameSpace),
606608
It.Is<string>(i => i == $"{methodName} error"),
607-
It.Is<Exception>(i => i == exception
609+
It.Is<string>(i => i == message
608610
)
609611
), Times.Once);
610612
}
@@ -650,7 +652,8 @@ public void OnException_WhenTracerCaptureModeIsError_CapturesError()
650652
configurations.Setup(c => c.TracingDisabled).Returns(false);
651653
var recorder = new Mock<IXRayRecorder>();
652654
var exception = new Exception("Test Exception");
653-
655+
var message = GetException(exception);
656+
654657
var handler = new TracingAspectHandler(null, nameSpace, TracingCaptureMode.Error,
655658
configurations.Object, recorder.Object);
656659
var eventArgs = new AspectEventArgs {Name = methodName};
@@ -664,7 +667,7 @@ public void OnException_WhenTracerCaptureModeIsError_CapturesError()
664667
v.AddMetadata(
665668
It.Is<string>(i => i == nameSpace),
666669
It.Is<string>(i => i == $"{methodName} error"),
667-
It.Is<Exception>(i => i == exception
670+
It.Is<string>(i => i == message
668671
)
669672
), Times.Once);
670673
}
@@ -680,7 +683,8 @@ public void OnException_WhenTracerCaptureModeIsResponseAndError_CapturesError()
680683
configurations.Setup(c => c.TracingDisabled).Returns(false);
681684
var recorder = new Mock<IXRayRecorder>();
682685
var exception = new Exception("Test Exception");
683-
686+
var message = GetException(exception);
687+
684688
var handler = new TracingAspectHandler(null, nameSpace, TracingCaptureMode.ResponseAndError,
685689
configurations.Object, recorder.Object);
686690
var eventArgs = new AspectEventArgs {Name = methodName};
@@ -694,7 +698,7 @@ public void OnException_WhenTracerCaptureModeIsResponseAndError_CapturesError()
694698
v.AddMetadata(
695699
It.Is<string>(i => i == nameSpace),
696700
It.Is<string>(i => i == $"{methodName} error"),
697-
It.Is<Exception>(i => i == exception
701+
It.Is<string>(i => i == message
698702
)
699703
), Times.Once);
700704
}
@@ -761,6 +765,28 @@ public void OnException_WhenTracerCaptureModeIsDisabled_DoesNotCaptureError()
761765

762766
#endregion
763767

768+
#region Utilities
769+
770+
static string GetException(Exception exception)
771+
{
772+
var sb = new StringBuilder();
773+
sb.AppendLine($"Exception type: {exception.GetType()}");
774+
sb.AppendLine($"Exception message: {exception.Message}");
775+
sb.AppendLine($"Stack trace: {exception.StackTrace}");
776+
777+
if (exception.InnerException != null)
778+
{
779+
sb.AppendLine("---BEGIN InnerException--- ");
780+
sb.AppendLine($"Exception type {exception.InnerException.GetType()}");
781+
sb.AppendLine($"Exception message: {exception.InnerException.Message}");
782+
sb.AppendLine($"Stack trace: {exception.InnerException.StackTrace}");
783+
sb.AppendLine("---END Inner Exception");
784+
}
785+
return sb.ToString();
786+
}
787+
788+
#endregion
789+
764790
#region OnExit Tests
765791

766792
[Fact]

0 commit comments

Comments
 (0)