Skip to content

Am I still able to intercept exceptions on retry to emit client side metrics? #2144

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

Closed
kevinrobayna opened this issue Nov 12, 2020 · 6 comments
Labels
closing-soon This issue will close in 4 days unless further comments are made. guidance Question that needs advice or information.

Comments

@kevinrobayna
Copy link

kevinrobayna commented Nov 12, 2020

Describe the issue

In v1 I was able to add a lambda to intercept exceptions in client and emit metrics if desired, I'm struggling to do the same with v2.

import com.amazonaws.retry.PredefinedRetryPolicies;
import com.amazonaws.retry.RetryPolicy;

  private RetryPolicy buildRetryPolicy() {
    return new RetryPolicy(
        (originalRequest, exception, retriesAttempted) -> {
          LOGGER.info("AWS failed with exception {}", exception.getClass());
          if (exception instanceof ProvisionedThroughputExceededException) {
            metrics.getCounter(PROVIDER_EXCEPTION,Map.of("exception", "ProvisionedThroughputExceededException")).inc();
          }
          return PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION.shouldRetry(
              originalRequest, exception, retriesAttempted);
        },
        PredefinedRetryPolicies.DEFAULT_BACKOFF_STRATEGY,
        PredefinedRetryPolicies.DEFAULT_MAX_ERROR_RETRY,
        false);
  }

Your Environment

  • AWS Java SDK version used: '2.15.26'
  • JDK version used: 11
  • Operating System and version: N/A
@kevinrobayna kevinrobayna added guidance Question that needs advice or information. needs-triage This issue or PR still needs to be triaged. labels Nov 12, 2020
@debora-ito
Copy link
Member

RetryCondition is what you're looking for, here's a super quick code example:

RetryPolicy policy =  RetryPolicy.builder()
            .retryCondition(
                retryPolicyContext -> {
                    if (retryPolicyContext.exception() instanceof ProvisionedThroughputExceededException)
                         // add metrics
                    return false;
                })
            .build();

@debora-ito debora-ito added closing-soon This issue will close in 4 days unless further comments are made. and removed needs-triage This issue or PR still needs to be triaged. labels Nov 17, 2020
@kevinrobayna
Copy link
Author

That might be exactly what i need! Thanks! I'll try it out.

@github-actions github-actions bot removed the closing-soon This issue will close in 4 days unless further comments are made. label Nov 17, 2020
@kevinrobayna
Copy link
Author

@debora-ito but this return false; that it won't be retried. I don't want to change the default way I just want to intercept the method to log the exceptions.

          return PredefinedRetryPolicies.DEFAULT_RETRY_CONDITION.shouldRetry(
              originalRequest, exception, retriesAttempted);

@debora-ito
Copy link
Member

You are correct, I just wanted to show a way to use the RetryCondition, you'd need to add your logic.

@debora-ito
Copy link
Member

return RetryCondition.defaultRetryCondition().shouldRetry(RetryPolicyContext.builder().build());

Let us know if this would help.

@debora-ito debora-ito added the closing-soon This issue will close in 4 days unless further comments are made. label Nov 18, 2020
@kevinrobayna
Copy link
Author

Thanks! that worked. For anyone who reads this in the future.

            RetryPolicy.defaultRetryPolicy().toBuilder()
                .retryCondition(
                    retryPolicyContext -> {
                      LOGGER.info("AWS failed with exception {}", retryPolicyContext.getClass());
                      // send metrics
                      return RetryCondition.defaultRetryCondition().shouldRetry(retryPolicyContext);
                    })
                .build())

aws-sdk-java-automation added a commit that referenced this issue Sep 13, 2022
…d2bcc38c8

Pull request: release <- staging/51c6cb82-dbde-43cb-8e37-9d4d2bcc38c8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closing-soon This issue will close in 4 days unless further comments are made. guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

2 participants