diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java index df4fa605f8..72341d7c58 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/main/java/co/elastic/apm/agent/springwebflux/WebfluxHelper.java @@ -111,6 +111,10 @@ public static Transaction getOrCreateTransaction(Tracer tracer, ServerWebExch return null; } + if (!fromServlet) { + // Errors captured during dispatch copy the request context immediately. + fillRequest(transaction, exchange); + } transaction.withType("request").activate(); // store transaction in exchange to make it easy to retrieve from other handlers @@ -172,17 +176,15 @@ public static void endTransaction(@Nullable Throwable thrown, @Nullable Transact return; } - transaction.captureException(thrown); - - // Fill request/response details if they haven't been already by another HTTP plugin (servlet or other). - if (!transaction.getContext().getRequest().hasContent()) { - fillRequest(transaction, exchange); + boolean fromServlet = isServletTransaction(exchange); + if (!fromServlet) { fillResponse(transaction, exchange); } + transaction.captureException(thrown); // In case transaction has been created by Servlet, we should not terminate it as the Servlet instrumentation // will take care of this. - if (!WebfluxHelper.isServletTransaction(exchange)) { + if (!fromServlet) { transaction.end(); } } diff --git a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/test/java/co/elastic/apm/agent/springwebflux/AbstractServerInstrumentationTest.java b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/test/java/co/elastic/apm/agent/springwebflux/AbstractServerInstrumentationTest.java index f3393a10f1..c1ca6a991d 100644 --- a/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/test/java/co/elastic/apm/agent/springwebflux/AbstractServerInstrumentationTest.java +++ b/apm-agent-plugins/apm-spring-webflux/apm-spring-webflux-spring5/src/test/java/co/elastic/apm/agent/springwebflux/AbstractServerInstrumentationTest.java @@ -114,6 +114,23 @@ void dispatchHello() { hello(true); } + @ParameterizedTest + @CsvSource({"01, true", "00, false"}) + void unmappedRequestErrorIncludesHttpContext(String traceFlags, boolean sampled) { + client.setHeader("traceparent", "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-" + traceFlags); + StepVerifier.create(client.getMappingError404()) + .expectErrorMatches(expectClientError(404)) + .verify(); + + ErrorCaptureImpl error = getFirstError(); + assertThat(error.getTransactionInfo().isSampled()).isEqualTo(sampled); + RequestImpl request = error.getContext().getRequest(); + assertThat(request.getMethod()).isEqualTo("GET"); + assertThat(request.getUrl().getHostname()).isEqualTo("localhost"); + assertThat(request.getUrl().getPathname()).isEqualTo(client.getPathPrefix() + "/error-404"); + assertThat(error.getContext().getResponse().getStatusCode()).isEqualTo(404); + } + private void hello(boolean expectHeaders) { client.setHeader("random-value", "12345"); client.setCookie("cookie", "gdpr-compliant-no-chocolate-here");