Skip to content

Commit 9b8decf

Browse files
committed
Merge pull request #17998 from dreis2211
* gh-17998: Improve handling of non-standard status codes in WebMvcTags Closes gh-17998
2 parents f46a03d + a2a672d commit 9b8decf

File tree

2 files changed

+23
-10
lines changed
  • spring-boot-project/spring-boot-actuator/src

2 files changed

+23
-10
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,21 @@ public static Tag exception(Throwable exception) {
175175
*/
176176
public static Tag outcome(HttpServletResponse response) {
177177
if (response != null) {
178-
HttpStatus status = extractStatus(response);
179-
if (status != null) {
180-
if (status.is1xxInformational()) {
178+
HttpStatus.Series series = HttpStatus.Series.resolve(response.getStatus());
179+
if (series != null) {
180+
switch (series) {
181+
case INFORMATIONAL:
181182
return OUTCOME_INFORMATIONAL;
182-
}
183-
if (status.is2xxSuccessful()) {
183+
case SUCCESSFUL:
184184
return OUTCOME_SUCCESS;
185-
}
186-
if (status.is3xxRedirection()) {
185+
case REDIRECTION:
187186
return OUTCOME_REDIRECTION;
188-
}
189-
if (status.is4xxClientError()) {
187+
case CLIENT_ERROR:
190188
return OUTCOME_CLIENT_ERROR;
189+
case SERVER_ERROR:
190+
return OUTCOME_SERVER_ERROR;
191191
}
192192
}
193-
return OUTCOME_SERVER_ERROR;
194193
}
195194
return OUTCOME_UNKNOWN;
196195
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,25 @@ void outcomeTagIsClientErrorWhenResponseIs4xx() {
136136
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
137137
}
138138

139+
@Test
140+
void outcomeTagIsClientErrorWhenResponseIsNonStandardInClientSeries() {
141+
this.response.setStatus(490);
142+
Tag tag = WebMvcTags.outcome(this.response);
143+
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
144+
}
145+
139146
@Test
140147
void outcomeTagIsServerErrorWhenResponseIs5xx() {
141148
this.response.setStatus(500);
142149
Tag tag = WebMvcTags.outcome(this.response);
143150
assertThat(tag.getValue()).isEqualTo("SERVER_ERROR");
144151
}
145152

153+
@Test
154+
void outcomeTagIsUnknownWhenResponseStatusIsInUnknownSeries() {
155+
this.response.setStatus(701);
156+
Tag tag = WebMvcTags.outcome(this.response);
157+
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
158+
}
159+
146160
}

0 commit comments

Comments
 (0)