Skip to content

RestTemplateExchangeTags does not handle non-standard status codes #17991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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 @@ -18,6 +18,9 @@

import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import io.micrometer.core.instrument.Tag;
Expand Down Expand Up @@ -54,6 +57,18 @@ public final class RestTemplateExchangeTags {

private static final Tag OUTCOME_SERVER_ERROR = Tag.of("outcome", "SERVER_ERROR");

private static final Map<HttpStatus.Series, Tag> SERIES_OUTCOMES;

static {
Map<HttpStatus.Series, Tag> seriesOutcomes = new HashMap<>();
seriesOutcomes.put(HttpStatus.Series.INFORMATIONAL, OUTCOME_INFORMATIONAL);
seriesOutcomes.put(HttpStatus.Series.SUCCESSFUL, OUTCOME_SUCCESS);
seriesOutcomes.put(HttpStatus.Series.REDIRECTION, OUTCOME_REDIRECTION);
seriesOutcomes.put(HttpStatus.Series.CLIENT_ERROR, OUTCOME_CLIENT_ERROR);
seriesOutcomes.put(HttpStatus.Series.SERVER_ERROR, OUTCOME_SERVER_ERROR);
SERIES_OUTCOMES = Collections.unmodifiableMap(seriesOutcomes);
}

private RestTemplateExchangeTags() {
}

Expand Down Expand Up @@ -132,36 +147,24 @@ public static Tag clientName(HttpRequest request) {

/**
* Creates an {@code outcome} {@code Tag} derived from the
* {@link ClientHttpResponse#getStatusCode() status} of the given {@code response}.
* {@link ClientHttpResponse#getRawStatusCode() status} of the given {@code response}.
* @param response the response
* @return the outcome tag
* @since 2.2.0
*/
public static Tag outcome(ClientHttpResponse response) {
try {
if (response != null) {
HttpStatus statusCode = response.getStatusCode();
if (statusCode.is1xxInformational()) {
return OUTCOME_INFORMATIONAL;
}
if (statusCode.is2xxSuccessful()) {
return OUTCOME_SUCCESS;
}
if (statusCode.is3xxRedirection()) {
return OUTCOME_REDIRECTION;
}
if (statusCode.is4xxClientError()) {
return OUTCOME_CLIENT_ERROR;
}
if (statusCode.is5xxServerError()) {
return OUTCOME_SERVER_ERROR;
HttpStatus.Series series = HttpStatus.Series.resolve(response.getRawStatusCode());
if (series != null) {
return SERIES_OUTCOMES.getOrDefault(series, OUTCOME_UNKNOWN);
}
}
return OUTCOME_UNKNOWN;
}
catch (IOException | IllegalArgumentException ex) {
return OUTCOME_UNKNOWN;
// Continue
}
return OUTCOME_UNKNOWN;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static Tag clientName(ClientRequest request) {

/**
* Creates an {@code outcome} {@code Tag} derived from the
* {@link ClientResponse#statusCode() status} of the given {@code response}.
* {@link ClientResponse#rawStatusCode() status} of the given {@code response}.
* @param response the response
* @return the outcome tag
* @since 2.2.0
Expand All @@ -154,7 +154,7 @@ public static Tag outcome(ClientResponse response) {
}
}
}
catch (IllegalArgumentException exc) {
catch (IllegalArgumentException ex) {
// Continue
}
return OUTCOME_UNKNOWN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,31 @@ void outcomeTagIsServerErrorWhenResponseIs5xx() {
@Test
void outcomeTagIsUnknownWhenResponseThrowsIOException() throws Exception {
ClientHttpResponse response = mock(ClientHttpResponse.class);
given(response.getStatusCode()).willThrow(IOException.class);
given(response.getRawStatusCode()).willThrow(IOException.class);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

@Test
void outcomeTagIsUnknownForCustomResponseStatus() throws Exception {
ClientHttpResponse response = mock(ClientHttpResponse.class);
given(response.getStatusCode()).willThrow(IllegalArgumentException.class);
given(response.getRawStatusCode()).willThrow(IllegalArgumentException.class);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

@Test
void outcomeTagIsClientErrorWhenResponseIsNonStandardInKnownSeries() throws IOException {
ClientHttpResponse response = mock(ClientHttpResponse.class);
given(response.getRawStatusCode()).willReturn(490);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
}

@Test
void outcomeTagIsUnknownWhenResponseStatusIsInUnknownSeries() throws IOException {
ClientHttpResponse response = mock(ClientHttpResponse.class);
given(response.getRawStatusCode()).willReturn(701);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ void outcomeTagIsServerErrorWhenResponseIs5xx() {
}

@Test
void outcomeTagIsServerErrorWhenResponseIsNonStandardInKnownSeries() {
void outcomeTagIsClientErrorWhenResponseIsNonStandardInKnownSeries() {
given(this.response.rawStatusCode()).willReturn(490);
Tag tag = WebClientExchangeTags.outcome(this.response);
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
Expand Down