1.簡介
系統(tǒng)越來越大,我們所需要關(guān)聯(lián)的組件也越來越多遥诉,為了更好的排查問題,我們需要對不同的指標(biāo)進(jìn)行監(jiān)控噪叙,從而發(fā)現(xiàn)問題矮锈,Metrics作為一款監(jiān)控指標(biāo)的度量類庫,提供了許多工具幫助開發(fā)者來完成自定義的監(jiān)控工作睁蕾。
2.環(huán)境
添加maven依賴
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>${metrics.version}</version>
</dependency>
3.使用
Metrics中MetricRegistry是中心容器苞笨,它是程序中所有度量的容器,所有新的度量工具都要注冊到一個MetricRegistry實例中才可以使用子眶,盡量在一個應(yīng)用中保持讓這個MetricRegistry實例保持單例瀑凝。
Metrics提供了多個指標(biāo) Gauges、Counters臭杰、Histograms粤咪、Meters、Timers
3.1Timers
Timer用于統(tǒng)計QPS和耗時硅卢,這里用于統(tǒng)記DB查詢所耗時間射窒,由于try-catch-resources屬性自動stop了,也可以顯示調(diào)用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;//獲取用戶天維度的標(biāo)記
@PostConstruct
public void setup() {
timer = metricRegistry.timer("dbQuery");
reporter.start(10, TimeUnit.SECONDS);//報告10秒輸出一次
}
@PostMapping("/timer")
public JsonResult testTimer() {
List<DailyTradeMarking> markingList = null;
try (Timer.Context context = timer.time()) {
markingList = dailyTradeMarkingDao.getByAccountIdAndStockId(401422L, 14L);
}
return JsonResult.ok(markingList);
}
}
顯示結(jié)果:
-- 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 将塑,這里統(tǒng)計/meter接口的QPS是多少
{
meter = metricRegistry.meter("qps");
@PostMapping("/meter")
public JsonResult testMeter() {
meter.mark();//根據(jù)不同的需求設(shè)置不同的n ,默認(rèn)1
return JsonResult.ok();
}
}
顯示結(jié)果:
-- 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 是一個統(tǒng)計圖表,可以統(tǒng)計次數(shù)蝌麸、最大点寥、最小、平均值等, 這里記錄reques長度的大小
{
histogram = metricRegistry.histogram("request.size");
@PostMapping("/histogram")
public JsonResult testHistogram(HttpServletRequest request) {
histogram.update(request.toString().length());
return JsonResult.ok();
}
}
顯示結(jié)果:
-- 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用來統(tǒng)計次數(shù)来吩,這里用來統(tǒng)計大于5的個數(shù)有多少個
{
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();
}
}
顯示結(jié)果:
-- Counters --------------------------------------------------------------------
counterFive
count = 1
count
count = 7
3.5Gauges
gauge可以實現(xiàn)自己的度量標(biāo)準(zhǔn)敢辩,這里用來檢測緩存的大小
{
private LoadingCache<Integer, Integer> cache = CacheBuilder.newBuilder()
.maximumSize(1000)
.refreshAfterWrite(10, TimeUnit.SECONDS).build(
new CacheLoader<Integer, Integer>() {
@Override
public Integer load(Integer key) throws Exception {
return 1;
}
}
);
gauge = metricRegistry.gauge("cache.size", new MetricRegistry.MetricSupplier<Gauge>() {
@Override
public Gauge newMetric() {
return new Gauge<Long>() {
@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());
}
}
顯示結(jié)果:
-- Gauges ----------------------------------------------------------------------
cache.size
value = 10
-- Gauges ----------------------------------------------------------------------
cache.size
value = 11
4.數(shù)據(jù)上報(顯示)
ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build();//輸出在控制臺
也可以繼承ScheduledReporter實現(xiàn)自己的聚合格式