为什么我们需要Metrics: 一般来说,作为一个后端系统。作为黑盒运行的一个后端服务,我们可能需要了解系统的运行情况及性能等等,最常见的比如:Metrics是一款Java的类库,主要用于监控和统计,可以对系统的一些性能及业务指标进行监测。
除此之外,还有很多业务及运维场景可以投入到实际使用中。
- 系统的TPS or QPS是多少?
- 一次核心业务请求的处理时间是多少ms?
- 系统的线程、队列等资源使用情况?
- 缓存命中情况?
入门使用:
添加maven依赖
MetricRegistry metrics组件的核心类是MetricRegistry,分析下该类的源码可知io.dropwizard.metrics metrics-core 3.1.0
public class MetricRegistry implements MetricSet {
private final ConcurrentMap metrics = this.buildMap();
private final List listeners = new CopyOnWriteArrayList();
public MetricRegistry() {
}
public T register(String name, T metric) throws IllegalArgumentException {
if (metric instanceof MetricSet) {
this.registerAll(name, (MetricSet)metric);
} else {
Metric existing = (Metric)this.metrics.putIfAbsent(name, metric);
if (existing != null) {
throw new IllegalArgumentException("A metric named " + name + " already exists");
}
this.onMetricAdded(name, metric);
}
return metric;
}
public void registerAll(MetricSet metrics) throws IllegalArgumentException {
this.registerAll((String)null, metrics);
}
public Counter counter(String name) {
return (Counter)this.getOrAdd(name, MetricRegistry.MetricBuilder.COUNTERS);
}
public Histogram histogram(String name) {
return (Histogram)this.getOrAdd(name, MetricRegistry.MetricBuilder.HISTOGRAMS);
}
public Meter meter(String name) {
return (Meter)this.getOrAdd(name, MetricRegistry.MetricBuilder.METERS);
}
public Timer timer(String name) {
return (Timer)this.getOrAdd(name, MetricRegistry.MetricBuilder.TIMERS);
}
MetricRegistry中维护了一个map,当向MetricRegistry register时,会将对应的Metric放入map中,该类是监测调度的主要类。该类中定义了主要的四种监测类型:counter、meter、histogram、timer。
详细了解下对应的使用方法和对应场景。 Counter counter主要用于统计,主要包含inc()和dec()方法,用于增减。 使用示例:
MetricRegistry metricRegistry= new MetricRegistry();
Counter counter = new Counter();
metricRegistry.register("counter-test", counter);
counter.inc();
System.out.println(metricRegistry.getCounters().get("counter-test").getCount());
首先示例化MetricRegistry、Counter;将counter注册到MetricRegistry中,注册时需携带一个唯一的key标识,用于标识该次监测对应的实际业务场景,在后续的实战中会需要用到这个key来做可视化。
Meter
Meter用于度量一系列事件发生的速率,主要用在统计TPS、QPS等。Meters会统计最近1分钟,5分钟,15分钟,还有全部时间的速率。 Meter的主要方法会mark(),meter中自己维护了计数器和Time相关变量用于达到统计速率的效果,使用方法和Counter类似,生成meter实例并注册。
MetricRegistry metricRegistry= new MetricRegistry();
Meter meter = new Meter();
metricRegistry.register("meter-test", meter);
meter.mark();
Histogram
Histogram统计数据的分布情况。比如最小值,最大值,中间值,还有中位数,75百分位,90百分位,95百分位,98百分位,99百分位,和 99.9百分位的值。 Meter的主要方法是update(),根据对update的调用,通过Snapshot类维护了小值,最大值,中间值等等数据。
MetricRegistry metricRegistry= new MetricRegistry();
Histogram histogram = new Histogram(new ExponentiallyDecayingReservoir());
metricRegistry.register("histogram-test", histogram);
Random random = new Random();
for (int i= 0; i < 100; i++) {
histogram.update(random.nextInt(1000));
}
System.out.println(histogram.getSnapshot());
Timer
Timer其实是 Histogram 和 Meter 的结合, histogram 某部分代码/调用的耗时, meter统计TPS。主要方法就是time()。
MetricRegistry metricRegistry= new MetricRegistry();
Timer timer = new Timer();
metricRegistry.register("timer-test", timer);
timer.time();
关于Metrics类库的基本使用就介绍到这里,后续会介绍下实战使用和可视化组件的配合使用。



