Chinaunix首页 | 论坛 | 博客
  • 博客访问: 495677
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1916
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-11 22:01
个人简介

从事实时计算多年,熟悉jstorm/spark/flink/kafka/rocketMq, 热衷于开源,希望在这里和前辈们一起学习与分享,得到长足的进步!邮箱:hustfxj@gmail.com 我的githup地址是:https://github.com/hustfxj。欢迎和大家一起交流探讨问题。

文章分类

全部博文(80)

文章存档

2017年(11)

2015年(3)

2014年(33)

2013年(33)

分类: Java

2014-08-15 13:38:08

Metrics是一个给JAVA服务的各项指标提供度量工具的包,在JAVA代码中嵌入Metrics代码,可以方便的对业务代码的各个指标进行监控,同时,Metrics能够很好的跟Ganlia、Graphite结合,方便的提供图形化接口。基本使用方式直接将core包(目前稳定版本3.0.1)导入pom文件即可,配置如下:

<dependency> <groupId>com.codahale.metricsgroupId> <artifactId>metrics-coreartifactId> <version>3.0.1version> dependency>

core包主要提供如下核心功能:

  • Metrics Registries类似一个metrics容器,维护一个Map,可以是一个服务一个实例。
  • 支持五种metric类型:Gauges、Counters、Meters、Histograms和Timers。
  • 可以将metrics值通过JMX、Console,CSV文件和SLF4J loggers发布出来。

五种Metrics类型:

1.       Gauges

Gauges是一个最简单的计量,一般用来统计瞬时状态的数据信息,比如系统中处于pending状态的job。测试代码

点击(此处)折叠或打开

  1. package com.netease.test.metrics;

  2. import com.codahale.metrics.ConsoleReporter;
  3. import com.codahale.metrics.Gauge;
  4. import com.codahale.metrics.JmxReporter;
  5. import com.codahale.metrics.MetricRegistry;

  6. import java.util.Queue;
  7. import java.util.concurrent.LinkedBlockingDeque;
  8. import java.util.concurrent.TimeUnit;

  9. /**
  10.  * User: hzwangxx
  11.  * Date: 14-2-17
  12.  * Time: 14:47
  13.  * 测试Gauges,实时统计pending状态的job个数
  14.  */
  15. public class TestGauges {
  16.     /**
  17.      * 实例化一个registry,最核心的一个模块,相当于一个应用程序的metrics系统的容器,维护一个Map
  18.      */
  19.     private static final MetricRegistry metrics = new MetricRegistry();

  20.     private static Queue<String> queue = new LinkedBlockingDeque<String>();

  21.     /**
  22.      * 在控制台上打印输出
  23.      */
  24.     private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();

  25.     public static void main(String[] args) throws InterruptedException {
  26.         reporter.start(3, TimeUnit.SECONDS);

  27.         //实例化一个Gauge
  28.         Gauge<Integer> gauge = new Gauge<Integer>() {
  29.             @Override
  30.             public Integer getValue() {
  31.                 return queue.size();
  32.             }
  33.         };

  34.         //注册到容器中
  35.         metrics.register(MetricRegistry.name(TestGauges.class, "pending-job", "size"), gauge);

  36.         //测试JMX
  37.         JmxReporter jmxReporter = JmxReporter.forRegistry(metrics).build();
  38.         jmxReporter.start();

  39.         //模拟数据
  40.         for (int i=0; i<20; i++){
  41.             queue.add("a");
  42.             Thread.sleep(1000);
  43.         }

  44.     }
  45. }

  46. /*
  47. console output:
  48. 14-2-17 15:29:35 ===============================================================

  49. -- Gauges ----------------------------------------------------------------------
  50. com.netease.test.metrics.TestGauges.pending-job.size
  51.              value = 4


  52. 14-2-17 15:29:38 ===============================================================

  53. -- Gauges ----------------------------------------------------------------------
  54. com.netease.test.metrics.TestGauges.pending-job.size
  55.              value = 6


  56. 14-2-17 15:29:41 ===============================================================

  57. -- Gauges ----------------------------------------------------------------------
  58. com.netease.test.metrics.TestGauges.pending-job.size
  59.              value = 9
  60.  */


通过以上步骤将会向MetricsRegistry容器中注册一个名字为com.netease.test.metrics .TestGauges.pending-job.size的metrics,实时获取队列长度的指标。另外,Core包种还扩展了几种特定的Gauge:
  • JMX Gauges—提供给第三方库只通过JMX将指标暴露出来。
  • Ratio Gauges—简单地通过创建一个gauge计算两个数的比值。
  • Cached Gauges—对某些计量指标提供缓存

Derivative Gauges—提供Gauge的值是基于其他Gauge值的接口。

2.       Counter

Counter是Gauge的一个特例,维护一个计数器,可以通过inc()和dec()方法对计数器做修改。使用步骤与Gauge基本类似,在MetricRegistry中提供了静态方法可以直接实例化一个Counter。


点击(此处)折叠或打开

  1. package com.netease.test.metrics;

  2. import com.codahale.metrics.ConsoleReporter;
  3. import com.codahale.metrics.Counter;
  4. import com.codahale.metrics.MetricRegistry;

  5. import java.util.LinkedList;
  6. import java.util.Queue;
  7. import java.util.concurrent.TimeUnit;
  8. import static com.codahale.metrics.MetricRegistry.*;
  9. /**
  10.  * User: hzwangxx
  11.  * Date: 14-2-14
  12.  * Time: 14:02
  13.  * 测试Counter
  14.  */
  15. public class TestCounter {

  16.     /**
  17.      * 实例化一个registry,最核心的一个模块,相当于一个应用程序的metrics系统的容器,维护一个Map
  18.      */
  19.     private static final MetricRegistry metrics = new MetricRegistry();

  20.     /**
  21.      * 在控制台上打印输出
  22.      */
  23.     private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();

  24.     /**
  25.      * 实例化一个counter,同样可以通过如下方式进行实例化再注册进去
  26.      * pendingJobs = new Counter();
  27.      * metrics.register(MetricRegistry.name(TestCounter.class, "pending-jobs"), pendingJobs);
  28.      */
  29.     private static Counter pendingJobs = metrics.counter(name(TestCounter.class, "pedding-jobs"));
  30. // private static Counter pendingJobs = metrics.counter(MetricRegistry.name(TestCounter.class, "pedding-jobs"));



  31.     private static Queue<String> queue = new LinkedList<String>();

  32.     public static void add(String str) {
  33.         pendingJobs.inc();
  34.         queue.offer(str);
  35.     }

  36.     public String take() {
  37.         pendingJobs.dec();
  38.         return queue.poll();
  39.     }

  40.     public static void main(String[]args) throws InterruptedException {
  41.         reporter.start(3, TimeUnit.SECONDS);
  42.         while(true){
  43.             add("1");
  44.             Thread.sleep(1000);
  45.         }

  46.     }
  47. }

  48. /*
  49. console output:
  50. 14-2-17 17:52:34 ===============================================================

  51. -- Counters --------------------------------------------------------------------
  52. com.netease.test.metrics.TestCounter.pedding-jobs
  53.              count = 4


  54. 14-2-17 17:52:37 ===============================================================

  55. -- Counters --------------------------------------------------------------------
  56. com.netease.test.metrics.TestCounter.pedding-jobs
  57.              count = 6


  58. 14-2-17 17:52:40 ===============================================================

  59. -- Counters --------------------------------------------------------------------
  60. com.netease.test.metrics.TestCounter.pedding-jobs
  61.              count = 9

  62.  */

3.       Meters

Meters用来度量某个时间段的平均处理次数(request per second),每1、5、15分钟的TPS。比如一个service的请求数,通过metrics.meter()实例化一个Meter之后,然后通过meter.mark()方法就能将本次请求记录下来。统计结果有总的请求数,平均每秒的请求数,以及最近的1、5、15分钟的平均TPS。


点击(此处)折叠或打开

  1. package com.netease.test.metrics;

  2. import com.codahale.metrics.ConsoleReporter;
  3. import com.codahale.metrics.Meter;
  4. import com.codahale.metrics.MetricRegistry;

  5. import java.util.concurrent.TimeUnit;

  6. import static com.codahale.metrics.MetricRegistry.*;

  7. /**
  8.  * User: hzwangxx
  9.  * Date: 14-2-17
  10.  * Time: 18:34
  11.  * 测试Meters
  12.  */
  13. public class TestMeters {
  14.     /**
  15.      * 实例化一个registry,最核心的一个模块,相当于一个应用程序的metrics系统的容器,维护一个Map
  16.      */
  17.     private static final MetricRegistry metrics = new MetricRegistry();

  18.     /**
  19.      * 在控制台上打印输出
  20.      */
  21.     private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();

  22.     /**
  23.      * 实例化一个Meter
  24.      */
  25.     private static final Meter requests = metrics.meter(name(TestMeters.class, "request"));

  26.     public static void handleRequest() {
  27.         requests.mark();
  28.     }

  29.     public static void main(String[] args) throws InterruptedException {
  30.         reporter.start(3, TimeUnit.SECONDS);
  31.         while(true){
  32.             handleRequest();
  33.             Thread.sleep(100);
  34.         }
  35.     }

  36. }

  37. /*
  38. 14-2-17 18:43:08 ===============================================================

  39. -- Meters ----------------------------------------------------------------------
  40. com.netease.test.metrics.TestMeters.request
  41.              count = 30
  42.          mean rate = 9.95 events/second
  43.      1-minute rate = 0.00 events/second
  44.      5-minute rate = 0.00 events/second
  45.     15-minute rate = 0.00 events/second


  46. 14-2-17 18:43:11 ===============================================================

  47. -- Meters ----------------------------------------------------------------------
  48. com.netease.test.metrics.TestMeters.request
  49.              count = 60
  50.          mean rate = 9.99 events/second
  51.      1-minute rate = 10.00 events/second
  52.      5-minute rate = 10.00 events/second
  53.     15-minute rate = 10.00 events/second


  54. 14-2-17 18:43:14 ===============================================================

  55. -- Meters ----------------------------------------------------------------------
  56. com.netease.test.metrics.TestMeters.request
  57.              count = 90
  58.          mean rate = 9.99 events/second
  59.      1-minute rate = 10.00 events/second
  60.      5-minute rate = 10.00 events/second
  61.     15-minute rate = 10.00 events/second
  62. */

4.       Histograms

Histograms主要使用来统计数据的分布情况,最大值、最小值、平均值、中位数,百分比(75%、90%、95%、98%、99%和99.9%)。例如,需要统计某个页面的请求响应时间分布情况,可以使用该种类型的Metrics进行统计。具体的样例代码如下:


点击(此处)折叠或打开

  1. package com.netease.test.metrics;

  2. import com.codahale.metrics.ConsoleReporter;
  3. import com.codahale.metrics.Histogram;
  4. import com.codahale.metrics.MetricRegistry;

  5. import java.util.Random;
  6. import java.util.concurrent.TimeUnit;

  7. import static com.codahale.metrics.MetricRegistry.name;

  8. /**
  9.  * User: hzwangxx
  10.  * Date: 14-2-17
  11.  * Time: 18:34
  12.  * 测试Histograms
  13.  */
  14. public class TestHistograms {
  15.     /**
  16.      * 实例化一个registry,最核心的一个模块,相当于一个应用程序的metrics系统的容器,维护一个Map
  17.      */
  18.     private static final MetricRegistry metrics = new MetricRegistry();

  19.     /**
  20.      * 在控制台上打印输出
  21.      */
  22.     private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();

  23.     /**
  24.      * 实例化一个Histograms
  25.      */
  26.     private static final Histogram randomNums = metrics.histogram(name(TestHistograms.class, "random"));

  27.     public static void handleRequest(double random) {
  28.         randomNums.update((int) (random*100));
  29.     }

  30.     public static void main(String[] args) throws InterruptedException {
  31.         reporter.start(3, TimeUnit.SECONDS);
  32.         Random rand = new Random();
  33.         while(true){
  34.             handleRequest(rand.nextDouble());
  35.             Thread.sleep(100);
  36.         }
  37.     }

  38. }

  39. /*
  40. 14-2-17 19:39:11 ===============================================================

  41. -- Histograms ------------------------------------------------------------------
  42. com.netease.test.metrics.TestHistograms.random
  43.              count = 30
  44.                min = 1
  45.                max = 97
  46.               mean = 45.93
  47.             stddev = 29.12
  48.             median = 39.50
  49.               75% <= 71.00
  50.               95% <= 95.90
  51.               98% <= 97.00
  52.               99% <= 97.00
  53.             99.9% <= 97.00


  54. 14-2-17 19:39:14 ===============================================================

  55. -- Histograms ------------------------------------------------------------------
  56. com.netease.test.metrics.TestHistograms.random
  57.              count = 60
  58.                min = 0
  59.                max = 97
  60.               mean = 41.17
  61.             stddev = 28.60
  62.             median = 34.50
  63.               75% <= 69.75
  64.               95% <= 92.90
  65.               98% <= 96.56
  66.               99% <= 97.00
  67.             99.9% <= 97.00


  68. 14-2-17 19:39:17 ===============================================================

  69. -- Histograms ------------------------------------------------------------------
  70. com.netease.test.metrics.TestHistograms.random
  71.              count = 90
  72.                min = 0
  73.                max = 97
  74.               mean = 44.67
  75.             stddev = 28.47
  76.             median = 43.00
  77.               75% <= 71.00
  78.               95% <= 91.90
  79.               98% <= 96.18
  80.               99% <= 97.00
  81.             99.9% <= 97.00
  82. */


5.       Timers

Timers主要是用来统计某一块代码段的执行时间以及其分布情况,具体是基于Histograms和Meters来实现的。样例代码如下:

点击(此处)折叠或打开

  1. package com.netease.test.metrics;

  2. import com.codahale.metrics.ConsoleReporter;
  3. import com.codahale.metrics.MetricRegistry;
  4. import com.codahale.metrics.Timer;

  5. import java.util.Random;
  6. import java.util.concurrent.TimeUnit;

  7. import static com.codahale.metrics.MetricRegistry.name;

  8. /**
  9.  * User: hzwangxx
  10.  * Date: 14-2-17
  11.  * Time: 18:34
  12.  * 测试Timers
  13.  */
  14. public class TestTimers {
  15.     /**
  16.      * 实例化一个registry,最核心的一个模块,相当于一个应用程序的metrics系统的容器,维护一个Map
  17.      */
  18.     private static final MetricRegistry metrics = new MetricRegistry();

  19.     /**
  20.      * 在控制台上打印输出
  21.      */
  22.     private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();

  23.     /**
  24.      * 实例化一个Meter
  25.      */
  26. // private static final Timer requests = metrics.timer(name(TestTimers.class, "request"));
  27.     private static final Timer requests = metrics.timer(name(TestTimers.class, "request"));

  28.     public static void handleRequest(int sleep) {
  29.         Timer.Context context = requests.time();
  30.         try {
  31.             //some operator
  32.             Thread.sleep(sleep);
  33.         } catch (InterruptedException e) {
  34.             e.printStackTrace();
  35.         } finally {
  36.             context.stop();
  37.         }

  38.     }

  39.     public static void main(String[] args) throws InterruptedException {
  40.         reporter.start(3, TimeUnit.SECONDS);
  41.         Random random = new Random();
  42.         while(true){
  43.             handleRequest(random.nextInt(1000));
  44.         }
  45.     }

  46. }

  47. /*
  48. 14-2-18 9:31:54 ================================================================

  49. -- Timers ----------------------------------------------------------------------
  50. com.netease.test.metrics.TestTimers.request
  51.              count = 4
  52.          mean rate = 1.33 calls/second
  53.      1-minute rate = 0.00 calls/second
  54.      5-minute rate = 0.00 calls/second
  55.     15-minute rate = 0.00 calls/second
  56.                min = 483.07 milliseconds
  57.                max = 901.92 milliseconds
  58.               mean = 612.64 milliseconds
  59.             stddev = 196.32 milliseconds
  60.             median = 532.79 milliseconds
  61.               75% <= 818.31 milliseconds
  62.               95% <= 901.92 milliseconds
  63.               98% <= 901.92 milliseconds
  64.               99% <= 901.92 milliseconds
  65.             99.9% <= 901.92 milliseconds


  66. 14-2-18 9:31:57 ================================================================

  67. -- Timers ----------------------------------------------------------------------
  68. com.netease.test.metrics.TestTimers.request
  69.              count = 8
  70.          mean rate = 1.33 calls/second
  71.      1-minute rate = 1.40 calls/second
  72.      5-minute rate = 1.40 calls/second
  73.     15-minute rate = 1.40 calls/second
  74.                min = 41.07 milliseconds
  75.                max = 968.19 milliseconds
  76.               mean = 639.50 milliseconds
  77.             stddev = 306.12 milliseconds
  78.             median = 692.77 milliseconds
  79.               75% <= 885.96 milliseconds
  80.               95% <= 968.19 milliseconds
  81.               98% <= 968.19 milliseconds
  82.               99% <= 968.19 milliseconds
  83.             99.9% <= 968.19 milliseconds


  84. 14-2-18 9:32:00 ================================================================

  85. -- Timers ----------------------------------------------------------------------
  86. com.netease.test.metrics.TestTimers.request
  87.              count = 15
  88.          mean rate = 1.67 calls/second
  89.      1-minute rate = 1.40 calls/second
  90.      5-minute rate = 1.40 calls/second
  91.     15-minute rate = 1.40 calls/second
  92.                min = 41.07 milliseconds
  93.                max = 968.19 milliseconds
  94.               mean = 591.35 milliseconds
  95.             stddev = 302.96 milliseconds
  96.             median = 650.56 milliseconds
  97.               75% <= 838.07 milliseconds
  98.               95% <= 968.19 milliseconds
  99.               98% <= 968.19 milliseconds
  100.               99% <= 968.19 milliseconds
  101.             99.9% <= 968.19 milliseconds

  102. */

6  Health Checks

Metrics提供了一个独立的模块:Health Checks,用于对Application、其子模块或者关联模块的运行是否正常做检测。该模块是独立metrics-core模块的,使用时则导入metrics-healthchecks包。

<dependency> <groupId>com.codahale.metricsgroupId> <artifactId>metrics-healthchecksartifactId> <version>3.0.1version> dependency>

使用起来和与上述几种类型的Metrics有点类似,但是需要重新实例化一个Metrics容器HealthCheckRegistry,待检测模块继承抽象类HealthCheck并实现check()方法即可,然后将该模块注册到HealthCheckRegistry中,判断的时候通过isHealthy()接口即可。如下示例代码:


点击(此处)折叠或打开

  1. package com.netease.test.metrics;

  2. import com.codahale.metrics.health.HealthCheck;
  3. import com.codahale.metrics.health.HealthCheckRegistry;

  4. import java.util.Map;
  5. import java.util.Random;

  6. /**
  7.  * User: hzwangxx
  8.  * Date: 14-2-18
  9.  * Time: 9:57
  10.  */
  11. public class DatabaseHealthCheck extends HealthCheck{
  12.     private final Database database;

  13.     public DatabaseHealthCheck(Database database) {
  14.         this.database = database;
  15.     }

  16.     @Override
  17.     protected Result check() throws Exception {
  18.         if (database.ping()) {
  19.             return Result.healthy();
  20.         }
  21.         return Result.unhealthy("Can't ping database.");
  22.     }

  23.     /**
  24.      * 模拟Database对象
  25.      */
  26.     static class Database {
  27.         /**
  28.          * 模拟database的ping方法
  29.          * @return 随机返回boolean值
  30.          */
  31.         public boolean ping() {
  32.             Random random = new Random();
  33.             return random.nextBoolean();
  34.         }
  35.     }

  36.     public static void main(String[] args) {
  37. // MetricRegistry metrics = new MetricRegistry();
  38. // ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();
  39.         HealthCheckRegistry registry = new HealthCheckRegistry();
  40.         registry.register("database1", new DatabaseHealthCheck(new Database()));
  41.         registry.register("database2", new DatabaseHealthCheck(new Database()));
  42.         while (true) {
  43.             for (Map.Entry<String, Result> entry : registry.runHealthChecks().entrySet()) {
  44.                 if (entry.getValue().isHealthy()) {
  45.                     System.out.println(entry.getKey() + ": OK");
  46.                 } else {
  47.                     System.err.println(entry.getKey() + ": FAIL, error message: " + entry.getValue().getMessage());
  48.                     final Throwable e = entry.getValue().getError();
  49.                     if (e != null) {
  50.                         e.printStackTrace();
  51.                     }
  52.                 }
  53.             }
  54.             try {
  55.                 Thread.sleep(1000);
  56.             } catch (InterruptedException e) {

  57.             }
  58.         }
  59.     }
  60. }

  61. /*
  62. console output:
  63. database1: OK
  64. database2: FAIL, error message: Can't ping database.
  65. database1: FAIL, error message: Can't ping database.
  66. database2: OK
  67. database1: OK
  68. database2: FAIL, error message: Can't ping database.
  69. database1: FAIL, error message: Can't ping database.
  70. database2: OK
  71. database1: FAIL, error message: Can't ping database.
  72. database2: FAIL, error message: Can't ping database.
  73. database1: FAIL, error message: Can't ping database.
  74. database2: FAIL, error message: Can't ping database.
  75. database1: OK
  76. database2: OK
  77. database1: OK
  78. database2: FAIL, error message: Can't ping database.
  79. database1: FAIL, error message: Can't ping database.
  80. database2: OK
  81. database1: OK
  82. database2: OK
  83. database1: FAIL, error message: Can't ping database.
  84. database2: OK
  85. database1: OK
  86. database2: OK
  87. database1: OK
  88. database2: OK
  89. database1: OK
  90. database2: FAIL, error message: Can't ping database.
  91. database1: FAIL, error message: Can't ping database.
  92. database2: FAIL, error message: Can't ping database.

  93.  */

其他支持

metrics提供了对Ehcache、Apache HttpClient、JDBI、Jersey、Jetty、Log4J、Logback、JVM等的集成,可以方便地将Metrics输出到Ganglia、Graphite中,供用户图形化展示。

阅读(6456) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~