【2016-08-08】

In addition to JMX and HTTP, Metrics also has reporters for the following outputs:

Metric Registries

        The starting point for Metrics is the  MetricRegistry  class, which is a collection of all the metrics for your application (or a subset of your application).

        Generally you only need one MetricRegistry instance per application, although you may choose to use more if you want to organize your metrics in particular reporting groups.

        Global named registries can also be shared through the static SharedMetricRegistries class. This allows the same registry to be used in different sections of code without explicitly passing a MetricRegistry instance around.

       Like all Metrics classes, SharedMetricRegistries is fully thread-safe.

Metric Names

         Each metric is associated with a  MetricRegistry , and has a unique  name  within that registry. This is a simple dotted name, like com.example.Queue.size . This flexibility allows you to encode a wide variety of context directly into a metric’s name. If you have two instances of  com.example.Queue , you can give them more specific:  com.example.Queue.requests.size  vs.  com.example.Queue.responses.size , for example.
MetricRegistry  has a set of static helper methods for easily creating names:
     
     
  1. MetricRegistry.name(Queue.class, "requests", "size")
  2. MetricRegistry.name(Queue.class, "responses", "size")
These methods will also elide any  null  values, allowing for easy optional scopes.

Gauges

         A gauge is the simplest metric type. It just returns a  value . If, for example, your application has a value which is maintained by a third-party library, you can easily expose it by registering a  Gauge  instance which returns that value:
【首先,它返回一个值,这个值是实时的,并且不限定只能是数值类型;其次,他能方便的获取到第三方库的数据】
   
   
  1. registry.register(name(SessionStore.class, "cache-evictions"), new Gauge<Integer>() {
  2. @Override
  3. public Integer getValue() {
  4. return cache.getEvictionsCount();
  5. }
  6. });

JMX Gauges

         Given that many third-party library often expose metrics only via JMX, Metrics provides the  JmxAttributeGauge  class, which takes the obje
   
   
  1. registry.register(name(SessionStore.class, "cache-evictions"),
  2. new JmxAttributeGauge("net.sf.ehcache:type=Cache,scope=sessions,name=eviction-count", "Value"));


Ratio Gauges

         A ratio gauge is a simple way to create a gauge which is the ratio between two numbers:
   
   
  1. public class CacheHitRatio extends RatioGauge {
  2. private final Meter hits;
  3. private final Timer calls;
  4. public CacheHitRatio(Meter hits, Timer calls) {
  5. this.hits = hits;
  6. this.calls = calls;
  7. }
  8. @Override
  9. public Ratio getRatio() {
  10. return Ratio.of(hits.getOneMinuteRate(),
  11. calls.getOneMinuteRate());
  12. }
  13. }
        This gauge returns the ratio of cache hits to misses using a meter and a timer.

Cached Gauges

        A cached gauge allows for a more efficient reporting of values which are expensive to calculate:
   
   
  1. registry.register(name(Cache.class, cache.getName(), "size"),
  2. new CachedGauge<Long>(10, TimeUnit.MINUTES) {
  3. @Override
  4. protected Long loadValue() {
  5. // assume this does something which takes a long time
  6. return cache.getSize();
  7. }
  8. });

Derivative Gauges

         A derivative gauge allows you to derive values from other gauges’ values:
   
   
  1. public class CacheSizeGauge extends DerivativeGauge<CacheStats, Long> {
  2. public CacheSizeGauge(Gauge<CacheStats> statsGauge) {
  3. super(statsGauge);
  4. }
  5. @Override
  6. protected Long transform(CacheStats stats) {
  7. return stats.getSize();
  8. }
  9. }


Counters

         A counter is a simple incrementing and decrementing 64-bit integer:
    
    
  1. final Counter evictions = registry.counter(name(SessionStore.class, "cache-evictions"));
  2. evictions.inc();
  3. evictions.inc(3);
  4. evictions.dec();
  5. evictions.dec(2);
        All  Counter  metrics start out at 0.

Histograms

        Histogram  measures the distribution of values in a stream of data: e.g., the number of results returned by a search:    
   
   
  1. final Histogram resultCounts = registry.histogram(name(ProductDAO.class, "result-counts");
  2. resultCounts.update(results.size());
        直方图统计,不仅可以统计简单的像最大值,最小值,平均值以及标准差,还可以统计分位数,中位数以及百分数。
         传统上,中位数(或其他任何位数)的计算方法是采取整个数据集,对其进行排序,并采取值在中间(或1 %,从年底,第99百分位数) 。这适用于小的数据集,或分批处理系统中,但不适用于高并发,低延迟服务。
        而Histograms能满足需求,是因为它采用了简单的采样数据的解决方案。 By maintaining a small, manageable reservoir which is statistically representative of the data stream as a whole, we can quickly and easily calculate quantiles which are valid approximations of the actual quantiles. This technique is called  reservoir sampling .

Meters

    
    
  1. final Meter getRequests = registry.meter(name(WebProxy.class, "get-requests", "requests"));
  2. getRequests.mark();
  3. getRequests.mark(requests.size());
        Meters有几种不同的方法测量时间发生的比率。mean rate是事件发生的平均比率。
        eg:请求的总数量/程序run的秒数。


Timers

    
    
  1. final Timer timer = registry.timer(name(WebProxy.class, "get-requests"));
  2. final Timer.Context context = timer.time();
  3. try {
  4. // handle request
  5. } finally {
  6. context.stop();
  7. }




















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值