介绍
Metrics是一个给JAVA服务的各项指标提供度量工具的包,在JAVA代码中嵌入Metrics代码,可以方便的对业务代码的各个指标进行监控,同时,Metrics能够很好的跟Ganlia、Graphite结合,方便的提供图形化接口。
基本使用方式直接将core包即可。
maven依赖
<!-- https://mvnrepository.com/artifact/io.dropwizard.metrics/metrics-core -->
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>4.2.9</version>
</dependency>
使用
core包主要提供如下核心功能:
- Metrics Registries类似一个metrics容器,维护一个Map,可以是一个服务一个实例。
- 支持五种metric类型:Gauges、Counters、Meters、Histograms和Timers。
- 可以将metrics值通过JMX、Console,CSV文件和SLF4J loggers发布出来。
类型 | 解释 |
---|---|
Gauge (计量器) | 统计瞬时状态的数据信息。 |
Counter(计数器) | 维护一个计数器。 |
Meters(度量器) | 度量某个时间段的平均处理次数(request per second) |
Histogram(直方图) | 统计数据的分布情况,最大值、最小值、平均值、中位数,百分比(75%、90%、95%、98%、99%和99.9%)。 |
Timers(计时器) | 统计某一块代码段的执行时间以及其分布情况,基于Histograms和Meters来实现的。 |
Metric Registry
MetricRegistry是Metric工具的入口,用于提供创建各种监控工具和存储这些监控工具,通常情况下,一个应用只需要一个MetricRegistry,并且初始化应用的时候初始化它。
如果需要多个MetricRegistry,可以使用SharedMetricRegistry来管理。SharedMetricRegistry维护一个继承自ConcurrentHashMap的REGISTRIES对象,key是注册中心的名字,value是注册中心的实例。
SharedMetricRegistry提供了一些基本的维护注册中心的方法。
Gauges
单个值的瞬时值测量。即测量时刻,测量值是多少
List<String> list = new LinkedList<>();
// 声明 MetricRegistry
MetricRegistry metricRegistry = new MetricRegistry();
// 创建控制台报告到metricRegistry
ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry)
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.build();
// 创建报告监控数据获取规则
Gauge<Integer> gauge = new Gauge<Integer>() {
@Override
public Integer getValue() {
return list.size();
}
};
// 注册Gauge到metricRegistry,并命名为connectionNUmber
metricRegistry.register("connectionNUmber", gauge);
// 3秒钟输出一次报告
reporter.start(3, TimeUnit.SECONDS);
// 模拟生成数据
for (int i = 0; i < 30; i++) {
list.add("1");
TimeUnit.SECONDS.sleep(2);
}
}
Counter
是一种特殊的Gauge度量,因为其持有的值就是一个AtomicLong
// 声明 MetricRegistry
MetricRegistry metricRegistry = new MetricRegistry();
// 创建控制台报告到metricRegistry
ConsoleReporter reporter =
ConsoleReporter.forRegistry(metricRegistry).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
// 创建一个Counter
Counter counter = new Counter();
// 注册Counter
metricRegistry.register("count",counter);
// 启动报告
reporter.start(2,TimeUnit.SECONDS);
// 模拟数据
for (;;){
counter.inc();
TimeUnit.SECONDS.sleep(2);
counter.inc();
TimeUnit.SECONDS.sleep(2);
counter.dec();
TimeUnit.SECONDS.sleep(2);
}
Meters
用于度量时间发生率,如每秒多少次。除了平均发生率之外,其还会同时测量1分钟、5分钟和15分钟的移动平均发生率
// 声明 MetricRegistry
MetricRegistry metricRegistry = new MetricRegistry();
// 创建控制台报告到metricRegistry
ConsoleReporter reporter =
ConsoleReporter.forRegistry(metricRegistry).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
// 启动报告
reporter.start(5,TimeUnit.SECONDS);
// 在容器中注册一个名称为requests的meter
Meter meter = new Meter();
metricRegistry.register("request",meter);
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
// 模拟数据
for (;;){
TimeUnit.SECONDS.sleep(threadLocalRandom.nextInt(0,4));
// 事件发生次数加1
meter.mark();
}
22-5-22 23:28:04 ===============================================================
-- Meters ----------------------------------------------------------------------
request
count = 3
mean rate = 0.60 events/second
1-minute rate = 0.60 events/second
5-minute rate = 0.60 events/second
15-minute rate = 0.60 events/second
22-5-22 23:28:09 ===============================================================
-- Meters ----------------------------------------------------------------------
request
count = 9
mean rate = 0.90 events/second
1-minute rate = 0.65 events/second
5-minute rate = 0.61 events/second
15-minute rate = 0.60 events/second
22-5-22 23:28:14 ===============================================================
-- Meters ----------------------------------------------------------------------
request
count = 10
mean rate = 0.67 events/second
1-minute rate = 0.61 events/second
5-minute rate = 0.60 events/second
15-minute rate = 0.60 events/second
22-5-22 23:28:19 ===============================================================
-- Meters ----------------------------------------------------------------------
request
count = 13
mean rate = 0.65 events/second
1-minute rate = 0.61 events/second
5-minute rate = 0.60 events/second
15-minute rate = 0.60 events/second
Histogram
用于测量一个数据流各值的统计分布。其除了能够测量最大值、最小值、平均值外,还可以测量中位数、75、90、95、98、99和99.9%等
// 声明 MetricRegistry
MetricRegistry metricRegistry = new MetricRegistry();
// 创建控制台报告到metricRegistry
ConsoleReporter reporter =
ConsoleReporter.forRegistry(metricRegistry).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
// 启动报告
reporter.start(1,TimeUnit.SECONDS);
Histogram number = metricRegistry.histogram(name(TestMetrics.class, "number"));
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
for (;;){
TimeUnit.SECONDS.sleep(1);
number.update(threadLocalRandom.nextInt(1,10));
}
22-5-22 23:34:52 ===============================================================
-- Histograms ------------------------------------------------------------------
org.example.TestMetrics.number
count = 0
min = 0
max = 0
mean = 0.00
stddev = 0.00
median = 0.00
75% <= 0.00
95% <= 0.00
98% <= 0.00
99% <= 0.00
99.9% <= 0.00
22-5-22 23:34:53 ===============================================================
-- Histograms ------------------------------------------------------------------
org.example.TestMetrics.number
count = 2
min = 1
max = 9
mean = 5.03
stddev = 4.00
median = 9.00
75% <= 9.00
95% <= 9.00
98% <= 9.00
99% <= 9.00
99.9% <= 9.00
22-5-22 23:34:54 ===============================================================
-- Histograms ------------------------------------------------------------------
org.example.TestMetrics.number
count = 3
min = 1
max = 9
mean = 5.36
stddev = 3.29
median = 6.00
75% <= 9.00
95% <= 9.00
98% <= 9.00
99% <= 9.00
99.9% <= 9.00
22-5-22 23:34:55 ===============================================================
-- Histograms ------------------------------------------------------------------
org.example.TestMetrics.number
count = 4
min = 1
max = 9
mean = 5.27
stddev = 2.84
median = 5.00
75% <= 6.00
95% <= 9.00
98% <= 9.00
99% <= 9.00
99.9% <= 9.00
Timer
Timer度量类型包含了Meter和Histogram的统计,即比率和统计信息的综合
// 声明 MetricRegistry
MetricRegistry metricRegistry = new MetricRegistry();
// 创建控制台报告到metricRegistry
ConsoleReporter reporter =
ConsoleReporter.forRegistry(metricRegistry).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
// 启动报告
reporter.start(1,TimeUnit.SECONDS);
Timer timer = metricRegistry.timer("timer");
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
for (;;){
// 新建一个context用于统计
Timer.Context context=timer.time();
// 模拟处理时间消耗
Thread.sleep(threadLocalRandom.nextInt(100,1000));
// 记录本次处理信息
context.stop();
}
22-5-22 23:43:11 ===============================================================
-- Timers ----------------------------------------------------------------------
timer
count = 1
mean rate = 0.99 calls/second
1-minute rate = 0.00 calls/second
5-minute rate = 0.00 calls/second
15-minute rate = 0.00 calls/second
min = 193.06 milliseconds
max = 193.06 milliseconds
mean = 193.06 milliseconds
stddev = 0.00 milliseconds
median = 193.06 milliseconds
75% <= 193.06 milliseconds
95% <= 193.06 milliseconds
98% <= 193.06 milliseconds
99% <= 193.06 milliseconds
99.9% <= 193.06 milliseconds
22-5-22 23:43:12 ===============================================================
-- Timers ----------------------------------------------------------------------
timer
count = 3
mean rate = 1.49 calls/second
1-minute rate = 0.00 calls/second
5-minute rate = 0.00 calls/second
15-minute rate = 0.00 calls/second
min = 193.06 milliseconds
max = 876.45 milliseconds
mean = 570.17 milliseconds
stddev = 281.71 milliseconds
median = 634.41 milliseconds
75% <= 876.45 milliseconds
95% <= 876.45 milliseconds
98% <= 876.45 milliseconds
99% <= 876.45 milliseconds
99.9% <= 876.45 milliseconds
22-5-22 23:43:13 ===============================================================
-- Timers ----------------------------------------------------------------------
timer
count = 5
mean rate = 1.65 calls/second
1-minute rate = 0.00 calls/second
5-minute rate = 0.00 calls/second
15-minute rate = 0.00 calls/second
min = 193.06 milliseconds
max = 876.45 milliseconds
mean = 574.19 milliseconds
stddev = 223.33 milliseconds
median = 634.41 milliseconds
75% <= 663.08 milliseconds
95% <= 876.45 milliseconds
98% <= 876.45 milliseconds
99% <= 876.45 milliseconds
99.9% <= 876.45 milliseconds
22-5-22 23:43:14 ===============================================================
-- Timers ----------------------------------------------------------------------
timer
count = 6
mean rate = 1.49 calls/second
1-minute rate = 0.00 calls/second
5-minute rate = 0.00 calls/second
15-minute rate = 0.00 calls/second
min = 193.06 milliseconds
max = 876.45 milliseconds
mean = 613.05 milliseconds
stddev = 220.54 milliseconds
median = 663.08 milliseconds
75% <= 801.07 milliseconds
95% <= 876.45 milliseconds
98% <= 876.45 milliseconds
99% <= 876.45 milliseconds
99.9% <= 876.45 milliseconds
参考:
https://metrics.dropwizard.io/4.1.0/getting-started.html
https://www.cnblogs.com/cocowool/p/java_metrics.html
https://www.programcreek.com/java-api-examples/index.php?api=com.yammer.metrics.core.Gauge