1.简介
系统越来越大,我们所需要关联的组件也越来越多,为了更好的排查问题,我们需要对不同的指标进行监控,从而发现问题,Metrics作为一款监控指标的度量类库,提供了许多工具帮助开发者来完成自定义的监控工作。
2.环境
添加maven依赖
io.dropwizard.metrics
metrics-core
${metrics.version}
3.使用
Metrics中MetricRegistry是中心容器,它是程序中所有度量的容器,所有新的度量工具都要注册到一个MetricRegistry实例中才可以使用,尽量在一个应用中保持让这个MetricRegistry实例保持单例。
Metrics提供了多个指标 Gauges、Counters、Histograms、Meters、Timers
3.1Timers
Timer用于统计QPS和耗时,这里用于统记DB查询所耗时间,由于try-catch-resources属性自动stop了,也可以显示调用Context stop方法
@RestController
@RequestMapping("/metric")
public class MetricCtrl {
private static final MetricRegistry metricRegistry = new MetricRegistry();
private Timer timer;
private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build();//使用控制台打印
@Autowired
private DailyTradeMarkingDao dailyTradeMarkingDao;//获取用户天维度的标记
@PostConstruct
public void setup() {
timer = metricRegistry.timer("dbQuery");
reporter.start(10, TimeUnit.SECONDS);//报告10秒输出一次
}
@PostMapping("/timer")
public JsonResult testTimer() {
List markingList = null;
try (Timer.Context context = timer.time()) {
markingList = dailyTradeMarkingDao.getByAccountIdAndStockId(401422L, 14L);
}
return JsonResult.ok(markingList);
}
}
显示结果:
-- Timers ----------------------------------------------------------------------
dbQuery
count = 7
mean rate = 0.12 calls/second
1-minute rate = 0.08 calls/second
5-minute rate = 0.02 calls/second
15-minute rate = 0.01 calls/second
min = 42.75 milliseconds
max = 1228.59 milliseconds
mean = 211.34 milliseconds
stddev = 401.27 milliseconds
median = 46.35 milliseconds
75% <= 87.25 milliseconds
95% <= 1228.59 milliseconds
98% <= 1228.59 milliseconds
99% <= 1228.59 milliseconds
99.9% <= 1228.59 milliseconds
3.2Meters
Meter用于测试QPS ,这里统计/meter接口的QPS是多少
{
meter = metricRegistry.meter("qps");
@PostMapping("/meter")
public JsonResult testMeter() {
meter.mark();//根据不同的需求设置不同的n ,默认1
return JsonResult.ok();
}
}
显示结果:
-- Meters ----------------------------------------------------------------------
qps
count = 6
mean rate = 0.02 events/second
1-minute rate = 0.09 events/second
5-minute rate = 0.02 events/second
15-minute rate = 0.01 events/second
3.3Histograms
Histogram 是一个统计图表,可以统计次数、最大、最小、平均值等, 这里记录reques长度的大小
{
histogram = metricRegistry.histogram("request.size");
@PostMapping("/histogram")
public JsonResult testHistogram(HttpServletRequest request) {
histogram.update(request.toString().length());
return JsonResult.ok();
}
}
显示结果:
-- Histograms ------------------------------------------------------------------
request.size
count = 8
min = 52
max = 52
mean = 52.00
stddev = 0.00
median = 52.00
75% <= 52.00
95% <= 52.00
98% <= 52.00
99% <= 52.00
99.9% <= 52.00
3.4 Counters
counter用来统计次数,这里用来统计大于5的个数有多少个
{
counter = metricRegistry.counter("count");
counterFive = metricRegistry.counter("counterFive");
@PostMapping("/counter")
public JsonResult testCounter() {
Random random = new Random();
counter.inc();
if (random.nextInt(10) > 5) {
counterFive.inc();
}
return JsonResult.ok();
}
}
显示结果:
-- Counters --------------------------------------------------------------------
counterFive
count = 1
count
count = 7
3.5Gauges
gauge可以实现自己的度量标准,这里用来检测缓存的大小
{
private LoadingCache cache = CacheBuilder.newBuilder()
.maximumSize(1000)
.refreshAfterWrite(10, TimeUnit.SECONDS).build(
new CacheLoader() {
@Override
public Integer load(Integer key) throws Exception {
return 1;
}
}
);
gauge = metricRegistry.gauge("cache.size", new MetricRegistry.MetricSupplier() {
@Override
public Gauge newMetric() {
return new Gauge() {
@Override
public Long getValue() {
return cache.size();
}
};
}
});
@PostMapping("/gauge")
public JsonResult testGauge() throws ExecutionException {
Random random = new Random();
cache.get(random.nextInt(100));
return JsonResult.ok(gauge.getValue());
}
}
显示结果:
-- Gauges ----------------------------------------------------------------------
cache.size
value = 10
-- Gauges ----------------------------------------------------------------------
cache.size
value = 11
4.数据上报(显示)
ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build();//输出在控制台
也可以继承ScheduledReporter实现自己的聚合格式