This is an automated email from the ASF dual-hosted git repository. dulvac pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-clients.git
The following commit(s) were added to refs/heads/master by this push: new 461a4a7 SLING-11748 - Improve logging output of HTTP retries in testing clients (#42) 461a4a7 is described below commit 461a4a71748172ff6f3ad211eb59220588a7a272 Author: Marc Pfaff <pfa...@adobe.com> AuthorDate: Tue Feb 7 11:58:38 2023 +0100 SLING-11748 - Improve logging output of HTTP retries in testing clients (#42) * SLING-11748 - Improve logging output of HTTP retries in testing clients * SLING-11748 - Improve logging output of HTTP retries in testing clients - replace wrapper.toString() * SLING-11748 - Improve logging output of HTTP retries in testing clients - wrap lines at 120 characters --- .../clients/util/ServerErrorRetryStrategy.java | 61 +++++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/sling/testing/clients/util/ServerErrorRetryStrategy.java b/src/main/java/org/apache/sling/testing/clients/util/ServerErrorRetryStrategy.java index 4bf9c8e..cc2eeac 100644 --- a/src/main/java/org/apache/sling/testing/clients/util/ServerErrorRetryStrategy.java +++ b/src/main/java/org/apache/sling/testing/clients/util/ServerErrorRetryStrategy.java @@ -18,6 +18,8 @@ package org.apache.sling.testing.clients.util; import org.apache.http.HttpResponse; import org.apache.http.client.ServiceUnavailableRetryStrategy; +import org.apache.http.client.methods.HttpRequestWrapper; +import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import org.apache.sling.testing.clients.SystemPropertiesConfig; @@ -28,7 +30,7 @@ import java.io.IOException; import java.util.Arrays; import java.util.Collection; -import static org.apache.http.HttpStatus.*; +import static org.apache.http.HttpStatus.SC_INTERNAL_SERVER_ERROR; import static org.apache.sling.testing.Constants.EXPECTED_STATUS; /** @@ -37,6 +39,7 @@ import static org.apache.sling.testing.Constants.EXPECTED_STATUS; public class ServerErrorRetryStrategy implements ServiceUnavailableRetryStrategy { private static final Logger LOG = LoggerFactory.getLogger(ServerErrorRetryStrategy.class); + private Collection<Integer> httpRetriesErrorCodes; public ServerErrorRetryStrategy() { super(); @@ -45,17 +48,20 @@ public class ServerErrorRetryStrategy implements ServiceUnavailableRetryStrategy @Override public boolean retryRequest(final HttpResponse response, final int executionCount, final HttpContext context) { int[] expectedStatus = (int[]) context.getAttribute(EXPECTED_STATUS); - boolean needsRetry = executionCount <= SystemPropertiesConfig.getHttpRetries() && responseRetryCondition(response, expectedStatus); + boolean needsRetry = executionCount <= SystemPropertiesConfig.getHttpRetries() && + responseRetryCondition(response, expectedStatus); if (SystemPropertiesConfig.isHttpLogRetries() && needsRetry && LOG.isWarnEnabled()) { - LOG.warn("Request retry needed due to service unavailable response"); - LOG.warn("Response headers contained:"); - Arrays.stream(response.getAllHeaders()).forEach(h -> LOG.warn("Header {}:{}", h.getName(), h.getValue())); + LOG.warn("Request retry condition met: [count={}/{}], [expected-codes={}], [retry-codes={}]", + executionCount, SystemPropertiesConfig.getHttpRetries(), expectedStatus, + httpRetriesErrorCodes); + LOG.warn("Request: {}", getRequestDetails(context)); + LOG.warn("Response: {}", getResponseDetails(response)); try { String content = EntityUtils.toString(response.getEntity()); - LOG.warn("Response content: {}", content); + LOG.warn("Response Body: {}", content); } catch (IOException exc) { - LOG.warn("Response as no content"); + LOG.warn("Failed to read the response body: {}", exc.getMessage()); } } return needsRetry; @@ -80,4 +86,45 @@ public class ServerErrorRetryStrategy implements ServiceUnavailableRetryStrategy statusCode < SC_INTERNAL_SERVER_ERROR + 100; } } + + /** + * Best effort attempt to build a request detail string for logging. + */ + private String getRequestDetails(HttpContext context) { + String details = "Not available"; + HttpClientContext clientContext = HttpClientContext.adapt(context); + HttpRequestWrapper wrapper = clientContext.getAttribute(HttpClientContext.HTTP_REQUEST, HttpRequestWrapper.class); + if (wrapper != null) { + // Build a request detail string like following example: + // GET /test/internalerror/resource HTTP/1.1 [Host: 127.0.0.1:35049, Connection: Keep-Alive, User-Agent: Java, + // Accept-Encoding: gzip,deflate, Authorization: Basic dXNlcjpwYXNz] + final StringBuilder sb = new StringBuilder(wrapper.getRequestLine().toString()); + sb.append(" ["); + Arrays.stream(wrapper.getAllHeaders()).forEach(header -> + sb.append(header.getName()).append(": ").append(header.getValue()).append(", ")); + sb.append("]"); + details = sb.toString(); + + } + return details; + } + + /** + * Best effort attempt to build response detail string for logging. + */ + private String getResponseDetails(HttpResponse response) { + String details = "Not available"; + if (response != null) { + // Build a response string like following example: + // HTTP/1.1 500 Internal Server Error [Date: Thu, 12 Jan 2023 08:32:42 GMT, Server: TEST/1.1, + // Content-Length: 8, Content-Type: text/plain; charset=ISO-8859-1, Connection: Keep-Alive, ] + final StringBuilder sb = new StringBuilder(response.getStatusLine().toString()); + sb.append(" ["); + Arrays.stream(response.getAllHeaders()).forEach(h -> + sb.append(h.getName()).append(": ").append(h.getValue()).append(", ")); + sb.append("]"); + details = sb.toString(); + } + return details; + } }