Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Change client request failed logging to info for 404 responses #791

Merged
merged 3 commits into from
Sep 16, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
Expand Down Expand Up @@ -261,7 +262,7 @@ public static <T> Mono<Response<T>> getFullResponse(Mono<T> source) {

private <T> Mono<T> convertError(RequestBuilder fullReq, Throwable throwable) {
String request = format("%s, headers: %s", fullReq.getFullUrl(), requestLogger.getHeaderValuesOrRedactClient(fullReq.getHeaders()));
LOG.warn("Failed request. Url: {}", request, throwable);
logFailedRequest(throwable, request);

if (isRetryExhausted(throwable)) {
throwable = throwable.getCause();
Expand All @@ -277,6 +278,15 @@ private <T> Mono<T> convertError(RequestBuilder fullReq, Throwable throwable) {
return Mono.error(throwable);
}

private static void logFailedRequest(Throwable throwable, String request) {
var isExpectedError = throwable instanceof WebException webException &&
webException.getStatus().code() == HttpResponseStatus.NOT_FOUND.code() &&
!"resource.not.found".equals(webException.getError());

var level = isExpectedError ? Level.INFO : Level.WARN;
LOG.atLevel(level).setCause(throwable).log("Failed request. Url: {}", request);
}

protected Flux<Object> parseResponseSingle(Method method, RwHttpClientResponse response) {
if (expectsByteArrayResponse(method)) {
return Flux.from(collector.collectBytes(response.getContent()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,49 @@ void shouldApplyHeaderTransformerInLogsAndExceptionMessage() throws Exception {
loggingVerifier.verify(WARN, "Failed request. Url: localhost:" + server.port() + "/hello, headers: [Host=localhost, someHeader=ABCD]");
}

@Test
void shouldLogWarnOnErrorResponse() throws Exception {
server = startServer(BAD_REQUEST, "");
HttpClientConfig config = new HttpClientConfig("localhost:" + server.port());
TestResource resource = getHttpProxy(config);

assertThatExceptionOfType(WebException.class)
.isThrownBy(() -> resource.getHello()
.toBlocking()
.single());

loggingVerifier.verify(WARN, "Failed request. Url: localhost:" + server.port() + "/hello, headers: [Host=localhost]");
}

@Test
void shouldLogInfoOnNotFoundResponse() throws Exception {
server = startServer(NOT_FOUND, "");
HttpClientConfig config = new HttpClientConfig("localhost:" + server.port());
TestResource resource = getHttpProxy(config);

assertThatExceptionOfType(WebException.class)
.isThrownBy(() -> resource.getHello()
.toBlocking()
.single());

loggingVerifier.verify(INFO, "Failed request. Url: localhost:" + server.port() + "/hello, headers: [Host=localhost]");
}

@Test
void shouldLogWarnOnNotFoundResource() throws Exception {
server = startServer(NOT_FOUND, "{\"error\":\"resource.not.found\"}");
HttpClientConfig config = new HttpClientConfig("localhost:" + server.port());
TestResource resource = getHttpProxy(config);

assertThatExceptionOfType(WebException.class)
.isThrownBy(() -> resource.getHello()
.toBlocking()
.single());

loggingVerifier.verify(WARN, "Failed request. Url: localhost:" + server.port() + "/hello, headers: [Host=localhost]");
}


@Test
void shouldRedactSensitiveHeaderInLogsAndExceptionMessage() throws Exception {
server = startServer(BAD_REQUEST, "someError");
Expand Down Expand Up @@ -1628,7 +1671,7 @@ void shouldLogConfiguredIpAddressOnFailedRequest() throws URISyntaxException {
.cause().cause()
.satisfies(exception -> assertThat(exception)
.hasMessageContaining("URL: %s:%s/hello", host, server.port())));
loggingVerifier.verify(WARN, "Failed request. Url: %1$s:%2$s/hello, headers: [Host=%1$s]".formatted(host, server.port()));
loggingVerifier.verify(INFO, "Failed request. Url: %1$s:%2$s/hello, headers: [Host=%1$s]".formatted(host, server.port()));
}

@Test
Expand Down
Loading