Metrics
提供了五个基本的度量类型:
Gauges(度量)
Counters(计数器)
Histograms(直方图数据)
Meters(TPS计算器)
Timers(计时器)
Metrics
中MetricRegistry
是中心容器,它是程序中所有度量的容器,所有新的度量工具都要注册到一个MetricRegistry
实例中才可以使用,尽量在一个应用中保持让这个MetricRegistry
实例保持单例。
MetricRegistry 容器
在代码中配置好这个MetricRegistry
容器:
@Bean public MetricRegistry metrics() { return new MetricRegistry(); }
Meters TPS计算器
TPS计算器
这个名称并不准确,Meters
工具会帮助我们统计系统中某一个事件的速率。比如每秒请求数(TPS),每秒查询数(QPS)等等。这个指标能反应系统当前的处理能力,帮助我们判断资源是否已经不足。Meters
本身是一个自增计数器。
通过MetricRegistry
可以获得一个Meter
:
@Beanpublic Meter requestMeter(MetricRegistry metrics) { return metrics.meter("request"); }
在请求中调用mark()
方法,来增加计数,我们可以在不同的请求中添加不同的Meter
,针对自己的系统完成定制的监控需求。
@RequestMapping("/hello")@ResponseBodypublic String helloWorld() { requestMeter.mark(); return "Hello World"; }
应用运行的过程中,在console中反馈的信息:
-- Meters ---------------------------------------------------------------------- request count = 21055 mean rate = 133.35 events/second 1-minute rate = 121.66 events/second 5-minute rate = 36.99 events/second 15-minute rate = 13.33 events/second
从以上信息中可以看出Meter
可以为我们提供平均速率,以及采样后的1分钟,5分钟,15分钟的速率。
Histogram 直方图数据
直方图是一种非常常见的统计图表,Metrics
通过这个Histogram
这个度量类型提供了一些方便实时绘制直方图的数据。
和之前的Meter
相同,我们可以通过MetricRegistry
来获得一个Histogram
。
@Beanpublic Histogram responseSizes(MetricRegistry metrics) { return metrics.histogram("response-sizes"); }
在应用中,需要统计的位置调用Histogram
的update()
方法。
responseSizes.update(new Random().nextInt(10));
比如我们需要统计某个方法的网络流量,通过Histogram
就非常的方便。
在console中Histogram
反馈的信息:
-- Histograms ------------------------------------------------------------------ response-sizes count = 21051 min = 0 max = 9 mean = 4.55 stddev = 2.88 median = 4.00 75% <= 7.00 95% <= 9.00 98% <= 9.00 99% <= 9.00 99.9% <= 9.00
Histogram
为我们提供了最大值,最小值和平均值等数据,利用这些数据,我们就可以开始绘制自定义的直方图了。
Counter 计数器
Counter
的本质就是一个AtomicLong
实例,可以增加或者减少值,可以用它来统计队列中Job的总数。
通过MetricRegistry
也可以获得一个Counter
实例。
@Beanpublic Counter pendingJobs(MetricRegistry metrics) { return metrics.counter("requestCount"); }
在需要统计数据的位置调用inc()
和dec()
方法。
// 增加计数pendingJobs.inc();// 减去计数pendingJobs.dec();
console的输出非常简单:
-- Counters --------------------------------------------------------------------requestCount count = 21051
只是输出了当前度量的值。
Timer 计时器
Timer
是一个Meter
和Histogram
的组合。这个度量单位可以比较方便地统计请求的速率和处理时间。对于接口中调用的延迟等信息的统计就比较方便了。如果发现一个方法的RPS(请求速率)
很低,而且平均的处理时间很长,那么这个方法八成出问题了。
同样,通过MetricRegistry
获取一个Timer
的实例:
@Beanpublic Timer responses(MetricRegistry metrics) { return metrics.timer("executeTime"); }
在需要统计信息的位置使用这样的代码:
final Timer.Context context = responses.time();try { // handle request} finally { context.stop(); }
console中就会实时返回这个Timer
的信息:
-- Timers ---------------------------------------------------------------------- executeTime count = 21061 mean rate = 133.39 calls/second 1-minute rate = 122.22 calls/second 5-minute rate = 37.11 calls/second 15-minute rate = 13.37 calls/second min = 0.00 milliseconds max = 0.01 milliseconds mean = 0.00 milliseconds stddev = 0.00 milliseconds median = 0.00 milliseconds 75% <= 0.00 milliseconds 95% <= 0.00 milliseconds 98% <= 0.00 milliseconds 99% <= 0.00 milliseconds 99.9% <= 0.01 milliseconds
Gauges 度量
除了Metrics
提供的几个度量类型,我们可以通过Gauges
完成自定义的度量类型。比方说很简单的,我们想看我们缓存里面的数据大小,就可以自己定义一个Gauges
。
metrics.register( MetricRegistry.name(ListManager.class, "cache", "size"), (Gauge) () -> cache.size() );
这样Metrics
就会一直监控Cache
的大小。
除此之外有时候,我们需要计算自己定义的一直单位,比如消息队列里面消费者(consumers)消费的速率和生产者(producers)的生产速率的比例,这也是一个度量。
public class CompareRatio extends RatioGauge { private final Meter consumers; private final Meter producers; public CacheHitRatio(Meter consumers, Meter producers) { this.consumers = consumers; this.producers = producers; } @Override protected Ratio getRatio() { return Ratio.of(consumers.getOneMinuteRate(), producers.getOneMinuteRate()); } }
把这个类也注册到Metrics
容器里面:
@Beanpublic CompareRatio cacheHitRatio(MetricRegistry metrics, Meter requestMeter, Meter producers) { CompareRatio compareRatio = new CompareRatio(consumers, producers); metrics.register("生产者消费者比率", compareRatio); return cacheHitRatio; }
Reporter 报表
Metrics
通过报表,将采集的数据展现到不同的位置,这里比如我们注册一个ConsoleReporter
到MetricRegistry
中,那么console中就会打印出对应的信息。
@Beanpublic ConsoleReporter consoleReporter(MetricRegistry metrics) { return ConsoleReporter.forRegistry(metrics) .convertRatesTo(TimeUnit.SECONDS) .convertDurationsTo(TimeUnit.MILLISECONDS) .build(); }
除此之外Metrics
还支持JMX
、HTTP
、Slf4j
等等,可以访问 http://metrics.dropwizard.io/3.1.0/manual/core/#reporters 来查看Metrics
提供的报表,如果还是不能满足自己的业务,也可以自己继承Metrics
提供的ScheduledReporter
类完成自定义的报表类。
import java.lang.management.ManagementFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Slf4jReporter;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.jvm.BufferPoolMetricSet;
import com.codahale.metrics.jvm.FileDescriptorRatioGauge;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurerAdapter;
import com.zaxxer.hikari.HikariDataSource;
import fr.ippon.spark.metrics.SparkReporter;
@Configuration
@EnableMetrics(proxyTargetClass = true)
public class MetricsConfiguration extends MetricsConfigurerAdapter {
private static final String PROP_METRIC_REG_JVM_MEMORY = "jvm.memory";
private static final String PROP_METRIC_REG_JVM_GARBAGE = "jvm.garbage";
private static final String PROP_METRIC_REG_JVM_THREADS = "jvm.threads";
private static final String PROP_METRIC_REG_JVM_FILES = "jvm.files";
private static final String PROP_METRIC_REG_JVM_BUFFERS = "jvm.buffers";
private final Logger log = LoggerFactory.getLogger(MetricsConfiguration.class);
private MetricRegistry metricRegistry = new MetricRegistry();
private HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();
@Autowired
private JHipsterProperties jHipsterProperties;
@Autowired(required = false)
private HikariDataSource hikariDataSource;
@Override
@Bean
public MetricRegistry getMetricRegistry() {
return metricRegistry;
}
@Override
@Bean
public HealthCheckRegistry getHealthCheckRegistry() {
return healthCheckRegistry;
}
@PostConstruct
public void init() {
log.debug("Registering JVM gauges");
metricRegistry.register(PROP_METRIC_REG_JVM_MEMORY, new MemoryUsageGaugeSet());
metricRegistry.register(PROP_METRIC_REG_JVM_GARBAGE, new GarbageCollectorMetricSet());
metricRegistry.register(PROP_METRIC_REG_JVM_THREADS, new ThreadStatesGaugeSet());
metricRegistry.register(PROP_METRIC_REG_JVM_FILES, new FileDescriptorRatioGauge());
metricRegistry.register(PROP_METRIC_REG_JVM_BUFFERS, new BufferPoolMetricSet(ManagementFactory.getPlatformMBeanServer()));
if (hikariDataSource != null) {
log.debug("Monitoring the datasource");
hikariDataSource.setMetricRegistry(metricRegistry);
}
if (jHipsterProperties.getMetrics().getJmx().isEnabled()) {
log.debug("Initializing Metrics JMX reporting");
JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).build();
jmxReporter.start();
}
if (jHipsterProperties.getMetrics().getLogs().isEnabled()) {
log.info("Initializing Metrics Log reporting");
final Slf4jReporter reporter = Slf4jReporter.forRegistry(metricRegistry)
.outputTo(LoggerFactory.getLogger("metrics"))
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
reporter.start(jHipsterProperties.getMetrics().getLogs().getReportFrequency(), TimeUnit.SECONDS);
}
}
@Configuration
@ConditionalOnClass(Graphite.class)
public static class GraphiteRegistry {
private final Logger log = LoggerFactory.getLogger(GraphiteRegistry.class);
@Autowired
private MetricRegistry metricRegistry;
@Autowired
private JHipsterProperties jHipsterProperties;
@PostConstruct
private void init() {
if (jHipsterProperties.getMetrics().getGraphite().isEnabled()) {
log.info("Initializing Metrics Graphite reporting");
String graphiteHost = jHipsterProperties.getMetrics().getGraphite().getHost();
Integer graphitePort = jHipsterProperties.getMetrics().getGraphite().getPort();
String graphitePrefix = jHipsterProperties.getMetrics().getGraphite().getPrefix();
Graphite graphite = new Graphite(new InetSocketAddress(graphiteHost, graphitePort));
GraphiteReporter graphiteReporter = GraphiteReporter.forRegistry(metricRegistry)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.prefixedWith(graphitePrefix)
.build(graphite);
graphiteReporter.start(1, TimeUnit.MINUTES);
}
}
}
@Configuration
@ConditionalOnClass(SparkReporter.class)
public static class SparkRegistry {
private final Logger log = LoggerFactory.getLogger(SparkRegistry.class);
@Autowired
private MetricRegistry metricRegistry;
@Autowired
private JHipsterProperties jHipsterProperties;
@PostConstruct
private void init() {
if (jHipsterProperties.getMetrics().getSpark().isEnabled()) {
log.info("Initializing Metrics Spark reporting");
String sparkHost = jHipsterProperties.getMetrics().getSpark().getHost();
Integer sparkPort = jHipsterProperties.getMetrics().getSpark().getPort();
SparkReporter sparkReporter = SparkReporter.forRegistry(metricRegistry)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build(sparkHost, sparkPort);
sparkReporter.start(1, TimeUnit.MINUTES);
}
}
}
}