diff --git a/include/aws/lambda-runtime/runtime.h b/include/aws/lambda-runtime/runtime.h index 46b8817..4d62789 100644 --- a/include/aws/lambda-runtime/runtime.h +++ b/include/aws/lambda-runtime/runtime.h @@ -85,6 +85,11 @@ class invocation_response { */ bool m_success; + /** + * The serialized XRay response header. + */ + std::string m_xray_response; + /** * Instantiate an empty response. Used by the static functions 'success' and 'failure' to create a populated * invocation_response @@ -102,6 +107,15 @@ class invocation_response { { } + invocation_response( + std::string const& payload, + std::string const& content_type, + bool success, + std::string const& xray_response) + : m_payload(payload), m_content_type(content_type), m_success(success), m_xray_response(xray_response) + { + } + /** * Create a successful invocation response with the given payload and content-type. */ @@ -113,6 +127,11 @@ class invocation_response { */ static invocation_response failure(std::string const& error_message, std::string const& error_type); + static invocation_response failure( + std::string const& error_message, + std::string const& error_type, + std::string const& xray_response); + /** * Get the MIME type of the payload. */ @@ -127,6 +146,11 @@ class invocation_response { * Returns true if the payload and content-type are set. Returns false if the error message and error types are set. */ bool is_success() const { return m_success; } + + /** + * Get the XRay response string. The string isassumed to be UTF-8 encoded. + */ + std::string const& get_xray_response() const { return m_xray_response; } }; struct no_result {}; diff --git a/src/runtime.cpp b/src/runtime.cpp index 1013a19..2a8825c 100644 --- a/src/runtime.cpp +++ b/src/runtime.cpp @@ -347,6 +347,10 @@ runtime::post_outcome runtime::do_post( headers = curl_slist_append(headers, ("content-type: " + handler_response.get_content_type()).c_str()); } + if (!handler_response.get_xray_response().empty()) { + headers = curl_slist_append( + headers, ("lambda-runtime-function-xray-error-cause: " + handler_response.get_xray_response()).c_str()); + } headers = curl_slist_append(headers, "Expect:"); headers = curl_slist_append(headers, "transfer-encoding:"); headers = curl_slist_append(headers, m_user_agent_header.c_str()); @@ -522,12 +526,22 @@ invocation_response invocation_response::success(std::string payload, std::strin AWS_LAMBDA_RUNTIME_API invocation_response invocation_response::failure(std::string const& error_message, std::string const& error_type) +{ + return failure(error_message, error_type, ""); +} + +AWS_LAMBDA_RUNTIME_API +invocation_response invocation_response::failure( + std::string const& error_message, + std::string const& error_type, + std::string const& xray_response) { invocation_response r; r.m_success = false; r.m_content_type = "application/json"; r.m_payload = R"({"errorMessage":")" + json_escape(error_message) + R"(","errorType":")" + json_escape(error_type) + R"(","stackTrace":[]})"; + r.m_xray_response = xray_response; return r; }