From 8748ef400f79aa039fa37329d59c70c49dca6153 Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Thu, 1 Oct 2020 14:38:39 +0200 Subject: [PATCH] Add more logs. --- .../RetryLoadBalancerInterceptor.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java index ca7a4208..65e47cf8 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptor.java @@ -19,6 +19,9 @@ package org.springframework.cloud.client.loadbalancer; import java.io.IOException; import java.net.URI; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.cloud.client.ServiceInstance; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpRequestExecution; @@ -36,9 +39,12 @@ import org.springframework.util.StreamUtils; * @author Ryan Baxter * @author Will Tran * @author Gang Li + * @author Olga Maciaszek-Sharma */ public class RetryLoadBalancerInterceptor implements ClientHttpRequestInterceptor { + private static final Log LOG = LogFactory.getLog(RetryLoadBalancerInterceptor.class); + private final LoadBalancerClient loadBalancer; private final LoadBalancerRetryProperties lbProperties; @@ -73,15 +79,32 @@ public class RetryLoadBalancerInterceptor implements ClientHttpRequestIntercepto if (context instanceof LoadBalancedRetryContext) { LoadBalancedRetryContext lbContext = (LoadBalancedRetryContext) context; serviceInstance = lbContext.getServiceInstance(); + if (LOG.isDebugEnabled()) { + LOG.debug(String.format( + "Retrieved service instance from LoadBalancedRetryContext: %s", + serviceInstance)); + } } if (serviceInstance == null) { + if (LOG.isDebugEnabled()) { + LOG.debug( + "Service instance retrieved from LoadBalancedRetryContext: was null. " + + "Reattempting service instance selection"); + } serviceInstance = loadBalancer.choose(serviceName); + if (LOG.isDebugEnabled()) { + LOG.debug(String.format("Selected service instance: %s", + serviceInstance)); + } } ClientHttpResponse response = loadBalancer.execute(serviceName, serviceInstance, requestFactory.createRequest(request, body, execution)); int statusCode = response.getRawStatusCode(); if (retryPolicy != null && retryPolicy.retryableStatusCode(statusCode)) { + if (LOG.isDebugEnabled()) { + LOG.debug(String.format("Retrying on status code: %d", statusCode)); + } byte[] bodyCopy = StreamUtils.copyToByteArray(response.getBody()); response.close(); throw new ClientHttpResponseStatusCodeException(serviceName, response,