Updating flux status code depending on the exception thrown; fixes gh-1852

This commit is contained in:
Marcin Grzejszczak
2021-02-19 10:53:09 +01:00
2 changed files with 18 additions and 1 deletions

View File

@@ -40,6 +40,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
@@ -353,12 +354,16 @@ public class TraceWebFilter implements WebFilter, Ordered {
@Override
public int statusCode() {
HttpStatus statusCode = delegate.getStatusCode();
if (this.throwable != null && this.throwable instanceof ResponseStatusException) {
return ((ResponseStatusException) this.throwable).getRawStatusCode();
}
HttpStatus statusCode = this.delegate.getStatusCode();
return statusCode != null ? statusCode.value() : 0;
}
@Override
public Collection<String> headerNames() {
// TODO: As with the status code, these headers get rewritten later
return this.delegate.getHeaders().keySet();
}

View File

@@ -77,6 +77,12 @@ public class TraceWebFluxTests {
thenFunctionalSpanWasReportedWithTags(spans, response);
spans.clear();
// when
response = whenRequestIsSent(port, "/missing-endpoint");
// then
thenSpanWith404StatusCodeWasReported(spans, response);
spans.clear();
// when
ClientResponse nonSampledResponse = whenNonSampledRequestIsSent(port);
// then
@@ -125,6 +131,12 @@ public class TraceWebFluxTests {
value -> then(value).startsWith("TraceWebFluxTests$Config$$Lambda$"));
}
private void thenSpanWith404StatusCodeWasReported(TestSpanHandler spans, ClientResponse response) {
Awaitility.await().untilAsserted(() -> then(response.statusCode().value()).isEqualTo(404));
then(spans).hasSize(1);
then(spans.get(0).tags()).hasEntrySatisfying("http.status_code", value -> then(value).isEqualTo("404"));
}
private void thenNoSpanWasReported(TestSpanHandler spans, ClientResponse response, Controller2 controller2) {
Awaitility.await().untilAsserted(() -> {
then(response.statusCode().value()).isEqualTo(200);