Metrics 使用

Metrics是一个系统性能度量框架,提供了Gauge、Counter、Meter、Histogram、Timer等度量工具类以及Health Check功能。

Gauge (仪表)

Gauge代表一个度量的即时值,即返回一个数值

 
  
registry.register(name(SessionStore.class, "cache-evictions"), new Gauge<Integer>() {
    @Override
    public Integer getValue() {
        return cache.getEvictionsCount();
    }
});

Counter (计数器)

final Counter evictions = registry.counter(name(SessionStore.class, "cache-evictions"));
evictions.inc();
evictions.inc(3);
evictions.dec();
evictions.dec(2);

Meter (速率计算)

final Meter getRequests = registry.meter(name(WebProxy.class, "get-requests", "requests"));
getRequests.mark();
getRequests.mark(requests.size());

Histogram (直方图)

final Histogram resultCounts = registry.histogram(name(ProductDAO.class, "result-counts");
resultCounts.update(results.size());

Timer (计时器,Meter Histogram 结合体)

final Timer timer = registry.timer(name(WebProxy.class, "get-requests"));

final Timer.Context context = timer.time();
try {
    // handle request
} finally {
    context.stop();
}
与spring的集成

   
          com.ryantenney.metrics
          metrics-spring
          3.1.3
     


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:metrics="http://www.ryantenney.com/schema/metrics"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.ryantenney.com/schema/metrics
           http://www.ryantenney.com/schema/metrics/metrics.xsd">


   
   


   
   


   
   
   


   
   
   


   
   
   
       
       
       
       
   




在使用时与spring 集成 在需要度量的方法上加上对应注解即可:
@Gauge @Counted  @Metered  @Metric @Timed

你可能感兴趣的:(日志收集)