Metrics是一个Java库,可以对系统进行监控,统计一些系统的性能指标。
比如一个系统后台服务,我们可能需要了解一下下面的一些情况:
1、每秒钟的请求数是多少(TPS)?
2、平均每个请求处理的时间?
3、请求处理的最长耗时?
4、等待处理的请求队列长度?
5、又或者一个缓存服务:缓存的命中率?平均查询缓存的时间?
基本上每一个服务、应用都需要做一个监控系统,这需要尽量以少量的代码,实现统计某类数据的功能。
Metric Registries
MetricRegistry类是Metrics的核心,它是存放应用中所有metrics的容器,也是我们使用 Metrics 库的起点。
MetricRegistry registry = new MetricRegistry();
Metrics 数据展示
Metrics 提供了 Report 接口,用于展示 metrics 获取到的统计数据。metrics-core
中主要实现了四种 reporter: JMX ,console, SLF4J, 和 CSV。 在本文的例子中,我们使用 ConsoleReporter 。
Metrics的五种类型
Gauges
最简单的度量指标,只有一个简单的返回值,例如,我们想衡量一个待处理队列中任务的个数,代码如下:
package com.zyh.maven.metricsdemo;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
public class GaugeTest {
public static Queue q = new LinkedList();
public static void main(String[] args) throws InterruptedException {
MetricRegistry metricRegistry = new MetricRegistry();
ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build();
reporter.start(1, TimeUnit.SECONDS);
metricRegistry.register(MetricRegistry.name(GaugeTest.class, "queue", "size"),
new Gauge(){
@Override
public Integer getValue() {
return q.size();
}
});
while (true)
{
Thread.sleep(1000);
q.add("张永辉");
}
}
}
运行结果
18-2-5 14:36:28 ================================================================
-- Gauges ----------------------------------------------------------------------
com.zyh.maven.metricsdemo.GaugeTest.queue.size
value = 1
18-2-5 14:36:29 ================================================================
-- Gauges ----------------------------------------------------------------------
com.zyh.maven.metricsdemo.GaugeTest.queue.size
value = 1
18-2-5 14:36:30 ================================================================
-- Gauges ----------------------------------------------------------------------
com.zyh.maven.metricsdemo.GaugeTest.queue.size
value = 2
18-2-5 14:36:31 ================================================================
-- Gauges ----------------------------------------------------------------------
com.zyh.maven.metricsdemo.GaugeTest.queue.size
value = 3
Counters
Counter 就是计数器,Counter 只是用 Gauge 封装了 AtomicLong ,我们可以使用如下的方法获得队列大小,代码如下:
package com.zyh.maven.metricsdemo;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
public class CounterTest {
public static Queue q = new LinkedBlockingDeque();
public static Counter pendingJobs;
public static Random random = new Random();
public static void addJob(String job)
{
pendingJobs.inc();
q.offer(job);
}
public static String takeJob()
{
pendingJobs.dec();
return q.poll();
}
public static void main(String[] args) throws InterruptedException {
MetricRegistry registry = new MetricRegistry();
ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();
reporter.start(1, TimeUnit.SECONDS);
pendingJobs = registry.counter(MetricRegistry.name(Queue.class, "pending-jobs", "size"));
int num = 1;
while(true)
{
Thread.sleep(200);
if(random.nextDouble() > 0.7)
{
String job = takeJob();
System.out.println("take job :" + job);
}else{
String job = "Job-" + num;
addJob(job);
System.out.println("add Job :" + job);
}
num++;
}
}
}
运行结果
take job :Job-14
add Job :Job-26
add Job :Job-27
add Job :Job-28
add Job :Job-29
18-2-5 14:39:58 ================================================================
-- Counters --------------------------------------------------------------------
java.util.Queue.pending-jobs.size
count = 11
take job :Job-16
add Job :Job-31
add Job :Job-32
take job :Job-17
take job :Job-18
18-2-5 14:39:59 ================================================================
-- Counters --------------------------------------------------------------------
java.util.Queue.pending-jobs.size
count = 10
Meters
Meter度量一系列事件发生的速率(rate),例如TPS。Meters会统计最近1分钟,5分钟,15分钟,还有全部时间的速率。
package com.zyh.maven.metricsdemo;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class MeterTest {
public static Random random = new Random();
public static void request(Meter meter)
{
System.out.println("request");
meter.mark();
}
public static void request(Meter meter, int n)
{
while(n > 0)
{
request(meter);
n--;
}
}
public static void main(String[] args) throws InterruptedException {
MetricRegistry registry = new MetricRegistry();
ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();
reporter.start(1, TimeUnit.SECONDS);
Meter meterTps = registry.meter(MetricRegistry.name(MeterTest.class, "request", "tps"));
while(true)
{
request(meterTps, random.nextInt(5));
Thread.sleep(1000);
}
}
}
运行结果
18-2-5 14:42:44 ================================================================
-- Meters ----------------------------------------------------------------------
com.zyh.maven.metricsdemo.MeterTest.request.tps
count = 16
mean rate = 2.67 events/second
1-minute rate = 3.20 events/second
5-minute rate = 3.20 events/second
15-minute rate = 3.20 events/second
request
request
request
request
18-2-5 14:42:45 ================================================================
-- Meters ----------------------------------------------------------------------
com.zyh.maven.metricsdemo.MeterTest.request.tps
count = 20
mean rate = 2.86 events/second
1-minute rate = 3.20 events/second
5-minute rate = 3.20 events/second
15-minute rate = 3.20 events/second
Histograms
Histogram统计数据的分布情况。比如最小值,最大值,中间值,还有中位数,75百分位,90百分位,95百分位,98百分位,99百分位,和 99.9百分位的值(percentiles)。
package com.zyh.maven.metricsdemo;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.ExponentiallyDecayingReservoir;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class HistogramsTest {
public static Random random = new Random();
public static void main(String[] args) throws InterruptedException {
MetricRegistry registry = new MetricRegistry();
ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();
reporter.start(1, TimeUnit.SECONDS);
Histogram histogram = new Histogram(new ExponentiallyDecayingReservoir());
registry.register(MetricRegistry.name(HistogramsTest.class, "request", "histogram"), histogram);
while (true)
{
Thread.sleep(1000);
histogram.update(random.nextInt(100000));
}
}
}
运行结果
18-2-5 14:45:45 ================================================================
-- Histograms ------------------------------------------------------------------
com.zyh.maven.metricsdemo.HistogramsTest.request.histogram
count = 8
min = 8676
max = 94954
mean = 36405.28
stddev = 27543.74
median = 28243.00
75% <= 58814.00
95% <= 94954.00
98% <= 94954.00
99% <= 94954.00
99.9% <= 94954.00
18-2-5 14:45:46 ================================================================
-- Histograms ------------------------------------------------------------------
com.zyh.maven.metricsdemo.HistogramsTest.request.histogram
count = 9
min = 8676
max = 94954
mean = 39131.65
stddev = 26922.72
median = 28243.00
75% <= 58814.00
95% <= 94954.00
98% <= 94954.00
99% <= 94954.00
99.9% <= 94954.00
Timers
Timer其实是 Histogram 和 Meter 的结合, histogram 某部分代码/调用的耗时, meter统计TPS。
package com.zyh.maven.metricsdemo;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import java.util.Random;
import java.util.concurrent.TimeUnit;
public class TimerTest {
public static Random random = new Random();
public static void main(String[] args) throws InterruptedException {
MetricRegistry registry = new MetricRegistry();
ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();
reporter.start(1, TimeUnit.SECONDS);
Timer timer = registry.timer(MetricRegistry.name(TimerTest.class, "get-latency"));
Timer.Context ctx;
while (true)
{
ctx = timer.time();
Thread.sleep(random.nextInt(1000));
ctx.stop();
}
}
}
运行结果
18-2-5 14:48:30 ================================================================
-- Timers ----------------------------------------------------------------------
com.zyh.maven.metricsdemo.TimerTest.get-latency
count = 30
mean rate = 2.15 calls/second
1-minute rate = 2.02 calls/second
5-minute rate = 2.00 calls/second
15-minute rate = 2.00 calls/second
min = 22.82 milliseconds
max = 987.23 milliseconds
mean = 439.66 milliseconds
stddev = 263.14 milliseconds
median = 421.99 milliseconds
75% <= 582.73 milliseconds
95% <= 926.66 milliseconds
98% <= 987.23 milliseconds
99% <= 987.23 milliseconds
99.9% <= 987.23 milliseconds
上面写了几个demo尝试用了一下Metrics,在这里记录一下!