Skip to content

Commit

Permalink
Exclude query from URI in WebClient checkpoints
Browse files Browse the repository at this point in the history
Closes gh-31992
  • Loading branch information
rstoyanchev committed Jan 10, 2024
1 parent 4a599d0 commit 6df8be8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -212,7 +212,7 @@ public ClientResponse build() {

return new DefaultClientResponse(httpResponse, this.strategies,
this.originalResponse != null ? this.originalResponse.logPrefix() : "",
this.request.getMethod() + " " + this.request.getURI(),
WebClientUtils.getRequestDescription(this.request.getMethod(), this.request.getURI()),
() -> this.request);
}

Expand Down
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,6 @@
package org.springframework.web.reactive.function.client;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -54,7 +53,6 @@
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.BodyExtractor;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
Expand Down Expand Up @@ -457,7 +455,9 @@ public Mono<ClientResponse> exchange() {
observationContext.setRequest(request);
Mono<ClientResponse> responseMono = filterFunction.apply(exchangeFunction)
.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);
if (this.contextModifier != null) {
responseMono = responseMono.contextWrite(this.contextModifier);
Expand Down Expand Up @@ -693,24 +693,13 @@ private <T> Mono<T> applyStatusHandlers(ClientResponse response) {
}
Mono<T> result = exMono.flatMap(Mono::error);
return result.checkpoint(statusCode + " from " +
this.httpMethod + " " + getUriToLog(this.uri) + " [DefaultWebClient]");
WebClientUtils.getRequestDescription(this.httpMethod, this.uri) +
" [DefaultWebClient]");
}
}
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 {

Expand Down
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -97,8 +97,8 @@ public WebClientResponseException(
}

private static String initMessage(HttpStatusCode status, String reasonPhrase, @Nullable HttpRequest request) {
return status.value() + " " + reasonPhrase +
(request != null ? " from " + request.getMethod() + " " + request.getURI() : "");
return status.value() + " " + reasonPhrase + (request != null ?
" from " + WebClientUtils.getRequestDescription(request.getMethod(), request.getURI()) : "");
}

/**
Expand Down
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@

package org.springframework.web.reactive.function.client;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.function.Predicate;

Expand All @@ -24,7 +26,9 @@
import reactor.core.publisher.Mono;

import org.springframework.core.codec.CodecException;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;

/**
* Internal methods shared between {@link DefaultWebClient} and
Expand Down Expand Up @@ -64,4 +68,20 @@ public static <T> Mono<ResponseEntity<List<T>>> mapToEntityList(ClientResponse r
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;
}

}

0 comments on commit 6df8be8

Please sign in to comment.