Skip to content

Commit 142270d

Browse files
committed
Merge pull request #17991 from izeye
* gh-17991: Polish "Improve handling of non-standard status codes in RestTemplate metrics" Improve handling of non-standard status codes in RestTemplate metrics Closes gh-17991
2 parents 7f8b3a7 + 0217de4 commit 142270d

File tree

4 files changed

+42
-23
lines changed

4 files changed

+42
-23
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/RestTemplateExchangeTags.java

+21-18
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import java.io.IOException;
2020
import java.net.URI;
21+
import java.util.Collections;
22+
import java.util.HashMap;
23+
import java.util.Map;
2124
import java.util.regex.Pattern;
2225

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

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

60+
private static final Map<HttpStatus.Series, Tag> SERIES_OUTCOMES;
61+
62+
static {
63+
Map<HttpStatus.Series, Tag> seriesOutcomes = new HashMap<>();
64+
seriesOutcomes.put(HttpStatus.Series.INFORMATIONAL, OUTCOME_INFORMATIONAL);
65+
seriesOutcomes.put(HttpStatus.Series.SUCCESSFUL, OUTCOME_SUCCESS);
66+
seriesOutcomes.put(HttpStatus.Series.REDIRECTION, OUTCOME_REDIRECTION);
67+
seriesOutcomes.put(HttpStatus.Series.CLIENT_ERROR, OUTCOME_CLIENT_ERROR);
68+
seriesOutcomes.put(HttpStatus.Series.SERVER_ERROR, OUTCOME_SERVER_ERROR);
69+
SERIES_OUTCOMES = Collections.unmodifiableMap(seriesOutcomes);
70+
}
71+
5772
private RestTemplateExchangeTags() {
5873
}
5974

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

133148
/**
134149
* Creates an {@code outcome} {@code Tag} derived from the
135-
* {@link ClientHttpResponse#getStatusCode() status} of the given {@code response}.
150+
* {@link ClientHttpResponse#getRawStatusCode() status} of the given {@code response}.
136151
* @param response the response
137152
* @return the outcome tag
138153
* @since 2.2.0
139154
*/
140155
public static Tag outcome(ClientHttpResponse response) {
141156
try {
142157
if (response != null) {
143-
HttpStatus statusCode = response.getStatusCode();
144-
if (statusCode.is1xxInformational()) {
145-
return OUTCOME_INFORMATIONAL;
146-
}
147-
if (statusCode.is2xxSuccessful()) {
148-
return OUTCOME_SUCCESS;
149-
}
150-
if (statusCode.is3xxRedirection()) {
151-
return OUTCOME_REDIRECTION;
152-
}
153-
if (statusCode.is4xxClientError()) {
154-
return OUTCOME_CLIENT_ERROR;
155-
}
156-
if (statusCode.is5xxServerError()) {
157-
return OUTCOME_SERVER_ERROR;
158+
HttpStatus.Series series = HttpStatus.Series.resolve(response.getRawStatusCode());
159+
if (series != null) {
160+
return SERIES_OUTCOMES.getOrDefault(series, OUTCOME_UNKNOWN);
158161
}
159162
}
160-
return OUTCOME_UNKNOWN;
161163
}
162164
catch (IOException | IllegalArgumentException ex) {
163-
return OUTCOME_UNKNOWN;
165+
// Continue
164166
}
167+
return OUTCOME_UNKNOWN;
165168
}
166169

167170
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTags.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public static Tag clientName(ClientRequest request) {
140140

141141
/**
142142
* Creates an {@code outcome} {@code Tag} derived from the
143-
* {@link ClientResponse#statusCode() status} of the given {@code response}.
143+
* {@link ClientResponse#rawStatusCode() status} of the given {@code response}.
144144
* @param response the response
145145
* @return the outcome tag
146146
* @since 2.2.0
@@ -154,7 +154,7 @@ public static Tag outcome(ClientResponse response) {
154154
}
155155
}
156156
}
157-
catch (IllegalArgumentException exc) {
157+
catch (IllegalArgumentException ex) {
158158
// Continue
159159
}
160160
return OUTCOME_UNKNOWN;

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/client/RestTemplateExchangeTagsTests.java

+18-2
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,31 @@ void outcomeTagIsServerErrorWhenResponseIs5xx() {
8181
@Test
8282
void outcomeTagIsUnknownWhenResponseThrowsIOException() throws Exception {
8383
ClientHttpResponse response = mock(ClientHttpResponse.class);
84-
given(response.getStatusCode()).willThrow(IOException.class);
84+
given(response.getRawStatusCode()).willThrow(IOException.class);
8585
Tag tag = RestTemplateExchangeTags.outcome(response);
8686
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
8787
}
8888

8989
@Test
9090
void outcomeTagIsUnknownForCustomResponseStatus() throws Exception {
9191
ClientHttpResponse response = mock(ClientHttpResponse.class);
92-
given(response.getStatusCode()).willThrow(IllegalArgumentException.class);
92+
given(response.getRawStatusCode()).willThrow(IllegalArgumentException.class);
93+
Tag tag = RestTemplateExchangeTags.outcome(response);
94+
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
95+
}
96+
97+
@Test
98+
void outcomeTagIsClientErrorWhenResponseIsNonStandardInClientSeries() throws IOException {
99+
ClientHttpResponse response = mock(ClientHttpResponse.class);
100+
given(response.getRawStatusCode()).willReturn(490);
101+
Tag tag = RestTemplateExchangeTags.outcome(response);
102+
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
103+
}
104+
105+
@Test
106+
void outcomeTagIsUnknownWhenResponseStatusIsInUnknownSeries() throws IOException {
107+
ClientHttpResponse response = mock(ClientHttpResponse.class);
108+
given(response.getRawStatusCode()).willReturn(701);
93109
Tag tag = RestTemplateExchangeTags.outcome(response);
94110
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
95111
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTagsTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void outcomeTagIsServerErrorWhenResponseIs5xx() {
148148
}
149149

150150
@Test
151-
void outcomeTagIsServerErrorWhenResponseIsNonStandardInKnownSeries() {
151+
void outcomeTagIsClientErrorWhenResponseIsNonStandardInClientSeries() {
152152
given(this.response.rawStatusCode()).willReturn(490);
153153
Tag tag = WebClientExchangeTags.outcome(this.response);
154154
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");

0 commit comments

Comments
 (0)