Browse Source

Exclude query from URI in WebClient checkpoints

Closes gh-31992
pull/32145/head
rstoyanchev 2 years ago
parent
commit
6df8be8be3
  1. 4
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java
  2. 23
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java
  3. 6
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java
  4. 22
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientUtils.java

4
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -212,7 +212,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
return new DefaultClientResponse(httpResponse, this.strategies, return new DefaultClientResponse(httpResponse, this.strategies,
this.originalResponse != null ? this.originalResponse.logPrefix() : "", this.originalResponse != null ? this.originalResponse.logPrefix() : "",
this.request.getMethod() + " " + this.request.getURI(), WebClientUtils.getRequestDescription(this.request.getMethod(), this.request.getURI()),
() -> this.request); () -> this.request);
} }

23
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClient.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.web.reactive.function.client; package org.springframework.web.reactive.function.client;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
@ -54,7 +53,6 @@ import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.BodyExtractor; import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.reactive.function.BodyInserter; import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.BodyInserters;
@ -457,7 +455,9 @@ final class DefaultWebClient implements WebClient {
observationContext.setRequest(request); observationContext.setRequest(request);
Mono<ClientResponse> responseMono = filterFunction.apply(exchangeFunction) Mono<ClientResponse> responseMono = filterFunction.apply(exchangeFunction)
.exchange(request) .exchange(request)
.checkpoint("Request to " + this.httpMethod.name() + " " + this.uri + " [DefaultWebClient]") .checkpoint("Request to " +
WebClientUtils.getRequestDescription(request.method(), request.url()) +
" [DefaultWebClient]")
.switchIfEmpty(NO_HTTP_CLIENT_RESPONSE_ERROR); .switchIfEmpty(NO_HTTP_CLIENT_RESPONSE_ERROR);
if (this.contextModifier != null) { if (this.contextModifier != null) {
responseMono = responseMono.contextWrite(this.contextModifier); responseMono = responseMono.contextWrite(this.contextModifier);
@ -693,24 +693,13 @@ final class DefaultWebClient implements WebClient {
} }
Mono<T> result = exMono.flatMap(Mono::error); Mono<T> result = exMono.flatMap(Mono::error);
return result.checkpoint(statusCode + " from " + return result.checkpoint(statusCode + " from " +
this.httpMethod + " " + getUriToLog(this.uri) + " [DefaultWebClient]"); WebClientUtils.getRequestDescription(this.httpMethod, this.uri) +
" [DefaultWebClient]");
} }
} }
return null; return null;
} }
private static URI getUriToLog(URI uri) {
if (StringUtils.hasText(uri.getQuery())) {
try {
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
}
catch (URISyntaxException ex) {
// ignore
}
}
return uri;
}
private static class StatusHandler { private static class StatusHandler {

6
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientResponseException.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -97,8 +97,8 @@ public class WebClientResponseException extends WebClientException {
} }
private static String initMessage(HttpStatusCode status, String reasonPhrase, @Nullable HttpRequest request) { private static String initMessage(HttpStatusCode status, String reasonPhrase, @Nullable HttpRequest request) {
return status.value() + " " + reasonPhrase + return status.value() + " " + reasonPhrase + (request != null ?
(request != null ? " from " + request.getMethod() + " " + request.getURI() : ""); " from " + WebClientUtils.getRequestDescription(request.getMethod(), request.getURI()) : "");
} }
/** /**

22
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/WebClientUtils.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,8 @@
package org.springframework.web.reactive.function.client; package org.springframework.web.reactive.function.client;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -24,7 +26,9 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import org.springframework.core.codec.CodecException; import org.springframework.core.codec.CodecException;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
/** /**
* Internal methods shared between {@link DefaultWebClient} and * Internal methods shared between {@link DefaultWebClient} and
@ -64,4 +68,20 @@ abstract class WebClientUtils {
new ResponseEntity<>(list, response.headers().asHttpHeaders(), response.statusCode())); new ResponseEntity<>(list, response.headers().asHttpHeaders(), response.statusCode()));
} }
/**
* Return a String representation of the request details for logging purposes.
* @since 6.0.16
*/
public static String getRequestDescription(HttpMethod httpMethod, URI uri) {
if (StringUtils.hasText(uri.getQuery())) {
try {
uri = new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), null, null);
}
catch (URISyntaxException ex) {
// ignore
}
}
return httpMethod.name() + " " + uri;
}
} }

Loading…
Cancel
Save