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.
