Skip to content

Commit de2dfa5

Browse files
committed
metric cleanup
1 parent c565c1e commit de2dfa5

File tree

7 files changed

+80
-67
lines changed

7 files changed

+80
-67
lines changed

spring-security-rest-full/src/main/java/org/baeldung/web/controller/RootController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import javax.servlet.http.HttpServletRequest;
77
import javax.servlet.http.HttpServletResponse;
88

9-
import org.baeldung.web.metric.ActuatorMetricService2;
9+
import org.baeldung.web.metric.IActuatorMetricService;
1010
import org.baeldung.web.metric.IMetricService;
1111
import org.baeldung.web.util.LinkUtil;
1212
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,7 +25,7 @@ public class RootController {
2525
private IMetricService metricService;
2626

2727
@Autowired
28-
private ActuatorMetricService2 actMetricService;
28+
private IActuatorMetricService actMetricService;
2929

3030
public RootController() {
3131
super();

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

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,27 @@
66
import java.util.List;
77

88
import org.springframework.beans.factory.annotation.Autowired;
9-
import org.springframework.boot.actuate.metrics.CounterService;
9+
import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics;
1010
import org.springframework.boot.actuate.metrics.Metric;
11-
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
12-
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
1311
import org.springframework.scheduling.annotation.Scheduled;
1412
import org.springframework.stereotype.Service;
1513

1614
@Service
1715
public class ActuatorMetricService implements IActuatorMetricService {
1816

19-
private MetricRepository repo;
20-
2117
@Autowired
22-
private CounterService counter;
18+
private MetricReaderPublicMetrics publicMetrics;
2319

2420
private final List<ArrayList<Integer>> statusMetric;
2521
private final List<String> statusList;
2622
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
2723

2824
public ActuatorMetricService() {
2925
super();
30-
repo = new InMemoryMetricRepository();
3126
statusMetric = new ArrayList<ArrayList<Integer>>();
3227
statusList = new ArrayList<String>();
3328
}
3429

35-
// API
36-
37-
@Override
38-
public void increaseCount(final int status) {
39-
counter.increment("status." + status);
40-
if (!statusList.contains("counter.status." + status)) {
41-
statusList.add("counter.status." + status);
42-
}
43-
}
4430

4531
@Override
4632
public Object[][] getGraphData() {
@@ -49,24 +35,26 @@ public Object[][] getGraphData() {
4935
final int rowCount = statusMetric.size() + 1;
5036
final Object[][] result = new Object[rowCount][colCount];
5137
result[0][0] = "Time";
52-
5338
int j = 1;
39+
5440
for (final String status : statusList) {
5541
result[0][j] = status;
5642
j++;
5743
}
5844

5945
List<Integer> temp;
46+
List<Integer> last = new ArrayList<Integer>();
6047
for (int i = 1; i < rowCount; i++) {
6148
temp = statusMetric.get(i - 1);
6249
result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i))));
6350
for (j = 1; j <= temp.size(); j++) {
64-
result[i][j] = temp.get(j - 1);
51+
result[i][j] = temp.get(j - 1) - (last.size() >= j ? last.get(j - 1) : 0);
6552
}
6653
while (j < colCount) {
6754
result[i][j] = 0;
6855
j++;
6956
}
57+
last = temp;
7058
}
7159
return result;
7260
}
@@ -75,18 +63,39 @@ public Object[][] getGraphData() {
7563

7664
@Scheduled(fixedDelay = 60000)
7765
private void exportMetrics() {
78-
Metric<?> metric;
79-
final ArrayList<Integer> statusCount = new ArrayList<Integer>();
80-
for (final String status : statusList) {
81-
metric = repo.findOne(status);
82-
if (metric != null) {
83-
statusCount.add(metric.getValue().intValue());
84-
repo.reset(status);
85-
} else {
86-
statusCount.add(0);
87-
}
66+
final ArrayList<Integer> statusCount = initializeCounterList(statusList.size());
8867

68+
for (final Metric<?> counterMetric : publicMetrics.metrics()) {
69+
updateStatusCount(counterMetric, statusCount);
8970
}
71+
9072
statusMetric.add(statusCount);
9173
}
74+
75+
private ArrayList<Integer> initializeCounterList(final int size) {
76+
final ArrayList<Integer> counterList = new ArrayList<Integer>();
77+
for (int i = 0; i < size; i++) {
78+
counterList.add(0);
79+
}
80+
return counterList;
81+
}
82+
83+
private void updateStatusCount(final Metric<?> counterMetric, final ArrayList<Integer> statusCount) {
84+
String status = "";
85+
int index = -1;
86+
int oldCount = 0;
87+
88+
if (counterMetric.getName().contains("counter.status.")) {
89+
status = counterMetric.getName().substring(15, 18); // example 404, 200
90+
if (!statusList.contains(status)) {
91+
statusList.add(status);
92+
statusCount.add(0);
93+
}
94+
index = statusList.indexOf(status);
95+
oldCount = statusCount.get(index) == null ? 0 : statusCount.get(index);
96+
statusCount.set(index, counterMetric.getValue().intValue() + oldCount);
97+
}
98+
}
99+
100+
//
92101
}

spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService2.java renamed to spring-security-rest-full/src/main/java/org/baeldung/web/metric/CustomActuatorMetricService.java

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,67 @@
66
import java.util.List;
77

88
import org.springframework.beans.factory.annotation.Autowired;
9-
import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics;
9+
import org.springframework.boot.actuate.metrics.CounterService;
1010
import org.springframework.boot.actuate.metrics.Metric;
11+
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
12+
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
1113
import org.springframework.scheduling.annotation.Scheduled;
1214
import org.springframework.stereotype.Service;
1315

1416
@Service
15-
public class ActuatorMetricService2 {
17+
public class CustomActuatorMetricService implements ICustomActuatorMetricService {
18+
19+
private MetricRepository repo;
1620

1721
@Autowired
18-
private MetricReaderPublicMetrics publicMetrics;
22+
private CounterService counter;
1923

2024
private final List<ArrayList<Integer>> statusMetric;
2125
private final List<String> statusList;
2226
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
2327

24-
public ActuatorMetricService2() {
28+
public CustomActuatorMetricService() {
2529
super();
30+
repo = new InMemoryMetricRepository();
2631
statusMetric = new ArrayList<ArrayList<Integer>>();
2732
statusList = new ArrayList<String>();
2833
}
2934

35+
// API
3036

37+
@Override
38+
public void increaseCount(final int status) {
39+
counter.increment("status." + status);
40+
if (!statusList.contains("counter.status." + status)) {
41+
statusList.add("counter.status." + status);
42+
}
43+
}
44+
45+
@Override
3146
public Object[][] getGraphData() {
3247
final Date current = new Date();
3348
final int colCount = statusList.size() + 1;
3449
final int rowCount = statusMetric.size() + 1;
3550
final Object[][] result = new Object[rowCount][colCount];
3651
result[0][0] = "Time";
52+
3753
int j = 1;
3854
for (final String status : statusList) {
3955
result[0][j] = status;
4056
j++;
4157
}
4258

4359
List<Integer> temp;
44-
List<Integer> last = new ArrayList<Integer>();
4560
for (int i = 1; i < rowCount; i++) {
4661
temp = statusMetric.get(i - 1);
4762
result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i))));
4863
for (j = 1; j <= temp.size(); j++) {
49-
System.out.println(last);
50-
result[i][j] = temp.get(j - 1) - (last.size() >= j ? last.get(j - 1) : 0);
64+
result[i][j] = temp.get(j - 1);
5165
}
5266
while (j < colCount) {
5367
result[i][j] = 0;
5468
j++;
5569
}
56-
last = temp;
5770
}
5871
return result;
5972
}
@@ -62,32 +75,18 @@ public Object[][] getGraphData() {
6275

6376
@Scheduled(fixedDelay = 60000)
6477
private void exportMetrics() {
78+
Metric<?> metric;
6579
final ArrayList<Integer> statusCount = new ArrayList<Integer>();
66-
String status = "";
67-
int index = -1;
68-
int old = 0;
69-
for (int i = 0; i < statusList.size(); i++) {
70-
statusCount.add(0);
71-
}
72-
for (final Metric<?> metric : publicMetrics.metrics()) {
73-
if (metric.getName().contains("counter.status.")) {
74-
status = metric.getName().substring(15, 18);
75-
if (!statusList.contains(status)) {
76-
statusList.add(status);
77-
statusCount.add(0);
78-
}
79-
System.out.println(statusList + " == " + statusCount);
80-
index = statusList.indexOf(status);
81-
old = statusCount.get(index) == null ? 0 : statusCount.get(index);
82-
statusCount.set(index, metric.getValue().intValue() + old);
83-
// metric.set(0);
84-
// repo.reset(metric.getName());
80+
for (final String status : statusList) {
81+
metric = repo.findOne(status);
82+
if (metric != null) {
83+
statusCount.add(metric.getValue().intValue());
84+
repo.reset(status);
85+
} else {
86+
statusCount.add(0);
8587
}
88+
8689
}
8790
statusMetric.add(statusCount);
88-
89-
// for (final Metric<?> metric : publicMetrics.metrics()) {
90-
// System.out.println(metric.getName() + " = " + metric.getValue() + " = " + metric.getTimestamp());
91-
// }
9291
}
9392
}
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.baeldung.web.metric;
22

33
public interface IActuatorMetricService {
4-
5-
void increaseCount(final int status);
6-
74
Object[][] getGraphData();
85
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.baeldung.web.metric;
2+
3+
public interface ICustomActuatorMetricService {
4+
5+
void increaseCount(final int status);
6+
7+
Object[][] getGraphData();
8+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public class MetricFilter implements Filter {
2020
private IMetricService metricService;
2121

2222
@Autowired
23-
private IActuatorMetricService actMetricService;
23+
private ICustomActuatorMetricService actMetricService;
2424

2525
@Override
2626
public void init(final FilterConfig config) throws ServletException {
2727
if (metricService == null || actMetricService == null) {
2828
metricService = (IMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("metricService");
29-
actMetricService = (IActuatorMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("actuatorMetricService");
29+
actMetricService = (ICustomActuatorMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("actuatorMetricService");
3030
}
3131
}
3232

spring-security-rest-full/src/main/webapp/WEB-INF/view/graph.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
});
1212
1313
function drawChart() {
14-
$.get("<c:url value="/metric-graph"/>",
14+
$.get("<c:url value="/metric-graph-data"/>",
1515
function(mydata) {
1616
1717
var data = google.visualization.arrayToDataTable(mydata);

0 commit comments

Comments
 (0)