文章目录
前言
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<String> q = new LinkedList<String>();
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<Integer>(){
@Override
public Integer getValue() {
return q.size();
}
});
while (true)
{
Thread.sleep(1000);
q.add("lfwhvip");
}
}
}
运行结果 :
22-11-3 14:36:28 ================================================================
-- Gauges ----------------------------------------------------------------------
com.zyh.maven.metricsdemo.GaugeTest.queue.size
value = 1
22-11-3 14:36:29 ================================================================
-- Gauges ----------------------------------------------------------------------
com.zyh.maven.metricsdemo.GaugeTest.queue.size
value = 1
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<String> q = new LinkedBlockingDeque<String>();
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
22-11-3 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
22-11-3 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);
}
}
}
运行结果
22-11-7 16:18:38 ===============================================================
-- Meters ----------------------------------------------------------------------
com.example.jkytest.modules.MeterTest.request.tps
count = 8
mean rate = 1.60 events/second
1-minute rate = 1.60 events/second
5-minute rate = 1.60 events/second
15-minute rate = 1.60 events/second
request
request
request
request
22-11-7 16:18:39 ===============================================================
-- Meters ----------------------------------------------------------------------
com.example.jkytest.modules.MeterTest.request.tps
count = 12
mean rate = 2.00 events/second
1-minute rate = 1.60 events/second
5-minute rate = 1.60 events/second
15-minute rate = 1.60 events/second
Histograms
Histogram统计数据的分布情况。比如最小值,最大值,中间值,还有中位数,75百分位,90百分位,95百分位,98百分位,99百分位,和 99.9百分位的值(percentiles)。
package com.example.jkytest.modules;
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));
}
}
}
运行结果
-- Histograms ------------------------------------------------------------------
com.example.jkytest.modules.HistogramsTest.request.histogram
count = 1
min = 33246
max = 33246
mean = 33246.00
stddev = 0.00
median = 33246.00
75% <= 33246.00
95% <= 33246.00
98% <= 33246.00
99% <= 33246.00
99.9% <= 33246.00
22-11-7 16:26:34 ===============================================================
-- Histograms ------------------------------------------------------------------
com.example.jkytest.modules.HistogramsTest.request.histogram
count = 2
min = 33246
max = 68864
mean = 51188.56
stddev = 17808.50
median = 68864.00
75% <= 68864.00
95% <= 68864.00
98% <= 68864.00
99% <= 68864.00
99.9% <= 68864.00
Timers
Timer其实是 Histogram 和 Meter 的结合, histogram 某部分代码/调用的耗时, meter统计TPS。
package com.example.jkytest.modules;
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();
}
}
}
运行结果
-- Timers ----------------------------------------------------------------------
com.example.jkytest.modules.TimerTest.get-latency
count = 1
mean rate = 1.00 calls/second
1-minute rate = 0.00 calls/second
5-minute rate = 0.00 calls/second
15-minute rate = 0.00 calls/second
min = 560.21 milliseconds
max = 560.21 milliseconds
mean = 560.21 milliseconds
stddev = 0.00 milliseconds
median = 560.21 milliseconds
75% <= 560.21 milliseconds
95% <= 560.21 milliseconds
98% <= 560.21 milliseconds
99% <= 560.21 milliseconds
99.9% <= 560.21 milliseconds
总结
如果此篇文章有帮助到您, 希望打大佬们能
关注
、点赞
、收藏
、评论
支持一波,非常感谢大家!
如果有不对的地方请指正!!!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/88154.html