Skip to content

Commit

Permalink
Improve handling of non-standard status codes in WebFluxTags
Browse files Browse the repository at this point in the history
Closes gh-18267
  • Loading branch information
wilkinsona committed Sep 18, 2019
1 parent 6534047 commit b15e427
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.springframework.boot.actuate.metrics.http.Outcome;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.AbstractServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.server.ServerWebExchange;
Expand Down Expand Up @@ -133,9 +135,18 @@ public static Tag exception(Throwable exception) {
* @since 2.1.0
*/
public static Tag outcome(ServerWebExchange exchange) {
HttpStatus status = exchange.getResponse().getStatusCode();
Outcome outcome = (status != null) ? Outcome.forStatus(status.value()) : Outcome.UNKNOWN;
Integer statusCode = extractStatusCode(exchange);
Outcome outcome = (statusCode != null) ? Outcome.forStatus(statusCode) : Outcome.UNKNOWN;
return outcome.asTag();
}

private static Integer extractStatusCode(ServerWebExchange exchange) {
ServerHttpResponse response = exchange.getResponse();
if (response instanceof AbstractServerHttpResponse) {
return ((AbstractServerHttpResponse) response).getStatusCodeValue();
}
HttpStatus status = response.getStatusCode();
return (status != null) ? status.value() : null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,18 @@ void outcomeTagIsServerErrorWhenResponseIs5xx() {
assertThat(tag.getValue()).isEqualTo("SERVER_ERROR");
}

@Test
void outcomeTagIsClientErrorWhenResponseIsNonStandardInClientSeries() {
this.exchange.getResponse().setStatusCodeValue(490);
Tag tag = WebFluxTags.outcome(this.exchange);
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
}

@Test
void outcomeTagIsUnknownWhenResponseStatusIsInUnknownSeries() {
this.exchange.getResponse().setStatusCodeValue(701);
Tag tag = WebFluxTags.outcome(this.exchange);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

}

0 comments on commit b15e427

Please sign in to comment.