#243 metrics module, 在showcase中使用
This commit is contained in:
@@ -31,6 +31,10 @@
|
||||
<groupId>org.springside</groupId>
|
||||
<artifactId>springside-extension</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springside</groupId>
|
||||
<artifactId>springside-metrics</artifactId>
|
||||
</dependency>
|
||||
<!-- SPRINGSIDE end -->
|
||||
|
||||
<!-- SPRING basic begin -->
|
||||
@@ -209,16 +213,6 @@
|
||||
</dependency>
|
||||
|
||||
<!-- MONITOR begin -->
|
||||
<!-- codahale metrics -->
|
||||
<dependency>
|
||||
<groupId>com.codahale.metrics</groupId>
|
||||
<artifactId>metrics-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.codahale.metrics</groupId>
|
||||
<artifactId>metrics-graphite</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- javasimon -->
|
||||
<dependency>
|
||||
<groupId>org.javasimon</groupId>
|
||||
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package org.springside.examples.showcase.demos.metrics;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.springside.modules.metrics.MetricRegistry;
|
||||
import org.springside.modules.metrics.report.ConsoleReporter;
|
||||
import org.springside.modules.metrics.report.GraphiteReporter;
|
||||
import org.springside.modules.metrics.report.ReportScheduler;
|
||||
|
||||
public class MetricsManager {
|
||||
|
||||
private ReportScheduler scheduler;
|
||||
|
||||
@PostConstruct
|
||||
public void start() {
|
||||
GraphiteReporter graphiteReporter = new GraphiteReporter(new InetSocketAddress("localhost", 2003));
|
||||
scheduler = new ReportScheduler(MetricRegistry.INSTANCE, new ConsoleReporter(), graphiteReporter);
|
||||
scheduler.start(10, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void stop() {
|
||||
scheduler.stop();
|
||||
}
|
||||
}
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
package org.springside.examples.showcase.demos.metrics;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import com.codahale.metrics.JmxReporter;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.graphite.Graphite;
|
||||
import com.codahale.metrics.graphite.GraphiteReporter;
|
||||
|
||||
public class MetricsReporterManager {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(MetricsReporterManager.class);
|
||||
|
||||
@Autowired
|
||||
private MetricRegistry metricRegistry;
|
||||
|
||||
private GraphiteReporter graphiteReporter;
|
||||
private JmxReporter jmxReporter;
|
||||
|
||||
@PostConstruct
|
||||
public void start() {
|
||||
Graphite graphite = new Graphite(new InetSocketAddress("localhost", 2003));
|
||||
graphiteReporter = GraphiteReporter.forRegistry(metricRegistry).prefixedWith("metrics").build(graphite);
|
||||
graphiteReporter.start(30, TimeUnit.SECONDS);
|
||||
|
||||
jmxReporter = JmxReporter.forRegistry(metricRegistry).build();
|
||||
jmxReporter.start();
|
||||
|
||||
logger.info("Started Metrics Reporter");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void stop() {
|
||||
graphiteReporter.stop();
|
||||
jmxReporter.stop();
|
||||
|
||||
logger.info("Stopped Metrics Reporter");
|
||||
}
|
||||
}
|
||||
+7
-10
@@ -14,9 +14,9 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springside.examples.showcase.entity.User;
|
||||
import org.springside.examples.showcase.service.AccountEffectiveService;
|
||||
import org.springside.modules.mapper.BeanMapper;
|
||||
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.Timer;
|
||||
import org.springside.modules.metrics.Execution;
|
||||
import org.springside.modules.metrics.Execution.ExecutionTimer;
|
||||
import org.springside.modules.metrics.MetricRegistry;
|
||||
|
||||
/**
|
||||
* Shiro的配置文件中对/api/secure/**进行拦截,要求authBasic认证.
|
||||
@@ -31,14 +31,11 @@ public class UserRestController {
|
||||
@Autowired
|
||||
private AccountEffectiveService accountService;
|
||||
|
||||
@Autowired
|
||||
private MetricRegistry metricRegistry;
|
||||
|
||||
private Timer metrics;
|
||||
private Execution executionMetrics;
|
||||
|
||||
@PostConstruct
|
||||
public void register() {
|
||||
metrics = metricRegistry.timer("Rest_GetUser");
|
||||
executionMetrics = MetricRegistry.INSTANCE.execution("Rest_GetUser");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -50,7 +47,7 @@ public class UserRestController {
|
||||
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public UserDTO getUser(@PathVariable("id") Long id) {
|
||||
final Timer.Context metricsContext = metrics.time();
|
||||
final ExecutionTimer exectuionTimer = executionMetrics.start();
|
||||
try {
|
||||
User user = accountService.getUser(id);
|
||||
|
||||
@@ -65,7 +62,7 @@ public class UserRestController {
|
||||
dto.setTeamId(user.getTeam().getId());
|
||||
return dto;
|
||||
} finally {
|
||||
metricsContext.stop();
|
||||
exectuionTimer.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,6 @@
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
|
||||
|
||||
<description>Yammer Coda Hale's Metrics监控配置</description>
|
||||
|
||||
<bean id="metrics" class="com.codahale.metrics.MetricRegistry "/>
|
||||
|
||||
<bean id="graphiteReporterManager" class="org.springside.examples.showcase.demos.metrics.MetricsReporterManager"/>
|
||||
|
||||
<description>Metrics监控配置</description>
|
||||
<bean id="metricsManager" class="org.springside.examples.showcase.demos.metrics.MetricsManager"/>
|
||||
</beans>
|
||||
@@ -0,0 +1,6 @@
|
||||
@echo off
|
||||
echo [INFO] Install jar to local repository.
|
||||
|
||||
cd %~dp0
|
||||
call mvn clean install -Dmaven.test.skip=true
|
||||
pause
|
||||
+4
@@ -79,6 +79,8 @@ public class GraphiteReporter implements Reporter {
|
||||
|
||||
private void reportCounter(String name, CounterMetric counter, long timestamp) throws IOException {
|
||||
send(MetricRegistry.name(prefix, name, "count"), format(counter.count), timestamp);
|
||||
send(MetricRegistry.name(prefix, name, "lastRate"), format(counter.lastRate), timestamp);
|
||||
send(MetricRegistry.name(prefix, name, "meanRate"), format(counter.meanRate), timestamp);
|
||||
}
|
||||
|
||||
private void reportHistogram(String name, HistogramMetric histogram, long timestamp) throws IOException {
|
||||
@@ -93,6 +95,8 @@ public class GraphiteReporter implements Reporter {
|
||||
|
||||
private void reportExecution(String name, ExecutionMetric execution, long timestamp) throws IOException {
|
||||
send(MetricRegistry.name(prefix, name, "count"), format(execution.counter.count), timestamp);
|
||||
send(MetricRegistry.name(prefix, name, "lastRate"), format(execution.counter.lastRate), timestamp);
|
||||
send(MetricRegistry.name(prefix, name, "meanRate"), format(execution.counter.meanRate), timestamp);
|
||||
|
||||
send(MetricRegistry.name(prefix, name, "min"), format(execution.histogram.min), timestamp);
|
||||
send(MetricRegistry.name(prefix, name, "max"), format(execution.histogram.max), timestamp);
|
||||
|
||||
+3
-3
@@ -75,16 +75,16 @@ public class ReportScheduler {
|
||||
}
|
||||
}
|
||||
}, period, period, unit);
|
||||
logger.info("metric reporter started.");
|
||||
logger.info("metric reporters started.");
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
executor.shutdownNow();
|
||||
try {
|
||||
if (executor.awaitTermination(1, TimeUnit.SECONDS)) {
|
||||
logger.info("metric reporter stopped.");
|
||||
logger.info("metric reporters stopped.");
|
||||
} else {
|
||||
logger.info("metric reporter can stopped in 1 seconds, force stopped");
|
||||
logger.info("metric reporters can't stopped in 1 seconds, force stopped");
|
||||
}
|
||||
} catch (InterruptedException ignored) {
|
||||
// do nothing
|
||||
|
||||
+5
-13
@@ -27,7 +27,6 @@
|
||||
<hibernate-validator.version>4.3.1.Final</hibernate-validator.version>
|
||||
<jolokia.version>1.1.5</jolokia.version>
|
||||
<hystrix.version>1.3.8</hystrix.version>
|
||||
<metrics.version>3.0.1</metrics.version>
|
||||
<javasimon.version>3.4.0</javasimon.version>
|
||||
<jackson.version>2.3.0</jackson.version>
|
||||
<slf4j.version>1.7.5</slf4j.version>
|
||||
@@ -93,6 +92,11 @@
|
||||
<artifactId>springside-extension</artifactId>
|
||||
<version>${springside.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springside</groupId>
|
||||
<artifactId>springside-metrics</artifactId>
|
||||
<version>${springside.version}</version>
|
||||
</dependency>
|
||||
<!-- SPRINGSIDE MODULES end -->
|
||||
|
||||
<!-- SPRING basic begin(一般不需要显式定义) -->
|
||||
@@ -415,18 +419,6 @@
|
||||
<!-- hystrix end -->
|
||||
|
||||
<!-- Monitor begin -->
|
||||
<!-- codahale metrics -->
|
||||
<dependency>
|
||||
<groupId>com.codahale.metrics</groupId>
|
||||
<artifactId>metrics-core</artifactId>
|
||||
<version>${metrics.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.codahale.metrics</groupId>
|
||||
<artifactId>metrics-graphite</artifactId>
|
||||
<version>${metrics.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- javasimon -->
|
||||
<dependency>
|
||||
<groupId>org.javasimon</groupId>
|
||||
|
||||
Reference in New Issue
Block a user