Metrics

Metrics 是一个Java库,可以让您无比深入地了解代码在生产中的作用。 度量标准提供了一个强大的工具包,用于衡量生产环境中关键组件的行为。

借助Jetty,Logback,Log4j,Apache HttpClient,Ehcache,JDBI,Jersey等公共库的模块以及Ganglia和Graphite等报告后端,Metrics为您提供全栈可见性。

快速开始

maven 引入

<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>3.1.0</version>
</dependency>

入门代码

package com.example;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;

import java.util.concurrent.TimeUnit;

/**
 * @author binbin.hou
 * date 2019/1/16
 */
public class GetStarted {

    static final MetricRegistry metrics = new MetricRegistry();
    public static void main(String args[]) {
        startReport();
        Meter requests = metrics.meter("requests");
        requests.mark();
        wait5Seconds();
    }

    static void startReport() {
        ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
        reporter.start(1, TimeUnit.SECONDS);
    }

    static void wait5Seconds() {
        try {
            Thread.sleep(5*1000);
        }
        catch(InterruptedException e) {}
    }

}

测试日志

-- Meters ----------------------------------------------------------------------
requests
             count = 1
         mean rate = 1.00 events/second
     1-minute rate = 0.00 events/second
     5-minute rate = 0.00 events/second
    15-minute rate = 0.00 events/second


19-1-16 11:53:02 ===============================================================

-- Meters ----------------------------------------------------------------------
requests
             count = 1
         mean rate = 0.50 events/second
     1-minute rate = 0.00 events/second
     5-minute rate = 0.00 events/second
    15-minute rate = 0.00 events/second


19-1-16 11:53:03 ===============================================================

-- Meters ----------------------------------------------------------------------
requests
             count = 1
         mean rate = 0.33 events/second
     1-minute rate = 0.00 events/second
     5-minute rate = 0.00 events/second
    15-minute rate = 0.00 events/second


19-1-16 11:53:04 ===============================================================

-- Meters ----------------------------------------------------------------------
requests
             count = 1
         mean rate = 0.25 events/second
     1-minute rate = 0.00 events/second
     5-minute rate = 0.00 events/second
    15-minute rate = 0.00 events/second


19-1-16 11:53:05 ===============================================================

-- Meters ----------------------------------------------------------------------
requests
             count = 1
         mean rate = 0.20 events/second
     1-minute rate = 0.00 events/second
     5-minute rate = 0.00 events/second
    15-minute rate = 0.00 events/second

参考资料

官方入门教程