Merge pull request #6834 from amit2103/BAEL-13458
[BAEL-13458] - Update Spring REST Metrics article, spring boot 2, mic…
This commit is contained in:
commit
d047b2e676
|
@ -8,10 +8,10 @@
|
|||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-1</relativePath>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -129,6 +129,7 @@
|
|||
<dependency>
|
||||
<groupId>org.javassist</groupId>
|
||||
<artifactId>javassist</artifactId>
|
||||
<version>${javassist.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
|
@ -287,6 +288,7 @@
|
|||
<!-- util -->
|
||||
<guava.version>19.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<javassist.version>3.25.0-GA</javassist.version>
|
||||
|
||||
<!-- Maven plugins -->
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
|
|
|
@ -17,7 +17,7 @@ public abstract class AbstractService<T extends Serializable> implements IOperat
|
|||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public T findOne(final long id) {
|
||||
return getDao().findOne(id);
|
||||
return getDao().findById(id).orElse(null);
|
||||
}
|
||||
|
||||
// read - all
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.springframework.boot.SpringApplication;
|
|||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.support.SpringBootServletInitializer;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.web.context.request.RequestContextListener;
|
||||
|
|
|
@ -6,13 +6,13 @@ import org.springframework.context.annotation.Configuration;
|
|||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.web")
|
||||
@EnableWebMvc
|
||||
public class WebConfig extends WebMvcConfigurerAdapter {
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
public WebConfig() {
|
||||
super();
|
||||
|
@ -29,7 +29,6 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
|||
// API
|
||||
@Override
|
||||
public void addViewControllers(final ViewControllerRegistry registry) {
|
||||
super.addViewControllers(registry);
|
||||
registry.addViewController("/graph.html");
|
||||
registry.addViewController("/homepage.html");
|
||||
}
|
||||
|
|
|
@ -6,16 +6,18 @@ import java.util.Date;
|
|||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.endpoint.MetricReaderPublicMetrics;
|
||||
import org.springframework.boot.actuate.metrics.Metric;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import io.micrometer.core.instrument.Counter;
|
||||
import io.micrometer.core.instrument.Meter;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
|
||||
@Service
|
||||
public class ActuatorMetricService implements IActuatorMetricService {
|
||||
|
||||
@Autowired
|
||||
private MetricReaderPublicMetrics publicMetrics;
|
||||
private MeterRegistry publicMetrics;
|
||||
|
||||
private final List<ArrayList<Integer>> statusMetricsByMinute;
|
||||
private final List<String> statusList;
|
||||
|
@ -68,7 +70,7 @@ public class ActuatorMetricService implements IActuatorMetricService {
|
|||
private void exportMetrics() {
|
||||
final ArrayList<Integer> lastMinuteStatuses = initializeStatuses(statusList.size());
|
||||
|
||||
for (final Metric<?> counterMetric : publicMetrics.metrics()) {
|
||||
for (final Meter counterMetric : publicMetrics.getMeters()) {
|
||||
updateMetrics(counterMetric, lastMinuteStatuses);
|
||||
}
|
||||
|
||||
|
@ -83,17 +85,17 @@ public class ActuatorMetricService implements IActuatorMetricService {
|
|||
return counterList;
|
||||
}
|
||||
|
||||
private void updateMetrics(final Metric<?> counterMetric, final ArrayList<Integer> statusCount) {
|
||||
private void updateMetrics(final Meter 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 (counterMetric.getId().getName().contains("counter.status.")) {
|
||||
status = counterMetric.getId().getName().substring(15, 18); // example 404, 200
|
||||
appendStatusIfNotExist(status, statusCount);
|
||||
index = statusList.indexOf(status);
|
||||
oldCount = statusCount.get(index) == null ? 0 : statusCount.get(index);
|
||||
statusCount.set(index, counterMetric.getValue().intValue() + oldCount);
|
||||
statusCount.set(index, (int)((Counter) counterMetric).count() + oldCount);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,20 +6,18 @@ 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.InMemoryMetricRepository;
|
||||
import org.springframework.boot.actuate.metrics.repository.MetricRepository;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import io.micrometer.core.instrument.Counter;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.search.Search;
|
||||
|
||||
@Service
|
||||
public class CustomActuatorMetricService implements ICustomActuatorMetricService {
|
||||
|
||||
private MetricRepository repo;
|
||||
|
||||
@Autowired
|
||||
private CounterService counter;
|
||||
private MeterRegistry registry;
|
||||
|
||||
private final List<ArrayList<Integer>> statusMetricsByMinute;
|
||||
private final List<String> statusList;
|
||||
|
@ -27,7 +25,6 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService
|
|||
|
||||
public CustomActuatorMetricService() {
|
||||
super();
|
||||
repo = new InMemoryMetricRepository();
|
||||
statusMetricsByMinute = new ArrayList<ArrayList<Integer>>();
|
||||
statusList = new ArrayList<String>();
|
||||
}
|
||||
|
@ -36,9 +33,10 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService
|
|||
|
||||
@Override
|
||||
public void increaseCount(final int status) {
|
||||
counter.increment("status." + status);
|
||||
if (!statusList.contains("counter.status." + status)) {
|
||||
statusList.add("counter.status." + status);
|
||||
String counterName = "counter.status." + status;
|
||||
registry.counter(counterName).increment(1);
|
||||
if (!statusList.contains(counterName)) {
|
||||
statusList.add(counterName);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -78,17 +76,16 @@ public class CustomActuatorMetricService implements ICustomActuatorMetricService
|
|||
|
||||
@Scheduled(fixedDelay = 60000)
|
||||
private void exportMetrics() {
|
||||
Metric<?> metric;
|
||||
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);
|
||||
Search search = registry.find(status);
|
||||
if (search != null) {
|
||||
Counter counter = search.counter();
|
||||
statusCount.add(counter != null ? ((int) counter.count()) : 0);
|
||||
registry.remove(counter);
|
||||
} else {
|
||||
statusCount.add(0);
|
||||
}
|
||||
|
||||
}
|
||||
statusMetricsByMinute.add(statusCount);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
server.port=8082
|
||||
server.context-path=/spring-rest-full
|
||||
server.servlet.context-path=/spring-rest-full
|
||||
endpoints.metrics.enabled=true
|
Loading…
Reference in New Issue