metrics度量的扩展

发布 : 2019-04-25 分类 : 工具类 浏览 :

metrics度量的扩展

前言

会有一系列的文章介绍common-*.jar的各种用法,这些工具类jar包都已上传在maven中央库。可以直接通过maven坐标引入使用。源码可以参见:https://gitee.com/rjzjh/common

使用场景

metrics是java版的指标度量工具,在JAVA代码中嵌入Metrics代码,可以方便的对业务代码的各个指标进行监控,同时,Metrics能够很好的跟Ganlia、Graphite等结合,方便的提供图形化接口。只要使用java语言的开发的中间件,如果要做指标度量,基本上都会使用它,如大名鼎鼎的kafka也是用它来做指标的度量,binlog监听中间件duckula也是使用它做指标的度量。

metrics支持五种类型的指标度量:Gauges、Counters、Meters、Histograms和Timers。使用较多的也就Meters(指示最近1分钟、5分钟、15分钟一段时间业务指标的数量,也就是产生速度),Counters(指示业务指标的总量),Histograms(指示50%,75% 95% 98% 99% 99.9% 这几个百分比的达标指标)

具体如何使用不具体展开讨论,感兴趣的同学百度一下一大堆。对于初学metrics有感觉在云里雾里的感觉。common-metrics 扩展包就是为了快速使用上面五种指标来度量自己的业务。

使用示例

使用metrics前需规划好指标的分组,如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class LongivityGroup extends TsMetricAbstractGroup {
public LongivityGroup(String serviceName) {
super(serviceName);
final JmxReporter reporter = JmxReporter.forRegistry(super.getMetrics()).build();
reporter.start();
}

public final Histogram req_histogram = newHistogram(this.getClass(), "req_histogram");

public final Histogram req_count_histogram = newHistogram(this.getClass(), "req_count_histogram");

public final Meter meter_req_num = newMeter(this.getClass(), "meter_req_num");

public final Counter counter_true_num = newCounter(this.getClass(), "counter_true_num");

}

然后 在业务代码中就可以使用这些指标:

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
public void sendTest() {
long allBegin = System.currentTimeMillis();
for (int i = 0; i < 40; i++) {
ThreadPool.getDefaultPool().submit(new Runnable() {
@Override
public void run() {
SqsService sqsService = SqsClientThreadlocal.createPerThreadSqsClient();
for (int j = 0; j < 1000; j++) {
long begin = System.currentTimeMillis();
Result sendStrMsg = sqsService.sendStrMsg(AwsHelper.buildQueueName(queueName), str, null);
longivityGroup.meter_req_num.mark();
longivityGroup.counter_true_num.inc();
longivityGroup.req_histogram.update(System.currentTimeMillis() - begin);
log.info(sendStrMsg.getMessage());
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
long def = System.currentTimeMillis() - allBegin;
System.out.println("总用时=" + def / 1000);
}

这个依赖包会使用业务系统定义好的日志工具,支持主流的日志工具(log4j,log4j2,logback)产生独立于业务日志的日志文件 ,下面是日志文件的内容示例,如下:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{
"groupName": "SenderTest.3504_1547459734243",
"timestamp": "2019-01-14 17:59:34",
"metricSet": [{
"name": "10.50.8.116.SenderTest.3504_1547459734243.com.xforceplus.xplat.aws.longivity.sender.LongivityGroup.counter_true_num",
"type": "COUNTER",
"count": 40000
}, {
"name": "10.50.8.116.SenderTest.3504_1547459734243.com.xforceplus.xplat.aws.longivity.sender.LongivityGroup.req_histogram",
"type": "HISTOGRAM",
"count": 40000,
"max": 6195,
"mean": 83.18373256540761,
"min": 53,
"p50": 63.0,
"p75": 74.0,
"p95": 171.0,
"p98": 244.0,
"p99": 274.0,
"p999": 2567.0,
"stddev": 153.6140655558241
}, {
"name": "10.50.8.116.SenderTest.3504_1547459734243.com.xforceplus.xplat.aws.longivity.sender.LongivityGroup.meter_req_num",
"type": "METER",
"count": 40000,
"m15_rate": 241.24800725788393,
"m1_rate": 56.68264357679845,
"m5_rate": 197.66823310104334,
"mean_rate": 166.91082892634594,
"units": "events/second"
}]
} {
"groupName": "SenderTest.3504_1547459734243",
"timestamp": "2019-01-14 18:00:34",
"metricSet": [{
"name": "10.50.8.116.SenderTest.3504_1547459734243.com.xforceplus.xplat.aws.longivity.sender.LongivityGroup.counter_true_num",
"type": "COUNTER",
"count": 40000
}, {
"name": "10.50.8.116.SenderTest.3504_1547459734243.com.xforceplus.xplat.aws.longivity.sender.LongivityGroup.req_histogram",
"type": "HISTOGRAM",
"count": 40000,
"max": 6195,
"mean": 83.18373256540761,
"min": 53,
"p50": 63.0,
"p75": 74.0,
"p95": 171.0,
"p98": 244.0,
"p99": 274.0,
"p999": 2567.0,
"stddev": 153.6140655558241
}, {
"name": "10.50.8.116.SenderTest.3504_1547459734243.com.xforceplus.xplat.aws.longivity.sender.LongivityGroup.meter_req_num",
"type": "METER",
"count": 40000,
"m15_rate": 225.68919591470882,
"m1_rate": 20.852379243152676,
"m5_rate": 161.8370613464113,
"mean_rate": 133.48987398557296,
"units": "events/second"
}]
}

本文作者 : andy.zhou
原文链接 : https://rjzjh.gitee.io/2019/04/25/common-metrics/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

知识 & 情怀 | 二者兼得

微信扫一扫, 向我投食

微信扫一扫, 向我投食

支付宝扫一扫, 向我投食

支付宝扫一扫, 向我投食

留下足迹