diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/WebRequestTraceFilter.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/WebRequestTraceFilter.java index e0022530ea..cffdc5d6ac 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/WebRequestTraceFilter.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/WebRequestTraceFilter.java @@ -90,7 +90,6 @@ public class WebRequestTraceFilter implements Filter, Ordered { HttpServletResponse response = (HttpServletResponse) res; Map trace = getTrace(request); - this.traceRepository.add(trace); if (this.logger.isTraceEnabled()) { this.logger.trace("Processing request " + request.getMethod() + " " + request.getRequestURI()); @@ -108,12 +107,29 @@ public class WebRequestTraceFilter implements Filter, Ordered { } } - chain.doFilter(request, response); + try { + chain.doFilter(request, response); + } + finally { + enhanceTrace(trace, response); + this.traceRepository.add(trace); + } + } + + protected void enhanceTrace(Map trace, HttpServletResponse response) { + Map headers = new LinkedHashMap(); + for (String header : response.getHeaderNames()) { + String value = response.getHeader(header); + headers.put(header, value); + } + @SuppressWarnings("unchecked") + Map allHeaders = (Map) trace.get("headers"); + allHeaders.put("response", headers); } protected Map getTrace(HttpServletRequest request) { - Map map = new LinkedHashMap(); + Map headers = new LinkedHashMap(); Enumeration names = request.getHeaderNames(); while (names.hasMoreElements()) { @@ -126,13 +142,15 @@ public class WebRequestTraceFilter implements Filter, Ordered { else if (values.isEmpty()) { value = ""; } - map.put(name, value); + headers.put(name, value); } Map trace = new LinkedHashMap(); + Map allHeaders = new LinkedHashMap(); + allHeaders.put("request", headers); trace.put("method", request.getMethod()); trace.put("path", request.getRequestURI()); - trace.put("headers", map); + trace.put("headers", allHeaders); return trace; } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/WebRequestTraceFilterTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/WebRequestTraceFilterTests.java index cade5c64c9..ec97771d6d 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/WebRequestTraceFilterTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/trace/WebRequestTraceFilterTests.java @@ -19,9 +19,8 @@ package org.springframework.boot.actuate.trace; import java.util.Map; import org.junit.Test; -import org.springframework.boot.actuate.trace.InMemoryTraceRepository; -import org.springframework.boot.actuate.trace.WebRequestTraceFilter; import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; import static org.junit.Assert.assertEquals; @@ -42,6 +41,20 @@ public class WebRequestTraceFilterTests { Map trace = this.filter.getTrace(request); assertEquals("GET", trace.get("method")); assertEquals("/foo", trace.get("path")); - assertEquals("{Accept=application/json}", trace.get("headers").toString()); + @SuppressWarnings("unchecked") + Map map = (Map) trace.get("headers"); + assertEquals("{Accept=application/json}", map.get("request").toString()); + } + + @Test + public void filterDumpsResponse() { + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); + MockHttpServletResponse response = new MockHttpServletResponse(); + response.addHeader("Content-Type", "application/json"); + Map trace = this.filter.getTrace(request); + this.filter.enhanceTrace(trace, response); + @SuppressWarnings("unchecked") + Map map = (Map) trace.get("headers"); + assertEquals("{Content-Type=application/json}", map.get("response").toString()); } }