From d696c4d236b0c7bc6b625baa2bd5d29cf707422d Mon Sep 17 00:00:00 2001 From: DOHA Date: Thu, 26 Mar 2015 20:25:44 +0200 Subject: [PATCH] Add spring boot metrics --- spring-security-mvc-ldap/pom.xml | 6 +- .../org/baeldung/SampleLDAPApplication.java | 3 + .../org/baeldung/controller/MyController.java | 13 +++ .../org/baeldung/metric/MetricFilter.java | 36 ++++++++ .../org/baeldung/metric/MetricService.java | 89 +++++++++++++++++++ .../src/main/resources/templates/graph.html | 42 +++++++++ .../org/baeldung/web/metric/MetricFilter.java | 4 +- .../baeldung/web/metric/MetricService.java | 9 +- 8 files changed, 197 insertions(+), 5 deletions(-) create mode 100644 spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java create mode 100644 spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java create mode 100644 spring-security-mvc-ldap/src/main/resources/templates/graph.html diff --git a/spring-security-mvc-ldap/pom.xml b/spring-security-mvc-ldap/pom.xml index 12353021f0..4c0f7adf9b 100644 --- a/spring-security-mvc-ldap/pom.xml +++ b/spring-security-mvc-ldap/pom.xml @@ -25,7 +25,11 @@ org.springframework.boot spring-boot-starter-thymeleaf - + + org.springframework.boot + spring-boot-starter-actuator + + org.springframework.security diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java index e380ea9602..748d9c703e 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java @@ -3,6 +3,7 @@ package org.baeldung; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; +import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @@ -11,6 +12,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter * class to run up a Jetty Server (on http://localhost:8080) * */ +@EnableScheduling @EnableAutoConfiguration @ComponentScan("org.baeldung") public class SampleLDAPApplication extends WebMvcConfigurerAdapter { @@ -22,6 +24,7 @@ public class SampleLDAPApplication extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/login").setViewName("login"); + registry.addViewController("/graph").setViewName("graph"); } } \ No newline at end of file diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java index e347bd8633..08a274a956 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java @@ -7,11 +7,15 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.baeldung.metric.MetricService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; /** * Spring Controller Definitions. @@ -19,6 +23,9 @@ import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyController { + @Autowired + private MetricService metricService; + @RequestMapping("/") public String init(Map model, Principal principal) { model.put("title", "PUBLIC AREA"); @@ -37,6 +44,12 @@ public class MyController { return "home"; } + @RequestMapping(value = "/metric-graph-data", method = RequestMethod.GET) + @ResponseBody + public Object[][] getMetricData() { + return metricService.getGraphData(); + } + private String getUserName(Principal principal) { if (principal == null) { return "anonymous"; diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java new file mode 100644 index 0000000000..2c76001889 --- /dev/null +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricFilter.java @@ -0,0 +1,36 @@ +package org.baeldung.metric; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class MetricFilter implements Filter { + + @Autowired + private MetricService metricService; + + @Override + public void init(final FilterConfig config) throws ServletException { + } + + @Override + public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws java.io.IOException, ServletException { + chain.doFilter(request, response); + + final int status = ((HttpServletResponse) response).getStatus(); + metricService.increaseCount(status); + } + + @Override + public void destroy() { + + } +} diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java new file mode 100644 index 0000000000..9b1e100ce1 --- /dev/null +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/metric/MetricService.java @@ -0,0 +1,89 @@ +package org.baeldung.metric; + +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.metrics.CounterService; +import org.springframework.boot.actuate.metrics.Metric; +import org.springframework.boot.actuate.metrics.repository.MetricRepository; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +@Service +public class MetricService { + + @Autowired + private MetricRepository repo; + + @Autowired + private CounterService counter; + + private ArrayList> statusMetric; + private List statusList; + + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + + public MetricService() { + super(); + statusMetric = new ArrayList>(); + statusList = new ArrayList(); + } + + public void increaseCount(final int status) { + counter.increment("status." + status); + if (!statusList.contains("counter.status." + status)) { + statusList.add("counter.status." + status); + } + } + + @Scheduled(fixedDelay = 60000) + private void exportMetrics() { + Metric metric; + ArrayList statusCount = new ArrayList(); + for (String status : statusList) { + metric = repo.findOne(status); + if (metric != null) { + statusCount.add(metric.getValue().intValue()); + repo.reset(status); + } else { + statusCount.add(0); + } + + } + statusMetric.add(statusCount); + } + + public Object[][] getGraphData() { + Date current = new Date(); + int colCount = statusList.size() + 1; + int rowCount = statusMetric.size() + 1; + + Object[][] result = new Object[rowCount][colCount]; + result[0][0] = "Time"; + + int j = 1; + for (final String status : statusList) { + result[0][j] = status; + j++; + } + + ArrayList temp; + for (int i = 1; i < rowCount; i++) { + temp = statusMetric.get(i - 1); + result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i)))); + for (j = 1; j <= temp.size(); j++) { + result[i][j] = temp.get(j - 1); + } + while (j < colCount) { + result[i][j] = 0; + j++; + } + } + + return result; + } + +} diff --git a/spring-security-mvc-ldap/src/main/resources/templates/graph.html b/spring-security-mvc-ldap/src/main/resources/templates/graph.html new file mode 100644 index 0000000000..1064068f06 --- /dev/null +++ b/spring-security-mvc-ldap/src/main/resources/templates/graph.html @@ -0,0 +1,42 @@ + + +Metric Graph + + + + + +
+ + \ No newline at end of file diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java index e915a54465..13988858b4 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java @@ -9,13 +9,15 @@ import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.springframework.web.context.support.WebApplicationContextUtils; + public class MetricFilter implements Filter { private MetricService metricService; @Override public void init(final FilterConfig config) throws ServletException { - metricService = new MetricService(); + metricService = (MetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("metricService"); } @Override diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java index 677c73df49..1ac1eefed8 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java @@ -11,13 +11,16 @@ import org.springframework.stereotype.Service; @Service public class MetricService { - private static HashMap> metricMap = new HashMap>(); - private static HashMap statusMetric = new HashMap(); - private static HashMap> timeMap = new HashMap>(); + private HashMap> metricMap; + private HashMap statusMetric; + private HashMap> timeMap; private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public MetricService() { super(); + metricMap = new HashMap>(); + statusMetric = new HashMap(); + timeMap = new HashMap>(); } public void increaseCount(final String request, final int status) {