metric cleanup

This commit is contained in:
DOHA 2015-12-09 19:48:34 +02:00
parent c565c1e181
commit de2dfa5178
7 changed files with 80 additions and 67 deletions

View File

@ -6,7 +6,7 @@ import java.util.Map;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.baeldung.web.metric.ActuatorMetricService2; import org.baeldung.web.metric.IActuatorMetricService;
import org.baeldung.web.metric.IMetricService; import org.baeldung.web.metric.IMetricService;
import org.baeldung.web.util.LinkUtil; import org.baeldung.web.util.LinkUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -25,7 +25,7 @@ public class RootController {
private IMetricService metricService; private IMetricService metricService;
@Autowired @Autowired
private ActuatorMetricService2 actMetricService; private IActuatorMetricService actMetricService;
public RootController() { public RootController() {
super(); super();

View File

@ -6,20 +6,16 @@ import java.util.Date;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.CounterService; import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class ActuatorMetricService implements IActuatorMetricService { public class ActuatorMetricService implements IActuatorMetricService {
private MetricRepository repo;
@Autowired @Autowired
private CounterService counter; private MetricReaderPublicMetrics publicMetrics;
private final List<ArrayList<Integer>> statusMetric; private final List<ArrayList<Integer>> statusMetric;
private final List<String> statusList; private final List<String> statusList;
@ -27,20 +23,10 @@ public class ActuatorMetricService implements IActuatorMetricService {
public ActuatorMetricService() { public ActuatorMetricService() {
super(); super();
repo = new InMemoryMetricRepository();
statusMetric = new ArrayList<ArrayList<Integer>>(); statusMetric = new ArrayList<ArrayList<Integer>>();
statusList = new ArrayList<String>(); statusList = new ArrayList<String>();
} }
// API
@Override
public void increaseCount(final int status) {
counter.increment("status." + status);
if (!statusList.contains("counter.status." + status)) {
statusList.add("counter.status." + status);
}
}
@Override @Override
public Object[][] getGraphData() { public Object[][] getGraphData() {
@ -49,24 +35,26 @@ public class ActuatorMetricService implements IActuatorMetricService {
final int rowCount = statusMetric.size() + 1; final int rowCount = statusMetric.size() + 1;
final Object[][] result = new Object[rowCount][colCount]; final Object[][] result = new Object[rowCount][colCount];
result[0][0] = "Time"; result[0][0] = "Time";
int j = 1; int j = 1;
for (final String status : statusList) { for (final String status : statusList) {
result[0][j] = status; result[0][j] = status;
j++; j++;
} }
List<Integer> temp; List<Integer> temp;
List<Integer> last = new ArrayList<Integer>();
for (int i = 1; i < rowCount; i++) { for (int i = 1; i < rowCount; i++) {
temp = statusMetric.get(i - 1); temp = statusMetric.get(i - 1);
result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i)))); result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i))));
for (j = 1; j <= temp.size(); j++) { for (j = 1; j <= temp.size(); j++) {
result[i][j] = temp.get(j - 1); result[i][j] = temp.get(j - 1) - (last.size() >= j ? last.get(j - 1) : 0);
} }
while (j < colCount) { while (j < colCount) {
result[i][j] = 0; result[i][j] = 0;
j++; j++;
} }
last = temp;
} }
return result; return result;
} }
@ -75,18 +63,39 @@ public class ActuatorMetricService implements IActuatorMetricService {
@Scheduled(fixedDelay = 60000) @Scheduled(fixedDelay = 60000)
private void exportMetrics() { private void exportMetrics() {
Metric<?> metric; final ArrayList<Integer> statusCount = initializeCounterList(statusList.size());
final ArrayList<Integer> statusCount = new ArrayList<Integer>();
for (final String status : statusList) {
metric = repo.findOne(status);
if (metric != null) {
statusCount.add(metric.getValue().intValue());
repo.reset(status);
} else {
statusCount.add(0);
}
for (final Metric<?> counterMetric : publicMetrics.metrics()) {
updateStatusCount(counterMetric, statusCount);
} }
statusMetric.add(statusCount); statusMetric.add(statusCount);
} }
private ArrayList<Integer> initializeCounterList(final int size) {
final ArrayList<Integer> counterList = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
counterList.add(0);
}
return counterList;
}
private void updateStatusCount(final Metric<?> counterMetric, final ArrayList<Integer> statusCount) {
String status = "";
int index = -1;
int oldCount = 0;
if (counterMetric.getName().contains("counter.status.")) {
status = counterMetric.getName().substring(15, 18); // example 404, 200
if (!statusList.contains(status)) {
statusList.add(status);
statusCount.add(0);
}
index = statusList.indexOf(status);
oldCount = statusCount.get(index) == null ? 0 : statusCount.get(index);
statusCount.set(index, counterMetric.getValue().intValue() + oldCount);
}
}
//
} }

View File

@ -6,34 +6,50 @@ import java.util.Date;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics; import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.Metric; import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository;
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
public class ActuatorMetricService2 { public class CustomActuatorMetricService implements ICustomActuatorMetricService {
private MetricRepository repo;
@Autowired @Autowired
private MetricReaderPublicMetrics publicMetrics; private CounterService counter;
private final List<ArrayList<Integer>> statusMetric; private final List<ArrayList<Integer>> statusMetric;
private final List<String> statusList; private final List<String> statusList;
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public ActuatorMetricService2() { public CustomActuatorMetricService() {
super(); super();
repo = new InMemoryMetricRepository();
statusMetric = new ArrayList<ArrayList<Integer>>(); statusMetric = new ArrayList<ArrayList<Integer>>();
statusList = new ArrayList<String>(); statusList = new ArrayList<String>();
} }
// API
@Override
public void increaseCount(final int status) {
counter.increment("status." + status);
if (!statusList.contains("counter.status." + status)) {
statusList.add("counter.status." + status);
}
}
@Override
public Object[][] getGraphData() { public Object[][] getGraphData() {
final Date current = new Date(); final Date current = new Date();
final int colCount = statusList.size() + 1; final int colCount = statusList.size() + 1;
final int rowCount = statusMetric.size() + 1; final int rowCount = statusMetric.size() + 1;
final Object[][] result = new Object[rowCount][colCount]; final Object[][] result = new Object[rowCount][colCount];
result[0][0] = "Time"; result[0][0] = "Time";
int j = 1; int j = 1;
for (final String status : statusList) { for (final String status : statusList) {
result[0][j] = status; result[0][j] = status;
@ -41,19 +57,16 @@ public class ActuatorMetricService2 {
} }
List<Integer> temp; List<Integer> temp;
List<Integer> last = new ArrayList<Integer>();
for (int i = 1; i < rowCount; i++) { for (int i = 1; i < rowCount; i++) {
temp = statusMetric.get(i - 1); temp = statusMetric.get(i - 1);
result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i)))); result[i][0] = dateFormat.format(new Date(current.getTime() - (60000 * (rowCount - i))));
for (j = 1; j <= temp.size(); j++) { for (j = 1; j <= temp.size(); j++) {
System.out.println(last); result[i][j] = temp.get(j - 1);
result[i][j] = temp.get(j - 1) - (last.size() >= j ? last.get(j - 1) : 0);
} }
while (j < colCount) { while (j < colCount) {
result[i][j] = 0; result[i][j] = 0;
j++; j++;
} }
last = temp;
} }
return result; return result;
} }
@ -62,32 +75,18 @@ public class ActuatorMetricService2 {
@Scheduled(fixedDelay = 60000) @Scheduled(fixedDelay = 60000)
private void exportMetrics() { private void exportMetrics() {
Metric<?> metric;
final ArrayList<Integer> statusCount = new ArrayList<Integer>(); final ArrayList<Integer> statusCount = new ArrayList<Integer>();
String status = ""; for (final String status : statusList) {
int index = -1; metric = repo.findOne(status);
int old = 0; if (metric != null) {
for (int i = 0; i < statusList.size(); i++) { statusCount.add(metric.getValue().intValue());
statusCount.add(0); repo.reset(status);
} } else {
for (final Metric<?> metric : publicMetrics.metrics()) { statusCount.add(0);
if (metric.getName().contains("counter.status.")) {
status = metric.getName().substring(15, 18);
if (!statusList.contains(status)) {
statusList.add(status);
statusCount.add(0);
}
System.out.println(statusList + " == " + statusCount);
index = statusList.indexOf(status);
old = statusCount.get(index) == null ? 0 : statusCount.get(index);
statusCount.set(index, metric.getValue().intValue() + old);
// metric.set(0);
// repo.reset(metric.getName());
} }
} }
statusMetric.add(statusCount); statusMetric.add(statusCount);
// for (final Metric<?> metric : publicMetrics.metrics()) {
// System.out.println(metric.getName() + " = " + metric.getValue() + " = " + metric.getTimestamp());
// }
} }
} }

View File

@ -1,8 +1,5 @@
package org.baeldung.web.metric; package org.baeldung.web.metric;
public interface IActuatorMetricService { public interface IActuatorMetricService {
void increaseCount(final int status);
Object[][] getGraphData(); Object[][] getGraphData();
} }

View File

@ -0,0 +1,8 @@
package org.baeldung.web.metric;
public interface ICustomActuatorMetricService {
void increaseCount(final int status);
Object[][] getGraphData();
}

View File

@ -20,13 +20,13 @@ public class MetricFilter implements Filter {
private IMetricService metricService; private IMetricService metricService;
@Autowired @Autowired
private IActuatorMetricService actMetricService; private ICustomActuatorMetricService actMetricService;
@Override @Override
public void init(final FilterConfig config) throws ServletException { public void init(final FilterConfig config) throws ServletException {
if (metricService == null || actMetricService == null) { if (metricService == null || actMetricService == null) {
metricService = (IMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("metricService"); metricService = (IMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("metricService");
actMetricService = (IActuatorMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("actuatorMetricService"); actMetricService = (ICustomActuatorMetricService) WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext()).getBean("actuatorMetricService");
} }
} }

View File

@ -11,7 +11,7 @@
}); });
function drawChart() { function drawChart() {
$.get("<c:url value="/metric-graph"/>", $.get("<c:url value="/metric-graph-data"/>",
function(mydata) { function(mydata) {
var data = google.visualization.arrayToDataTable(mydata); var data = google.visualization.arrayToDataTable(mydata);