Skip to content

Commit

Permalink
#1876 log webclient request on response timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
kasmarian committed Jan 7, 2025
1 parent 961beaf commit 69e8fcb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.zalando.logbook.spring.webflux;

import io.netty.handler.timeout.TimeoutException;
import lombok.RequiredArgsConstructor;
import org.apiguardian.api.API;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
Expand Down Expand Up @@ -34,7 +35,12 @@ public Mono<org.springframework.web.reactive.function.client.ClientResponse> fil
.body((outputMessage, context) -> request.body().insert(new BufferingClientHttpRequest(outputMessage, clientRequest), context))
.build()
)
.flatMap(throwingFunction(response -> {
.doOnError(throwingConsumer(throwable -> {
if (throwable.getCause() instanceof TimeoutException) {
requestWritingStage.write();
}
}
)).flatMap(throwingFunction(response -> {
Logbook.ResponseProcessingStage responseProcessingStage = requestWritingStage.write();

ClientResponse clientResponse = new ClientResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import reactor.netty.http.client.HttpClient;

import java.io.IOException;
import java.time.Duration;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
Expand Down Expand Up @@ -61,8 +62,9 @@ void setup() {
serverWithoutChunkEncoding.start();
when(writer.isActive()).thenReturn(true);

HttpClient httpClient = HttpClient.create().responseTimeout(Duration.ofMillis(200));
client = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.create()))
.clientConnector(new ReactorClientHttpConnector(httpClient))
.baseUrl(server.baseUrl())
.filter(new LogbookExchangeFilterFunction(logbook))
.codecs(it -> it.customCodecs().register(new Jackson2JsonEncoder(new ObjectMapper())))
Expand Down Expand Up @@ -136,6 +138,26 @@ void shouldLogRequestWithBody() throws IOException {
.contains("Hello, world!");
}

@Test
void shouldLogRequestOnTimeout() throws IOException {
server.stubFor(post("/discard").willReturn(aResponse().withStatus(200).withFixedDelay(1000)));

client.post()
.uri("/discard")
.bodyValue("Hello, world!")
.retrieve()
.bodyToMono(String.class)
.onErrorComplete()
.block();

final String message = captureRequest();

assertThat(message)
.startsWith("Outgoing Request:")
.contains(format("POST %s/discard HTTP/1.1", server.baseUrl()))
.contains("Hello, world!");
}

private String captureRequest() throws IOException {
final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
verify(writer).write(any(Precorrelation.class), captor.capture());
Expand Down

0 comments on commit 69e8fcb

Please sign in to comment.