|
| 1 | +diff --git a/include/aws/lambda-runtime/runtime.h b/include/aws/lambda-runtime/runtime.h |
| 2 | +--- a/include/aws/lambda-runtime/runtime.h |
| 3 | ++++ b/include/aws/lambda-runtime/runtime.h |
| 4 | +@@ -67,6 +67,11 @@ |
| 5 | + std::string tenant_id; |
| 6 | + |
| 7 | + /** |
| 8 | ++ * The unique invocation ID for cross-wiring protection. |
| 9 | ++ */ |
| 10 | ++ std::string invocation_id; |
| 11 | ++ |
| 12 | ++ /** |
| 13 | + * Function execution deadline counted in milliseconds since the Unix epoch. |
| 14 | + */ |
| 15 | + std::chrono::time_point<std::chrono::system_clock> deadline; |
| 16 | +@@ -184,12 +189,12 @@ |
| 17 | + /** |
| 18 | + * Tells lambda that the function has succeeded. |
| 19 | + */ |
| 20 | +- post_outcome post_success(std::string const& request_id, invocation_response const& handler_response); |
| 21 | ++ post_outcome post_success(std::string const& request_id, invocation_response const& handler_response, std::string const& invocation_id = ""); |
| 22 | + |
| 23 | + /** |
| 24 | + * Tells lambda that the function has failed. |
| 25 | + */ |
| 26 | +- post_outcome post_failure(std::string const& request_id, invocation_response const& handler_response); |
| 27 | ++ post_outcome post_failure(std::string const& request_id, invocation_response const& handler_response, std::string const& invocation_id = ""); |
| 28 | + |
| 29 | + /** |
| 30 | + * Tells lambda that the runtime has failed during initialization. |
| 31 | +@@ -203,7 +208,8 @@ |
| 32 | + std::string const& url, |
| 33 | + std::string const& content_type, |
| 34 | + std::string const& payload, |
| 35 | +- std::string const& xray_response); |
| 36 | ++ std::string const& xray_response, |
| 37 | ++ std::string const& invocation_id = ""); |
| 38 | + |
| 39 | + private: |
| 40 | + std::string const m_user_agent_header; |
| 41 | +diff --git a/src/runtime.cpp b/src/runtime.cpp |
| 42 | +--- a/src/runtime.cpp |
| 43 | ++++ b/src/runtime.cpp |
| 44 | +@@ -41,6 +41,7 @@ |
| 45 | + static constexpr auto DEADLINE_MS_HEADER = "lambda-runtime-deadline-ms"; |
| 46 | + static constexpr auto FUNCTION_ARN_HEADER = "lambda-runtime-invoked-function-arn"; |
| 47 | + static constexpr auto TENANT_ID_HEADER = "lambda-runtime-aws-tenant-id"; |
| 48 | ++static constexpr auto INVOCATION_ID_HEADER = "lambda-runtime-invocation-id"; |
| 49 | + |
| 50 | + enum Endpoints { |
| 51 | + INIT, |
| 52 | +@@ -294,6 +295,10 @@ |
| 53 | + req.tenant_id = resp.get_header(TENANT_ID_HEADER); |
| 54 | + } |
| 55 | + |
| 56 | ++ if (resp.has_header(INVOCATION_ID_HEADER)) { |
| 57 | ++ req.invocation_id = resp.get_header(INVOCATION_ID_HEADER); |
| 58 | ++ } |
| 59 | ++ |
| 60 | + if (resp.has_header(DEADLINE_MS_HEADER)) { |
| 61 | + auto const& deadline_string = resp.get_header(DEADLINE_MS_HEADER); |
| 62 | + constexpr int base = 10; |
| 63 | +@@ -310,29 +315,30 @@ |
| 64 | + return next_outcome(req); |
| 65 | + } |
| 66 | + |
| 67 | +-runtime::post_outcome runtime::post_success(std::string const& request_id, invocation_response const& handler_response) |
| 68 | ++runtime::post_outcome runtime::post_success(std::string const& request_id, invocation_response const& handler_response, std::string const& invocation_id) |
| 69 | + { |
| 70 | + std::string const url = m_endpoints[Endpoints::RESULT] + request_id + "/response"; |
| 71 | +- return do_post(url, handler_response.get_content_type(), handler_response.get_payload(), handler_response.get_xray_response()); |
| 72 | ++ return do_post(url, handler_response.get_content_type(), handler_response.get_payload(), handler_response.get_xray_response(), invocation_id); |
| 73 | + } |
| 74 | + |
| 75 | +-runtime::post_outcome runtime::post_failure(std::string const& request_id, invocation_response const& handler_response) |
| 76 | ++runtime::post_outcome runtime::post_failure(std::string const& request_id, invocation_response const& handler_response, std::string const& invocation_id) |
| 77 | + { |
| 78 | + std::string const url = m_endpoints[Endpoints::RESULT] + request_id + "/error"; |
| 79 | +- return do_post(url, handler_response.get_content_type(), handler_response.get_payload(), handler_response.get_xray_response()); |
| 80 | ++ return do_post(url, handler_response.get_content_type(), handler_response.get_payload(), handler_response.get_xray_response(), invocation_id); |
| 81 | + } |
| 82 | + |
| 83 | + runtime::post_outcome runtime::post_init_error(runtime_response const& init_error_response) |
| 84 | + { |
| 85 | + std::string const url = m_endpoints[Endpoints::INIT]; |
| 86 | +- return do_post(url, init_error_response.get_content_type(), init_error_response.get_payload(), init_error_response.get_xray_response()); |
| 87 | ++ return do_post(url, init_error_response.get_content_type(), init_error_response.get_payload(), init_error_response.get_xray_response(), ""); |
| 88 | + } |
| 89 | + |
| 90 | + runtime::post_outcome runtime::do_post( |
| 91 | + std::string const& url, |
| 92 | + std::string const& content_type, |
| 93 | + std::string const& payload, |
| 94 | +- std::string const& xray_response) |
| 95 | ++ std::string const& xray_response, |
| 96 | ++ std::string const& invocation_id) |
| 97 | + { |
| 98 | + set_curl_post_result_options(); |
| 99 | + curl_easy_setopt(m_curl_handle, CURLOPT_URL, url.c_str()); |
| 100 | +@@ -351,6 +357,10 @@ |
| 101 | + headers = curl_slist_append(headers, "transfer-encoding:"); |
| 102 | + headers = curl_slist_append(headers, m_user_agent_header.c_str()); |
| 103 | + |
| 104 | ++ if (!invocation_id.empty()) { |
| 105 | ++ headers = curl_slist_append(headers, ("lambda-runtime-invocation-id: " + invocation_id).c_str()); |
| 106 | ++ } |
| 107 | ++ |
| 108 | + logging::log_debug( |
| 109 | + LOG_TAG, "calculating content length... %s", ("content-length: " + std::to_string(payload.length())).c_str()); |
| 110 | + headers = curl_slist_append(headers, ("content-length: " + std::to_string(payload.length())).c_str()); |
0 commit comments