本文概览:metrics主要由MeterRegry、度量组件组成。通过一个应用实例介绍使用metric实现打点并保存到指标到influxdb。
1 Metrics介绍
Metrics是一个记录监控指标的度量类型,类似于Logback记录日志一样,它是面向监控的度量数据。
目前常用的有dropwizard.metrics下面的库,maven的pom.xml如下
io.dropwizard.metrics
metrics-core
3.2.2
1
2
3
4
5
6
7
8
io.dropwizard.metrics
metrics-core
3.2.2
它提供了五种基本度量组件:
Gauge度量值,包含简单度量值、比率信息。
Counters 计数器
Histograms 直方图
Meters qps计算器
Timer 计时器
2 MetricRegistry
对于一个应用,应该只有一个MetricRegistry对象。MetricRegistry存放了这个应用的所有Metric,每一个Metric都有一个唯一名字。
分为MetricRegistry和SharedMetricRegistries两个,SharedMetricRegitries相对于MetricRegistry是线程安全的
对于一个应用一般只有一个MerticRegistry
3 度量组件
3.1 计数器Counter
1、功能
记录次数
2、demo代码
如下:
/**
* 测试计数器
*/
public class RegistryCounterTest {
/**
* 测试计数器
*/
public static void testCounter() {
MetricRegistry registry = new MetricRegistry();
// 1.定义counter的名字
Counter counter = registry.counter("query_gmv_count_sucess_count");
// 2.增加计数
// 默认值为1
counter.inc();
// 增加指定的数
counter.inc(3);
// 3.减少计数
// 3.1默认值是1
counter.dec();
// 3.2减少指定的数
counter.dec(2);
// 获取值
System.out.println(counter.getCount());
}
public static void main(String[] args) {
RegistryCounterTest.testCounter();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 测试计数器
*/
publicclassRegistryCounterTest{
/**
* 测试计数器
*/
publicstaticvoidtestCounter(){
MetricRegistryregistry=newMetricRegistry();
// 1.定义counter的名字
Countercounter=registry.counter("query_gmv_count_sucess_count");
// 2.增加计数
// 默认值为1
counter.inc();
// 增加指定的数
counter.inc(3);
// 3.减少计数
// 3.1默认值是1
counter.dec();
// 3.2减少指定的数
counter.dec(2);
// 获取值
System.out.println(counter.getCount());
}
publicstaticvoidmain(String[]args){
RegistryCounterTest.testCounter();
}
}
执行结果为:
1
1
1
3.2 计时器Timer
1、功能
记录执行时间
2、demo代码
package test;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
/**
* Created by wuzhonghu on 17/9/21.
*/
public class RegistryTimerTest {
/**
* 测试Timer
*/
public static void testTimer(){
MetricRegistry registry = new MetricRegistry();
// 1.定义counter的名字
Timer timer = registry.timer("query_gmv_count_sucess_time");
// 2.打印时间
// 2.1开始计时
Timer.Context context = timer.time();
// 2.2暂停2秒,模拟执行任务
try {
Thread.sleep(2000);
}catch (Exception e){
}
// 2.3 获取执行时间
System.out.println(context.stop()/1000000 + "mis");
}
public static void main(String[] args){
RegistryTimerTest.testTimer();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
packagetest;
importcom.codahale.metrics.MetricRegistry;
importcom.codahale.metrics.Timer;
/**
* Created by wuzhonghu on 17/9/21.
*/
publicclassRegistryTimerTest{
/**
* 测试Timer
*/
publicstaticvoidtestTimer(){
MetricRegistryregistry=newMetricRegistry();
// 1.定义counter的名字
Timertimer=registry.timer("query_gmv_count_sucess_time");
// 2.打印时间
// 2.1开始计时
Timer.Contextcontext=timer.time();
// 2.2暂停2秒,模拟执行任务
try{
Thread.sleep(2000);
}catch(Exceptione){
}
// 2.3 获取执行时间
System.out.println(context.stop()/1000000+"mis");
}
publicstaticvoidmain(String[]args){
RegistryTimerTest.testTimer();
}
}
执行结果为:
2001mis
1
2001mis
3.3 Gauge
1、功能
Guague是一个接口,只是用来返回一个度量值。如下
public interface Gauge extends Metric {
/**
* Returns the metric's current value.
*
* @return the metric's current value
*/
T getValue();
}
1
2
3
4
5
6
7
8
publicinterfaceGaugeextendsMetric{
/**
* Returns the metric's current value.
*
* @return the metric's current value
*/
TgetValue();
}
它包括了
自定义Gauage,返回一个度量值
RationGauage,返回一个比率值。
Cached Guages
Derivative Guages
3.3.1 自定义
代码如下:
public class RegistryGaugeTest {
/**
*自定义
*/
public static void testGauge() {
MetricRegistry registry = new MetricRegistry();
Gauge gauge = registry.register("selfGuage", new Gauge() {
public Integer getValue() {
return 1;
}
});
System.out.println(gauge.getValue());
}
public static void main(Strin