From 5ab1c13f0cacd7d46b8528427c825f16fbfac216 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Tue, 30 Mar 2021 13:22:35 +0200 Subject: [PATCH] Assuming 500 status code when an exception was thrown (#1889) * Assuming 500 status code when an exception was thrown in the controller; fixes gh-1880 * Ensures that the response is committed --- .../sleuth/instrument/web/TraceWebFilter.java | 7 +++++-- .../brave/instrument/web/TraceWebFluxTests.java | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebFilter.java b/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebFilter.java index 4fb8edd66..c2ed27cc8 100644 --- a/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebFilter.java +++ b/spring-cloud-sleuth-instrumentation/src/main/java/org/springframework/cloud/sleuth/instrument/web/TraceWebFilter.java @@ -415,8 +415,11 @@ public class TraceWebFilter implements WebFilter, Ordered, ApplicationContextAwa @Override public int statusCode() { - if (this.throwable != null && this.throwable instanceof ResponseStatusException) { - return ((ResponseStatusException) this.throwable).getRawStatusCode(); + if (!this.delegate.isCommitted() && this.throwable != null) { + if (this.throwable instanceof ResponseStatusException) { + return ((ResponseStatusException) this.throwable).getRawStatusCode(); + } + return HttpStatus.INTERNAL_SERVER_ERROR.value(); } HttpStatus statusCode = this.delegate.getStatusCode(); return statusCode != null ? statusCode.value() : 0; diff --git a/tests/brave/spring-cloud-sleuth-instrumentation-webflux-tests/src/test/java/org/springframework/cloud/sleuth/brave/instrument/web/TraceWebFluxTests.java b/tests/brave/spring-cloud-sleuth-instrumentation-webflux-tests/src/test/java/org/springframework/cloud/sleuth/brave/instrument/web/TraceWebFluxTests.java index 4cfdd8073..294c0e700 100644 --- a/tests/brave/spring-cloud-sleuth-instrumentation-webflux-tests/src/test/java/org/springframework/cloud/sleuth/brave/instrument/web/TraceWebFluxTests.java +++ b/tests/brave/spring-cloud-sleuth-instrumentation-webflux-tests/src/test/java/org/springframework/cloud/sleuth/brave/instrument/web/TraceWebFluxTests.java @@ -83,6 +83,12 @@ public class TraceWebFluxTests { thenSpanWith404StatusCodeWasReported(spans, response); spans.clear(); + // when + response = whenRequestIsSent(port, "/exception"); + // then + thenSpanWithExceptionWasReported(spans, response); + spans.clear(); + // when ClientResponse nonSampledResponse = whenNonSampledRequestIsSent(port); // then @@ -137,6 +143,12 @@ public class TraceWebFluxTests { then(spans.get(0).tags()).hasEntrySatisfying("http.status_code", value -> then(value).isEqualTo("404")); } + private void thenSpanWithExceptionWasReported(TestSpanHandler spans, ClientResponse response) { + Awaitility.await().untilAsserted(() -> then(response.statusCode().value()).isEqualTo(500)); + then(spans).hasSize(1); + then(spans.get(0).tags()).hasEntrySatisfying("http.status_code", value -> then(value).isEqualTo("500")); + } + private void thenNoSpanWasReported(TestSpanHandler spans, ClientResponse response, Controller2 controller2) { Awaitility.await().untilAsserted(() -> { then(response.statusCode().value()).isEqualTo(200); @@ -229,6 +241,11 @@ public class TraceWebFluxTests { return Flux.just(sampled.toString()); } + @GetMapping("/exception") + public Flux exception() { + throw new RuntimeException("Exception"); + } + } }