Fix Tracing when exception is thrown. Prevent Tracing from throwing InvalidOperationException #223
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix Tracing when exception is thrown. Prevent Tracing from throwing InvalidOperationException
Issue number: 211
Summary
This issue occurs when:
The following exception would occur
This exception would be thrown which does not match (hides) the real exception that occurred.
The code responsible to add the exception to the metadata when an exception occurs in our code can be found at AWS.Lambda.Powertools.Tracing/Internal/TracingAspectHandler.cs line 161
And bellow
The problem occurs when we pass the exception object to AddMetadata which internally cals XRay sdk
AWSXRayRecorder.Instance.AddMetadata(nameSpace, key, value);
Although value accepts an object, exception type is not valid in this case due to the type being generic and without a T assigned.
So what exactly is a "Type for which Type.IsGenericParameter is true"
That means it is a generic type argument in an open generic type - i.e. where we haven't picked a T yet; for example:
Changes
Transform the exception into a string
User experience
This approach works and we can see in the image bellow that the original exception is logged in the Lambda logs and added as Metadata in Xray
Checklist
Please leave checklist items unchecked if they do not apply to your change.
Is this a breaking change?
RFC issue number:
Checklist:
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.