metrics是一个JAVA的度量工具包,可以方便的服务进行监控和统计。目前最为流行的metrics库来自Coda Hale的dropwizard/metrics,这个库被多个知名的开源项目使用。

metrics的简单使用可以参考这里,官方文档

MetricRegistry

MetricRegistry是metrics的核心类,是用来注册多个度量工具的地方。源码如下:

public class MetricRegistry implements MetricSet {...private final ConcurrentMap<String, Metric> metrics;private final List<MetricRegistryListener> listeners;/*** Creates a new {@link MetricRegistry}.*/public MetricRegistry() {this.metrics = buildMap();this.listeners = new CopyOnWriteArrayList<>();}
...
}

其中metrics是一个线性安全的map,key是度量类的名字,value就是具体的度量类。listeners是MetricRegistryListener的一个List,在现在版本上有两个具体的实现一个是JmxListener一个MetricRegistryListenerTest,但是MetricRegistryListener我工作中并没有涉及所有具体的源码我没有看,从名字以及MetricRegistry类上的方法可以猜出来是通过观察者模式实现的监听功能类。

五种Metrics 类型

Gauges

Gauges接口是最简单的度量指标,只有一个简单的返回值

public interface Gauge<T> extends Metric {T getValue();
}

在包里也有3个实现了Gauge接口的抽象类RatioGauge、CachedGauge、DerivativeGauge。

[转]例如,我们想衡量一个待处理队列中任务的个数,代码如下

public class GaugeTest {public static Queue<String> q = new LinkedList<String>();public static void main(String[] args) throws InterruptedException {MetricRegistry registry = new MetricRegistry();ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();reporter.start(1, TimeUnit.SECONDS);registry.register(MetricRegistry.name(GaugeTest.class, "queue", "size"), new Gauge<Integer>() {public Integer getValue() {return q.size();}});while(true){Thread.sleep(1000);q.add("Job-xxx");}}
}
Counter

Counter 是一个简单的计数器,源码如下:

public class Counter implements Metric, Counting {private final LongAdder count;public Counter() {this.count = new LongAdder();}public void inc() {inc(1);}public void inc(long n) {count.add(n);}public void dec() {dec(1);}public void dec(long n) {count.add(-n);}@Overridepublic long getCount() {return count.sum();}
}

可以看出Counter只是封装了LongAdder,实现了Counting接口,返回数值。

[转]我们可以使用如下的方法,使得获得队列大小更加高效。

public class CounterTest {public static Queue<String> q = new LinkedBlockingQueue<String>();public static Counter pendingJobs;public static Random random = new Random();public static void addJob(String job) {pendingJobs.inc();q.offer(job);}public static String takeJob() {pendingJobs.dec();return q.poll();}public static void main(String[] args) throws InterruptedException {MetricRegistry registry = new MetricRegistry();ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();reporter.start(1, TimeUnit.SECONDS);pendingJobs = registry.counter(MetricRegistry.name(Queue.class,"pending-jobs","size"));int num = 1;while(true){Thread.sleep(200);if (random.nextDouble() > 0.7){String job = takeJob();System.out.println("take job : "+job);}else{String job = "Job-"+num;addJob(job);System.out.println("add job : "+job);}num++;}}
}
Meter

Meter度量一系列事件发生的速率(rate),例如TPS。Meters会统计最近1分钟,5分钟,15分钟,还有全部时间的速率。

public class MeterTest {public static Random random = new Random();public static void request(Meter meter){System.out.println("request");meter.mark();}public static void request(Meter meter, int n){while(n > 0){request(meter);n--;}}public static void main(String[] args) throws InterruptedException {MetricRegistry registry = new MetricRegistry();ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();reporter.start(1, TimeUnit.SECONDS);Meter meterTps = registry.meter(MetricRegistry.name(MeterTest.class,"request","tps"));while(true){request(meterTps,random.nextInt(5));Thread.sleep(1000);}}
}
Histogram

Histogram统计数据的分布情况。比如最小值,最大值,中间值,还有中位数,75百分位, 90百分位, 95百分位, 98百分位, 99百分位, 和 99.9百分位的值(percentiles)。
看下源码:

public class Histogram implements Metric, Sampling, Counting {private final Reservoir reservoir;private final LongAdder count;public Histogram(Reservoir reservoir) {this.reservoir = reservoir;this.count = new LongAdder();}public void update(int value) {update((long) value);}public void update(long value) {count.increment();reservoir.update(value);}@Overridepublic long getCount() {return count.sum();}@Overridepublic Snapshot getSnapshot() {return reservoir.getSnapshot();}
}

可以看到这里因为计算需要用到分布所以需要缓冲下统计区间的数据,Reservoir就是用来对数据进行缓存的。根据功能的不同目前有ExponentiallyDecayingReservoir、SlidingTimeWindowArrayReservoir、SlidingTimeWindowReservoir、SlidingWindowReservoir、UniformReservoir五个。这些类根据各自的需求将数据缓存到Snapshot,Snapshot又分为UniformSnapshot、WeightedSnapshot。
UniformSnapshot源码如下:

public class UniformSnapshot extends Snapshot {private final long[] values;public UniformSnapshot(Collection<Long> values) {final Object[] copy = values.toArray();this.values = new long[copy.length];for (int i = 0; i < copy.length; i++) {this.values[i] = (Long) copy[i];}Arrays.sort(this.values);}public UniformSnapshot(long[] values) {this.values = Arrays.copyOf(values, values.length);Arrays.sort(this.values);}@Overridepublic double getValue(double quantile) {if (quantile < 0.0 || quantile > 1.0 || Double.isNaN(quantile)) {throw new IllegalArgumentException(quantile + " is not in [0..1]");}if (values.length == 0) {return 0.0;}final double pos = quantile * (values.length + 1);final int index = (int) pos;if (index < 1) {return values[0];}if (index >= values.length) {return values[values.length - 1];}final double lower = values[index - 1];final double upper = values[index];return lower + (pos - floor(pos)) * (upper - lower);}@Overridepublic int size() {return values.length;}@Overridepublic long[] getValues() {return Arrays.copyOf(values, values.length);}@Overridepublic long getMax() {if (values.length == 0) {return 0;}return values[values.length - 1];}@Overridepublic long getMin() {if (values.length == 0) {return 0;}return values[0];}@Overridepublic double getMean() {if (values.length == 0) {return 0;}double sum = 0;for (long value : values) {sum += value;}return sum / values.length;}@Overridepublic double getStdDev() {// two-pass algorithm for variance, avoids numeric overflowif (values.length <= 1) {return 0;}final double mean = getMean();double sum = 0;for (long value : values) {final double diff = value - mean;sum += diff * diff;}final double variance = sum / (values.length - 1);return Math.sqrt(variance);}
}

很简单就是通过一个数组排序,利用quantile计算index直接取得分布。虽然简单但是疑问不懂在超大数据量的时候性能怎么样?

Histogram具体使用举个栗子:

public class HistogramTest {public static Random random = new Random();public static void main(String[] args) throws InterruptedException {MetricRegistry registry = new MetricRegistry();ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();reporter.start(1, TimeUnit.SECONDS);Histogram histogram = new Histogram(new ExponentiallyDecayingReservoir());registry.register(MetricRegistry.name(HistogramTest.class, "request", "histogram"), histogram);while(true){Thread.sleep(1000);histogram.update(random.nextInt(100000));}}
Timer

Timer其实是 Histogram 和 Meter 的结合, histogram 某部分代码/调用的耗时, meter统计TPS。

public class TimerTest {public static Random random = new Random();public static void main(String[] args) throws InterruptedException {MetricRegistry registry = new MetricRegistry();ConsoleReporter reporter = ConsoleReporter.forRegistry(registry).build();reporter.start(1, TimeUnit.SECONDS);Timer timer = registry.timer(MetricRegistry.name(TimerTest.class,"get-latency"));Timer.Context ctx;while(true){ctx = timer.time();Thread.sleep(random.nextInt(1000));ctx.stop();}}
}

总结

上面简单的简绍如何使用5种metric度量类,掺杂的简绍了一点源码。可以看出目前这个工具主要用于实时的数据统计。

metrics简单简绍相关推荐

  1. SqlMap自动化SQL注入测试工具简绍

    Sqlmap是一个开源的渗透测试工具,可以自动检测和利用SQL注入漏洞并接管数据库服务器.它配备了强大的检测引擎,为终极渗透测试仪提供了许多小众功能,以及从数据库指纹识别,从数据库获取数据到访问底层文 ...

  2. Atitit 关于艾提拉个人简绍 中文日文英文对照版

    Atitit 关于艾提拉个人简绍 日文版 经历了很多项目实践,具备较为宽广的IT从业与信息化工作背景,具备若干创业历程,道路曲折,初心不改.在相关领域累计了较深的深度(细化度)与高度(抽象度)与广度 ...

  3. windows下gradle下使用windows版普罗米修斯prometheus和metrics简单的制作一个监控java环境的内存状况的教程

    1.引入依赖(如果是maven可以自行翻译): 例如: compile 'io.prometheus:simpleclient_hotspot:0.5.0'groupid 为 io.prometheu ...

  4. Linux -- ×××服务简绍、配置及应用(2)

    二.不同的××× 技术,比方说,PPTP.SSL×××.CIPE.IPSec 等. 1. IPSec(InternetProtocol Security) IPSec是IETF(InternetEng ...

  5. 曙光服务器做系统,IT管理变简单 简析曙光Gridview 2.0软件

    对于不少服务器系统来说,用户同意管理.系统统一控制.数据统一存储.管理统一配置等问题一直困扰IT管理员,针对这些问题,曙光Gridview 2.0管理系统可以实现客服广域网的地域特性和数据资源的局部特 ...

  6. 1.Spring Security 详细简绍与入门

    Spring Security 1.Java web 应用中安全框架使用率高的莫过于: spring-security:https://spring.io/projects/spring-securi ...

  7. .net反编译软件简绍

    http://reflector.red-gate.com/Download.aspx 转载于:https://www.cnblogs.com/zhangtao/archive/2010/07/19/ ...

  8. scrapy爬虫框架简绍与安装使用

    Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中. 其最初是为了页面抓取 (更确切来说, 网络抓取 )所 ...

  9. IT、CT、UI、前端工程师以及后端工程师的简绍

    IT指的是信息技术产业,一些软件开发,软件管理: CT指的是通讯技术产业,2/3/4/5G通信技术. UI是UI设计师,是根据产品的需求,设计出图案,在界面中设计和排版对应的交互.视觉元素. 前端工程 ...

最新文章

  1. Homebrew常用命令
  2. 使用solr的DIHandler 构建mysql大表全量索引,内存溢出问题的解决方法
  3. Failed to connect to github.com port 443 after 21505 ms: Timed out
  4. HDU-2502 月之数 组合数
  5. rust笔记7 rust中的包管理
  6. mysql性能优化 洪斌_洪斌 - MySQL性能诊断与实践
  7. MATLAB GUI程序设计中ListBox控件在运行期间消失的原因及解决方法
  8. Linux上配置jupyter的步骤及与本地映射
  9. Hitool工具烧写程序(按分区烧写)
  10. JQuery.validate验证表单后Ajax异步提交
  11. 解决Dev-C++ [Error] ‘for‘ loop initial declarations are only allowed in C99 or C11 mode
  12. 音视频 | 音视频学习-01
  13. 判断是手机还是平板html,“吃鸡”用平板好还是手机好?大神给出了答案,不同理解不同格局...
  14. 值得推荐好用的网址导航网站大全
  15. 突破限制,这类网站的仅在线视频也能轻松能下载了!
  16. 翻倍增长!C-V2X商业化“提速”,新一代模组加速“助跑”
  17. python如何画3个相切的圆_使用python绘制4个相切的圆形
  18. Python获取二维数组的行列数
  19. 主动提交sitemap让谷歌、雅虎、MSN统统收录你的网站、博客
  20. 一个程序员单枪匹马,靠一个网站一年赚1个亿

热门文章

  1. H.264,x264,DivX,Xvid
  2. Python源码:根据输入的年份得到对应的生肖(要求年份>=1900)
  3. 分享自建的 Jrebel License Server 激活 Jrebel
  4. 清华计算机系录取,高考696,全省第8被清华计算机学院录取!自述回忆高考和深圳中学...
  5. QT学习之信号和槽,图片的添加
  6. mldonkey配置小结
  7. Django models新增属性后 迁移报错 KeyError ,--fake解决
  8. java实现word导入导出富文本(含图片)-附完整测试用例
  9. 自然语言处理(四): Part of Speech Tagging
  10. CF1368E Ski Accidents