Skip to content

Commit d696c4d

Browse files
committed
Add spring boot metrics
1 parent 90b5676 commit d696c4d

File tree

8 files changed

+197
-5
lines changed

8 files changed

+197
-5
lines changed

spring-security-mvc-ldap/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
<groupId>org.springframework.boot</groupId>
2626
<artifactId>spring-boot-starter-thymeleaf</artifactId>
2727
</dependency>
28-
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-actuator</artifactId>
31+
</dependency>
32+
2933
<!-- LDAP Dependencies -->
3034
<dependency>
3135
<groupId>org.springframework.security</groupId>

spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
55
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.scheduling.annotation.EnableScheduling;
67
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
78
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
89

@@ -11,6 +12,7 @@
1112
* class to run up a Jetty Server (on http://localhost:8080)
1213
*
1314
*/
15+
@EnableScheduling
1416
@EnableAutoConfiguration
1517
@ComponentScan("org.baeldung")
1618
public class SampleLDAPApplication extends WebMvcConfigurerAdapter {
@@ -22,6 +24,7 @@ public static void main(String[] args) {
2224
@Override
2325
public void addViewControllers(ViewControllerRegistry registry) {
2426
registry.addViewController("/login").setViewName("login");
27+
registry.addViewController("/graph").setViewName("graph");
2528
}
2629

2730
}

spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@
77
import java.util.Map;
88
import java.util.Set;
99

10+
import org.baeldung.metric.MetricService;
11+
import org.springframework.beans.factory.annotation.Autowired;
1012
import org.springframework.security.core.Authentication;
1113
import org.springframework.security.core.GrantedAuthority;
1214
import org.springframework.security.core.userdetails.UserDetails;
1315
import org.springframework.stereotype.Controller;
1416
import org.springframework.web.bind.annotation.RequestMapping;
17+
import org.springframework.web.bind.annotation.RequestMethod;
18+
import org.springframework.web.bind.annotation.ResponseBody;
1519

1620
/**
1721
* Spring Controller Definitions.
1822
*/
1923
@Controller
2024
public class MyController {
2125

26+
@Autowired
27+
private MetricService metricService;
28+
2229
@RequestMapping("/")
2330
public String init(Map<String, Object> model, Principal principal) {
2431
model.put("title", "PUBLIC AREA");
@@ -37,6 +44,12 @@ public String secure(Map<String, Object> model, Principal principal) {
3744
return "home";
3845
}
3946

47+
@RequestMapping(value = "/metric-graph-data", method = RequestMethod.GET)
48+
@ResponseBody
49+
public Object[][] getMetricData() {
50+
return metricService.getGraphData();
51+
}
52+
4053
private String getUserName(Principal principal) {
4154
if (principal == null) {
4255
return "anonymous";
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.baeldung.metric;
2+
3+
import javax.servlet.Filter;
4+
import javax.servlet.FilterChain;
5+
import javax.servlet.FilterConfig;
6+
import javax.servlet.ServletException;
7+
import javax.servlet.ServletRequest;
8+
import javax.servlet.ServletResponse;
9+
import javax.servlet.http.HttpServletResponse;
10+
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.stereotype.Component;
13+
14+
@Component
15+
public class MetricFilter implements Filter {
16+
17+
@Autowired
18+
private MetricService metricService;
19+
20+
@Override
21+
public void init(final FilterConfig config) throws ServletException {
22+
}
23+
24+
@Override
25+
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws java.io.IOException, ServletException {
26+
chain.doFilter(request, response);
27+
28+
final int status = ((HttpServletResponse) response).getStatus();
29+
metricService.increaseCount(status);
30+
}
31+
32+
@Override
33+
public void destroy() {
34+
35+
}
36+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.baeldung.metric;
2+
3+
import java.text.SimpleDateFormat;
4+
import java.util.ArrayList;
5+
import java.util.Date;
6+
import java.util.List;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.actuate.metrics.CounterService;
10+
import org.springframework.boot.actuate.metrics.Metric;
11+
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
12+
import org.springframework.scheduling.annotation.Scheduled;
13+
import org.springframework.stereotype.Service;
14+
15+
@Service
16+
public class MetricService {
17+
18+
@Autowired
19+
private MetricRepository repo;
20+
21+
@Autowired
22+
private CounterService counter;
23+
24+
private ArrayList<ArrayList<Integer>> statusMetric;
25+
private List<String> statusList;
26+
27+
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
28+
29+
public MetricService() {
30+
super();
31+
statusMetric = new ArrayList<ArrayList<Integer>>();
32+
statusList = new ArrayList<String>();
33+
}
34+
35+
public void increaseCount(final int status) {
36+
counter.increment("status." + status);
37+
if (!statusList.contains("counter.status." + status)) {
38+
statusList.add("counter.status." + status);
39+
}
40+
}
41+
42+
@Scheduled(fixedDelay = 60000)
43+
private void exportMetrics() {
44+
Metric<?> metric;
45+
ArrayList<Integer> statusCount = new ArrayList<Integer>();
46+
for (String status : statusList) {
47+
metric = repo.findOne(status);
48+
if (metric != null) {
49+
statusCount.add(metric.getValue().intValue());
50+
repo.reset(status);
51+
} else {
52+
statusCount.add(0);
53+
}
54+
55+
}
56+
statusMetric.add(statusCount);
57+
}
58+
59+
public Object[][] getGraphData() {
60+
Date current = new Date();
61+
int colCount = statusList.size() + 1;
62+
int rowCount = statusMetric.size() + 1;
63+
64+
Object[][] result = new Object[rowCount][colCount];
65+
result[0][0] = "Time";
66+
67+
int j = 1;
68+
for (final String status : statusList) {
69+
result[0][j] = status;
70+
j++;
71+
}
72+
73+
ArrayList<Integer> temp;
74+
for (int i = 1; i < rowCount; i++) {
75+
temp = statusMetric.get(i - 1);
76+
result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i))));
77+
for (j = 1; j <= temp.size(); j++) {
78+
result[i][j] = temp.get(j - 1);
79+
}
80+
while (j < colCount) {
81+
result[i][j] = 0;
82+
j++;
83+
}
84+
}
85+
86+
return result;
87+
}
88+
89+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<html>
2+
<head>
3+
<title>Metric Graph</title>
4+
<script
5+
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
6+
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
7+
<script type="text/javascript">
8+
google.load("visualization", "1", {
9+
packages : [ "corechart" ]
10+
});
11+
12+
function drawChart() {
13+
$.get("http://localhost:8080/metric-graph-data",
14+
function(mydata) {
15+
16+
var data = google.visualization.arrayToDataTable(mydata);
17+
var options = {
18+
title : 'Website Metric',
19+
hAxis : {
20+
title : 'Time',
21+
titleTextStyle : {
22+
color : '#333'
23+
}
24+
},
25+
vAxis : {
26+
minValue : 0
27+
}
28+
};
29+
30+
var chart = new google.visualization.AreaChart(document
31+
.getElementById('chart_div'));
32+
chart.draw(data, options);
33+
34+
});
35+
36+
}
37+
</script>
38+
</head>
39+
<body onload="drawChart()">
40+
<div id="chart_div" style="width: 900px; height: 500px;"></div>
41+
</body>
42+
</html>

spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import javax.servlet.http.HttpServletRequest;
1010
import javax.servlet.http.HttpServletResponse;
1111

12+
import org.springframework.web.context.support.WebApplicationContextUtils;
13+
1214
public class MetricFilter implements Filter {
1315

1416
private MetricService metricService;
1517

1618
@Override
1719
public void init(final FilterConfig config) throws ServletException {
18-
metricService = new MetricService();
20+
metricService = (MetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("metricService");
1921
}
2022

2123
@Override

spring-security-rest-full/src/main/java/org/baeldung/web/metric/MetricService.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
@Service
1212
public class MetricService {
1313

14-
private static HashMap<String, HashMap<Integer, Integer>> metricMap = new HashMap<String, HashMap<Integer, Integer>>();
15-
private static HashMap<Integer, Integer> statusMetric = new HashMap<Integer, Integer>();
16-
private static HashMap<String, HashMap<Integer, Integer>> timeMap = new HashMap<String, HashMap<Integer, Integer>>();
14+
private HashMap<String, HashMap<Integer, Integer>> metricMap;
15+
private HashMap<Integer, Integer> statusMetric;
16+
private HashMap<String, HashMap<Integer, Integer>> timeMap;
1717
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
1818

1919
public MetricService() {
2020
super();
21+
metricMap = new HashMap<String, HashMap<Integer, Integer>>();
22+
statusMetric = new HashMap<Integer, Integer>();
23+
timeMap = new HashMap<String, HashMap<Integer, Integer>>();
2124
}
2225

2326
public void increaseCount(final String request, final int status) {

0 commit comments

Comments
 (0)