I have an AWS Lambda written in Python 3.6 using the latest stable aws_xray_sdk and deployed as a lambda function, not as an API gateway endpoint.
Everything is functioning as expected, however, I've created a custom exception handler for my lambda, so if an exception occurs the error is logged and an error response is sent to the caller, rather than just return false
.
Is there a way to flag my current aws xray subsegment as being an error?
I found the subsegment
object has an apply_status_code
method, however, this doesn't appear to do what I was hoping for either.
def unhandled_exception(e, event: LambdaDict, context: LambdaContext):
logging.exception(e)
logging.error(f"""Event: {str(event)}
Context: {str(dict(context))}""")
sub_segment = xray_recorder.current_subsegment()
sub_segment.apply_status_code(500)
return {
'sms_sent': False,
'error': f"{type(e).__name__}:{e}",
}
edit 1: I've been able to get at least my own subsegments reporting with 400 and error status in the xray traces with the following code.
...
# when an error state exists
if sub_segment:
sub_segment.add_error_flag()
sub_segment.apply_status_code(400)
...
...
# directly before the return
try:
xray_recorder.end_subsegment()
xray_recorder.end_segment()
except Exception as xray_e:
logging.warning(xray_e)
return response
However, this isn't what is used to generate the xray service map.
I'm starting to think I should have just made my lambdas rest APIs, as at least those would return HTTP status codes which I could trace.
The nodes in the service map correspond to segments so in order for you to see faults or errors reflected, you’ll need to add the error flag to the segment. Unfortunately, the Lambda::Function
segment is created by Lambda and cannot be modified so if you handle the exception yourself, there isn’t a way to set the Lambda::Function
segment error flag.
You can however make the im-xmpp-prod node reflect errors or faults in the service map by mapping the status code returned in the Lambda payload to a segment fault or error in your application code. You can do this (assuming this is also Python) by calling segment = xray_recorder.current_segment()
to get the current segment and then calling segment.add_error_flag()
or segment.add_fault_flag()
to set the segment error or fault attribute equal to true.