From 2b602b04852b41e963830fffd39d2d2aa6889fc8 Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Sun, 28 Jul 2019 16:12:14 +0200 Subject: [PATCH 1/4] actuator httpTrace endpoint --- spring-boot-management/pom.xml | 47 +++++++++++++++++++ .../spring/boot/management/trace/App.java | 13 +++++ .../trace/CustomTraceRepository.java | 28 +++++++++++ .../boot/management/trace/EchoController.java | 15 ++++++ .../management/trace/TraceRequestFilter.java | 27 +++++++++++ .../src/main/resources/application.properties | 3 ++ 6 files changed, 133 insertions(+) create mode 100644 spring-boot-management/pom.xml create mode 100644 spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/App.java create mode 100644 spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java create mode 100644 spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/EchoController.java create mode 100644 spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/TraceRequestFilter.java create mode 100644 spring-boot-management/src/main/resources/application.properties diff --git a/spring-boot-management/pom.xml b/spring-boot-management/pom.xml new file mode 100644 index 0000000000..2b3e8f5d06 --- /dev/null +++ b/spring-boot-management/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + + com.baeldung + spring-boot-management + 0.0.1-SNAPSHOT + spring-boot-management + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/App.java b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/App.java new file mode 100644 index 0000000000..150e451c57 --- /dev/null +++ b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/App.java @@ -0,0 +1,13 @@ +package com.baeldung.spring.boot.management.trace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class App { + + public static void main(String[] args) { + SpringApplication.run(App.class); + } + +} diff --git a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java new file mode 100644 index 0000000000..13a0bdda60 --- /dev/null +++ b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java @@ -0,0 +1,28 @@ +package com.baeldung.spring.boot.management.trace; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.boot.actuate.trace.http.HttpTrace; +import org.springframework.boot.actuate.trace.http.HttpTraceRepository; +import org.springframework.stereotype.Repository; + +@Repository +public class CustomTraceRepository implements HttpTraceRepository { + + List traces = new ArrayList<>(1); + + @Override + public List findAll() { + return traces; + } + + @Override + public void add(HttpTrace trace) { + if ("GET".equals(trace.getRequest().getMethod())) { + traces.clear(); + traces.add(trace); + } + } + +} diff --git a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/EchoController.java b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/EchoController.java new file mode 100644 index 0000000000..70bc27a3bb --- /dev/null +++ b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/EchoController.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.boot.management.trace; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController("echo") +public class EchoController { + + @GetMapping + public String echo(@RequestParam("msg") String msg) { + return "echoing " + msg; + } + +} diff --git a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/TraceRequestFilter.java b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/TraceRequestFilter.java new file mode 100644 index 0000000000..fd6312df47 --- /dev/null +++ b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/TraceRequestFilter.java @@ -0,0 +1,27 @@ +package com.baeldung.spring.boot.management.trace; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; + +import org.springframework.boot.actuate.trace.http.HttpExchangeTracer; +import org.springframework.boot.actuate.trace.http.HttpTraceRepository; +import org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter; +import org.springframework.stereotype.Component; + +@Component +public class TraceRequestFilter extends HttpTraceFilter { + /** + * Create a new {@link HttpTraceFilter} instance. + * + * @param repository the trace repository + * @param tracer used to trace exchanges + */ + public TraceRequestFilter(HttpTraceRepository repository, HttpExchangeTracer tracer) { + super(repository, tracer); + } + + @Override + protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException { + return request.getServletPath().contains("actuator"); + } +} diff --git a/spring-boot-management/src/main/resources/application.properties b/spring-boot-management/src/main/resources/application.properties new file mode 100644 index 0000000000..b6c303afdb --- /dev/null +++ b/spring-boot-management/src/main/resources/application.properties @@ -0,0 +1,3 @@ +management.endpoints.web.exposure.include=httptrace +management.trace.http.include=REQUEST_HEADERS + From 07c159ddc19f3c3474de10d9eeb20708e6354d48 Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Tue, 30 Jul 2019 19:39:46 +0200 Subject: [PATCH 2/4] added synchronization to HttpTraceRepository --- .../management/trace/CustomTraceRepository.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java index 13a0bdda60..1478fc90a3 100644 --- a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java +++ b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java @@ -1,6 +1,7 @@ package com.baeldung.spring.boot.management.trace; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.springframework.boot.actuate.trace.http.HttpTrace; @@ -14,14 +15,19 @@ public class CustomTraceRepository implements HttpTraceRepository { @Override public List findAll() { - return traces; + synchronized (this.traces) { + return Collections.unmodifiableList(new ArrayList<>(this.traces)); + } } @Override public void add(HttpTrace trace) { - if ("GET".equals(trace.getRequest().getMethod())) { - traces.clear(); - traces.add(trace); + if ("GET".equals(trace.getRequest() + .getMethod())) { + synchronized (this.traces) { + traces.clear(); + traces.add(trace); + } } } From ebfee30bb9a841e9b58bb993fb845378927577d1 Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Mon, 5 Aug 2019 22:46:01 +0200 Subject: [PATCH 3/4] improvements Spring httpTrace + added spring-boot-management to main pom --- pom.xml | 1 + .../management/trace/CustomTraceRepository.java | 13 ++++--------- .../src/main/resources/application.properties | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 5b5058763d..ed181cd919 100644 --- a/pom.xml +++ b/pom.xml @@ -652,6 +652,7 @@ spring-boot-keycloak spring-boot-kotlin spring-boot-logging-log4j2 + spring-boot-management spring-boot-mvc spring-boot-mvc-birt spring-boot-ops diff --git a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java index 1478fc90a3..d85c043dc0 100644 --- a/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java +++ b/spring-boot-management/src/main/java/com/baeldung/spring/boot/management/trace/CustomTraceRepository.java @@ -1,8 +1,8 @@ package com.baeldung.spring.boot.management.trace; -import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; import org.springframework.boot.actuate.trace.http.HttpTrace; import org.springframework.boot.actuate.trace.http.HttpTraceRepository; @@ -11,23 +11,18 @@ import org.springframework.stereotype.Repository; @Repository public class CustomTraceRepository implements HttpTraceRepository { - List traces = new ArrayList<>(1); + AtomicReference lastTrace = new AtomicReference<>(); @Override public List findAll() { - synchronized (this.traces) { - return Collections.unmodifiableList(new ArrayList<>(this.traces)); - } + return Collections.singletonList(lastTrace.get()); } @Override public void add(HttpTrace trace) { if ("GET".equals(trace.getRequest() .getMethod())) { - synchronized (this.traces) { - traces.clear(); - traces.add(trace); - } + lastTrace.set(trace); } } diff --git a/spring-boot-management/src/main/resources/application.properties b/spring-boot-management/src/main/resources/application.properties index b6c303afdb..835ca64eac 100644 --- a/spring-boot-management/src/main/resources/application.properties +++ b/spring-boot-management/src/main/resources/application.properties @@ -1,3 +1,3 @@ management.endpoints.web.exposure.include=httptrace -management.trace.http.include=REQUEST_HEADERS +management.trace.http.include=RESPONSE_HEADERS From b8a8a7f984789fd7c00628de3030c9f49b6a7c24 Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Mon, 5 Aug 2019 23:47:32 +0200 Subject: [PATCH 4/4] added spring-boot-management to IT build --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index ed181cd919..80181a2c32 100644 --- a/pom.xml +++ b/pom.xml @@ -1326,6 +1326,7 @@ spring-boot-jasypt spring-boot-keycloak spring-boot-logging-log4j2 + spring-boot-management spring-boot-mvc spring-boot-mvc-birt spring-boot-ops