Skip to content

Log response body of failed AWS requests #3579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions deps/rabbitmq_aws/src/rabbitmq_aws.erl
Original file line number Diff line number Diff line change
Expand Up @@ -546,19 +546,24 @@ api_get_request(Service, Path) ->
api_get_request_with_retries(Service, Path, ?MAX_RETRIES, ?LINEAR_BACK_OFF_MILLIS).


-spec api_get_request_with_retries(string(), path(), integer(), integer()) -> result().
%% @doc Invoke an API call to an AWS service with retries.
%% @end
-spec api_get_request_with_retries(string(), path(), integer(), integer()) -> result().
%% @doc Invoke an API call to an AWS service with retries.
%% @end
api_get_request_with_retries(_, _, 0, _) ->
rabbit_log:warning("Request to AWS service has failed after ~b retries", [?MAX_RETRIES]),
{error, "AWS service is unavailable"};
api_get_request_with_retries(Service, Path, Retries, WaitTimeBetweenRetries) ->
ensure_credentials_valid(),
case get(Service, Path) of
{ok, {_Headers, Payload}} -> rabbit_log:debug("AWS request: ~s~nResponse: ~p", [Path, Payload]),
{ok, Payload};
{error, {credentials, _}} -> {error, credentials};
{error, Message, _} -> rabbit_log:warning("Error occurred ~s~nWill retry AWS request, remaining retries: ~b", [Message, Retries]),
timer:sleep(WaitTimeBetweenRetries),
api_get_request_with_retries(Service, Path, Retries - 1, WaitTimeBetweenRetries)
{ok, {_Headers, Payload}} -> rabbit_log:debug("AWS request: ~s~nResponse: ~p", [Path, Payload]),
{ok, Payload};
{error, {credentials, _}} -> {error, credentials};
{error, Message, Response} -> rabbit_log:warning("Error occurred: ~s", [Message]),
case Response of
{_, Payload} -> rabbit_log:warning("Failed AWS request: ~s~nResponse: ~p", [Path, Payload]);
_ -> ok
end,
rabbit_log:warning("Will retry AWS request, remaining retries: ~b", [Retries]),
timer:sleep(WaitTimeBetweenRetries),
api_get_request_with_retries(Service, Path, Retries - 1, WaitTimeBetweenRetries)
end.
8 changes: 6 additions & 2 deletions deps/rabbitmq_aws/test/src/rabbitmq_aws_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ api_get_request_test_() ->
secret_access_key = "ExpiredAccessKey",
region = "us-east-1",
expiration = {{3016, 4, 1}, {12, 0, 0}}},
meck:expect(httpc, request, 4, {error, "invalid input"}),
meck:expect(httpc, request, 4, {error, "network error"}),
{ok, Pid} = rabbitmq_aws:start_link(),
rabbitmq_aws:set_region("us-east-1"),
rabbitmq_aws:set_credentials(State),
Expand All @@ -501,7 +501,11 @@ api_get_request_test_() ->
secret_access_key = "ExpiredAccessKey",
region = "us-east-1",
expiration = {{3016, 4, 1}, {12, 0, 0}}},
meck:expect(httpc, request, 4, meck:seq([{error, "invalid input"}, {ok, {{"HTTP/1.0", 200, "OK"}, [{"content-type", "application/json"}], "{\"data\": \"value\"}"}}])),
meck:expect(httpc, request, 4, meck:seq([
{error, "network error"},
{ok, {{"HTTP/1.0", 500, "OK"}, [{"content-type", "application/json"}], "{\"error\": \"server error\"}"}},
{ok, {{"HTTP/1.0", 200, "OK"}, [{"content-type", "application/json"}], "{\"data\": \"value\"}"}}
])),
{ok, Pid} = rabbitmq_aws:start_link(),
rabbitmq_aws:set_region("us-east-1"),
rabbitmq_aws:set_credentials(State),
Expand Down