From a53182f17640de81470c1b2e8522972c8d5c1469 Mon Sep 17 00:00:00 2001 From: SylvainJuge <763082+SylvainJuge@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:07:46 +0100 Subject: [PATCH] cleanup servlet request attribute (#3527) --- CHANGELOG.asciidoc | 4 ++++ .../apm/agent/servlet/ServletApiAdvice.java | 22 +++++++++++++++---- .../adapter/JakartaServletApiAdapter.java | 5 ++--- .../adapter/JavaxServletApiAdapter.java | 5 ++--- .../adapter/ServletRequestAdapter.java | 3 +-- ...trumentationWithExceptionResolverTest.java | 13 +++++++++++ 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 4d5a7a312e..589b635329 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -31,6 +31,10 @@ Use subheadings with the "=====" level for adding notes for unreleased changes: === Unreleased +[float] +===== Bug fixes +* Cleanup extra servlet request attribute used for Spring exception handler - {pull}3527[#3527] + [[release-notes-1.x]] === Java Agent version 1.x diff --git a/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletApiAdvice.java b/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletApiAdvice.java index fa41a89bb0..c52377be12 100644 --- a/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletApiAdvice.java +++ b/apm-agent-plugins/apm-servlet-plugin/src/main/java/co/elastic/apm/agent/servlet/ServletApiAdvice.java @@ -55,7 +55,15 @@ public abstract class ServletApiAdvice { private static final DetachedThreadLocal servletPathTL = GlobalVariables.get(ServletApiAdvice.class, "servletPath", WeakConcurrent.buildThreadLocal()); private static final DetachedThreadLocal pathInfoTL = GlobalVariables.get(ServletApiAdvice.class, "pathInfo", WeakConcurrent.buildThreadLocal()); - private static final List requestExceptionAttributes = Arrays.asList("javax.servlet.error.exception", "jakarta.servlet.error.exception", "exception", "org.springframework.web.servlet.DispatcherServlet.EXCEPTION", "co.elastic.apm.exception"); + private static final String ELASTIC_EXCEPTION = "co.elastic.apm.exception"; + private static final String JAVAX_ERROR_EXCEPTION = "javax.servlet.error.exception"; + private static final String JAKARTA_ERROR_EXCEPTION = "jakarta.servlet.error.exception"; + private static final List requestExceptionAttributes = Arrays.asList( + JAVAX_ERROR_EXCEPTION, + JAKARTA_ERROR_EXCEPTION, + "exception", + "org.springframework.web.servlet.DispatcherServlet.EXCEPTION", + ELASTIC_EXCEPTION); @Nullable public static Object onServletEnter( @@ -202,7 +210,7 @@ public static { void setAttribute(HttpServletRequest request, String attributeName, Object value); - @Nullable - Object getHttpAttribute(HttpServletRequest request, String attributeName); + void removeAttribute(HttpServletRequest request, String attributeName); Map getParameterMap(HttpServletRequest httpServletRequest); diff --git a/apm-agent-plugins/apm-spring-webmvc/apm-spring-webmvc-spring5/src/test/java/co/elastic/apm/agent/springwebmvc/exception/AbstractExceptionHandlerInstrumentationWithExceptionResolverTest.java b/apm-agent-plugins/apm-spring-webmvc/apm-spring-webmvc-spring5/src/test/java/co/elastic/apm/agent/springwebmvc/exception/AbstractExceptionHandlerInstrumentationWithExceptionResolverTest.java index 1f62d500d2..0800056204 100644 --- a/apm-agent-plugins/apm-spring-webmvc/apm-spring-webmvc-spring5/src/test/java/co/elastic/apm/agent/springwebmvc/exception/AbstractExceptionHandlerInstrumentationWithExceptionResolverTest.java +++ b/apm-agent-plugins/apm-spring-webmvc/apm-spring-webmvc-spring5/src/test/java/co/elastic/apm/agent/springwebmvc/exception/AbstractExceptionHandlerInstrumentationWithExceptionResolverTest.java @@ -26,6 +26,9 @@ import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultActions; +import java.util.Enumeration; + +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -45,6 +48,16 @@ public void testCallApiWithExceptionThrown() throws Exception { MvcResult result = resultActions.andReturn(); MockHttpServletResponse response = result.getResponse(); + + Enumeration attributeNames = result.getRequest().getAttributeNames(); + while (attributeNames.hasMoreElements()) { + String attributeName = attributeNames.nextElement(); + assertThat(attributeName) + .describedAs("elastic attributes should be removed after usage") + .doesNotStartWith("co.elastic."); + } + + assertExceptionCapture(ExceptionResolverRuntimeException.class, response, 200, "", "runtime exception occurred", "View#render error-page"); assertEquals("error-page", response.getForwardedUrl()); assertEquals("runtime exception occurred", result.getModelAndView().getModel().get("message"));