Metrics —— JVM上的實時監控類庫

轉載於:http://www.jianshu.com/p/e4f70ddbc287

系統開發到一定的階段,線上的機器越來越多,就需要一些監控了,除了服務器的監控,業務方面也需要一些監控服務。Metrics作爲一款監控指標的度量類庫,提供了許多工具幫助開發者來完成自定義的監控工作。

使用Metrics

通過構建一個Spring Boot的基本應用來演示Metrics的工作方式。

在Maven的pom.xml中引入Metrics

<dependency>
    <groupId>io.dropwizard.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>${metrics.version}</version>
</dependency>

目前Metrics的最新版本是3.1.2

Metrics的基本工具

Metrics提供了五個基本的度量類型:

  1. Gauges(度量)
  2. Counters(計數器)
  3. Histograms(直方圖數據)
  4. Meters(TPS計算器)
  5. Timers(計時器)

MetricsMetricRegistry是中心容器,它是程序中所有度量的容器,所有新的度量工具都要註冊到一個MetricRegistry實例中纔可以使用,儘量在一個應用中保持讓這個MetricRegistry實例保持單例。

MetricRegistry 容器

在代碼中配置好這個MetricRegistry容器:

@Bean
public MetricRegistry metrics() {
    return new MetricRegistry();
}

Meters TPS計算器

TPS計算器這個名稱並不準確,Meters工具會幫助我們統計系統中某一個事件的速率。比如每秒請求數(TPS),每秒查詢數(QPS)等等。這個指標能反應系統當前的處理能力,幫助我們判斷資源是否已經不足。Meters本身是一個自增計數器。

通過MetricRegistry可以獲得一個Meter

@Bean
public Meter requestMeter(MetricRegistry metrics) {
    return metrics.meter("request");
}

在請求中調用mark()方法,來增加計數,我們可以在不同的請求中添加不同的Meter,針對自己的系統完成定製的監控需求。

@RequestMapping("/hello")
@ResponseBody
public String helloWorld() {
    requestMeter.mark();
    return "Hello World";
}

應用運行的過程中,在console中反饋的信息:

-- Meters ----------------------------------------------------------------------
request
             count = 21055
         mean rate = 133.35 events/second
     1-minute rate = 121.66 events/second
     5-minute rate = 36.99 events/second
    15-minute rate = 13.33 events/second

從以上信息中可以看出Meter可以爲我們提供平均速率,以及採樣後的1分鐘,5分鐘,15分鐘的速率。

Histogram 直方圖數據

直方圖是一種非常常見的統計圖表,Metrics通過這個Histogram這個度量類型提供了一些方便實時繪製直方圖的數據

和之前的Meter相同,我們可以通過MetricRegistry來獲得一個Histogram

@Bean
public Histogram responseSizes(MetricRegistry metrics) {
    return metrics.histogram("response-sizes");
}

在應用中,需要統計的位置調用Histogramupdate()方法。

responseSizes.update(new Random().nextInt(10));

比如我們需要統計某個方法的網絡流量,通過Histogram就非常的方便。

在console中Histogram反饋的信息:

-- Histograms ------------------------------------------------------------------
response-sizes
             count = 21051
               min = 0
               max = 9
              mean = 4.55
            stddev = 2.88
            median = 4.00
              75% <= 7.00
              95% <= 9.00
              98% <= 9.00
              99% <= 9.00
            99.9% <= 9.00

Histogram爲我們提供了最大值,最小值和平均值等數據,利用這些數據,我們就可以開始繪製自定義的直方圖了。

Counter 計數器

Counter的本質就是一個AtomicLong實例,可以增加或者減少值,可以用它來統計隊列中Job的總數。

通過MetricRegistry也可以獲得一個Counter實例。

@Bean
public Counter pendingJobs(MetricRegistry metrics) {
    return metrics.counter("requestCount");
}

在需要統計數據的位置調用inc()dec()方法。

// 增加計數
pendingJobs.inc();
// 減去計數
pendingJobs.dec();

console的輸出非常簡單:

-- Counters --------------------------------------------------------------------
requestCount
             count = 21051

只是輸出了當前度量的值。

Timer 計時器

Timer是一個MeterHistogram的組合。這個度量單位可以比較方便地統計請求的速率和處理時間。對於接口中調用的延遲等信息的統計就比較方便了。如果發現一個方法的RPS(請求速率)很低,而且平均的處理時間很長,那麼這個方法八成出問題了。

同樣,通過MetricRegistry獲取一個Timer的實例:

@Bean
public Timer responses(MetricRegistry metrics) {
    return metrics.timer("executeTime");
}

在需要統計信息的位置使用這樣的代碼:

final Timer.Context context = responses.time();
try {
    // handle request
} finally {
    context.stop();
}

console中就會實時返回這個Timer的信息:

-- Timers ----------------------------------------------------------------------
executeTime
             count = 21061
         mean rate = 133.39 calls/second
     1-minute rate = 122.22 calls/second
     5-minute rate = 37.11 calls/second
    15-minute rate = 13.37 calls/second
               min = 0.00 milliseconds
               max = 0.01 milliseconds
              mean = 0.00 milliseconds
            stddev = 0.00 milliseconds
            median = 0.00 milliseconds
              75% <= 0.00 milliseconds
              95% <= 0.00 milliseconds
              98% <= 0.00 milliseconds
              99% <= 0.00 milliseconds
            99.9% <= 0.01 milliseconds

Gauges 度量

除了Metrics提供的幾個度量類型,我們可以通過Gauges完成自定義的度量類型。比方說很簡單的,我們想看我們緩存裏面的數據大小,就可以自己定義一個Gauges

metrics.register(
                MetricRegistry.name(ListManager.class, "cache", "size"),
                (Gauge<Integer>) () -> cache.size()
        );

這樣Metrics就會一直監控Cache的大小。

除此之外有時候,我們需要計算自己定義的一直單位,比如消息隊列裏面消費者(consumers)消費的速率生產者(producers)的生產速率的比例,這也是一個度量。

public class CompareRatio extends RatioGauge {

    private final Meter consumers;
    private final Meter producers;

    public CacheHitRatio(Meter consumers, Meter producers) {
        this.consumers = consumers;
        this.producers = producers;
    }

    @Override
    protected Ratio getRatio() {
        return Ratio.of(consumers.getOneMinuteRate(),
                producers.getOneMinuteRate());
    }
}


把這個類也註冊到Metrics容器裏面:

@Bean
public CompareRatio cacheHitRatio(MetricRegistry metrics, Meter requestMeter, Meter producers) {

    CompareRatio compareRatio = new CompareRatio(consumers, producers);

    metrics.register("生產者消費者比率", compareRatio);

    return cacheHitRatio;
}

Reporter 報表

Metrics通過報表,將採集的數據展現到不同的位置,這裏比如我們註冊一個ConsoleReporterMetricRegistry中,那麼console中就會打印出對應的信息。

@Bean
public ConsoleReporter consoleReporter(MetricRegistry metrics) {
    return ConsoleReporter.forRegistry(metrics)
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .build();
}

除此之外Metrics還支持JMXHTTPSlf4j等等,可以訪問http://metrics.dropwizard.io/3.1.0/manual/core/#reporters 來查看Metrics提供的報表,如果還是不能滿足自己的業務,也可以自己繼承Metrics提供的ScheduledReporter類完成自定義的報表類。

完整的代碼

這個demo是在一個很簡單的spring boot下運行的,關鍵的幾個類完整代碼如下。

配置類MetricConfig.java

package demo.metrics.config;

import com.codahale.metrics.*;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class MetricConfig {

    @Bean
    public MetricRegistry metrics() {
        return new MetricRegistry();
    }

    /**
     * Reporter 數據的展現位置
     *
     * @param metrics
     * @return
     */
    @Bean
    public ConsoleReporter consoleReporter(MetricRegistry metrics) {
        return ConsoleReporter.forRegistry(metrics)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
    }

    @Bean
    public Slf4jReporter slf4jReporter(MetricRegistry metrics) {
        return Slf4jReporter.forRegistry(metrics)
                .outputTo(LoggerFactory.getLogger("demo.metrics"))
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
    }

    @Bean
    public JmxReporter jmxReporter(MetricRegistry metrics) {
        return JmxReporter.forRegistry(metrics).build();
    }

    /**
     * 自定義單位
     *
     * @param metrics
     * @return
     */
    @Bean
    public ListManager listManager(MetricRegistry metrics) {
        return new ListManager(metrics);
    }

    /**
     * TPS 計算器
     *
     * @param metrics
     * @return
     */
    @Bean
    public Meter requestMeter(MetricRegistry metrics) {
        return metrics.meter("request");
    }

    /**
     * 直方圖
     *
     * @param metrics
     * @return
     */
    @Bean
    public Histogram responseSizes(MetricRegistry metrics) {
        return metrics.histogram("response-sizes");
    }

    /**
     * 計數器
     *
     * @param metrics
     * @return
     */
    @Bean
    public Counter pendingJobs(MetricRegistry metrics) {
        return metrics.counter("requestCount");
    }

    /**
     * 計時器
     *
     * @param metrics
     * @return
     */
    @Bean
    public Timer responses(MetricRegistry metrics) {
        return metrics.timer("executeTime");
    }

}


接收請求的類MainController.java

package demo.metrics.action;

import com.codahale.metrics.Counter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Timer;
import demo.metrics.config.ListManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Random;

@Controller
@RequestMapping("/")
public class MainController {

    @Autowired
    private Meter requestMeter;

    @Autowired
    private Histogram responseSizes;

    @Autowired
    private Counter pendingJobs;

    @Autowired
    private Timer responses;

    @Autowired
    private ListManager listManager;

    @RequestMapping("/hello")
    @ResponseBody
    public String helloWorld() {

        requestMeter.mark();

        pendingJobs.inc();

        responseSizes.update(new Random().nextInt(10));

        listManager.getList().add(1);

        final Timer.Context context = responses.time();
        try {
            return "Hello World";
        } finally {
            context.stop();
        }
    }
}


項目啓動類DemoApplication.java

package demo.metrics;

import com.codahale.metrics.ConsoleReporter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.concurrent.TimeUnit;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);

        // 啓動Reporter
        ConsoleReporter reporter = ctx.getBean(ConsoleReporter.class);
        reporter.start(1, TimeUnit.SECONDS);

    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章