Skip to content

Commit 5a4dd56

Browse files
committed
on exception clear Entity and create a new Error subsegment. update tests
1 parent 051305c commit 5a4dd56

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,29 @@ public void AddMetadata(string nameSpace, string key, object value)
113113
if (_isLambda)
114114
_awsxRayRecorder.AddMetadata(nameSpace, key, value);
115115
}
116-
116+
117117
/// <summary>
118118
/// Ends the subsegment.
119119
/// </summary>
120120
public void EndSubsegment()
121121
{
122-
if (_isLambda)
122+
if (!_isLambda) return;
123+
try
124+
{
125+
_awsxRayRecorder.EndSubsegment();
126+
}
127+
catch (Exception e)
123128
{
124-
try
125-
{
126-
_awsxRayRecorder.EndSubsegment();
127-
}
128-
catch (Exception e)
129-
{
130-
_awsxRayRecorder.AddException(e);
131-
_awsxRayRecorder.MarkFault();
132-
_awsxRayRecorder.MarkError();
133-
_awsxRayRecorder.EndSubsegment();
134-
}
129+
// if it fails at this stage the data is lost
130+
// so lets create a new subsegment with the error
131+
132+
Console.WriteLine("Error in Tracing utility - see Exceptions tab in Cloudwatch Traces");
133+
134+
_awsxRayRecorder.TraceContext.ClearEntity();
135+
_awsxRayRecorder.BeginSubsegment("Error in Tracing utility - see Exceptions tab");
136+
_awsxRayRecorder.AddException(e);
137+
_awsxRayRecorder.MarkError();
138+
_awsxRayRecorder.EndSubsegment();
135139
}
136140
}
137141

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ public void Tracing_End_Subsegment_Failed_Should_Cath_And_AddException()
171171
tracing.EndSubsegment();
172172

173173
// Assert
174-
awsXray.Received(2).EndSubsegment();
174+
awsXray.Received(1).BeginSubsegment("Error in Tracing utility - see Exceptions tab");
175+
awsXray.Received(1).MarkError();
175176
awsXray.Received(1).AddException(exception);
177+
awsXray.Received(2).EndSubsegment();
176178
}
177179

178180
[Fact]

0 commit comments

Comments
 (0)