Metrics:JVM的实时监控工具

一、背景

       最近两个月的工作中,一直在监控线上应用的情况,并且在排查线上问题的时候,发现线上的机器十分之多,为了能够自动化、平台化的监控线上应用,选择了Metrics。Metrics是给Java服务的各种指标提供监控的一种工具包。

 

二、简介

       结合项目组使用的是SpringBoot的应用部署方式和Maven的项目管理方式,引入Metrics的方式如下:

 

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

 

1、Metrics的基本工具

      Metrics提供了五中基本的度量类型:

      Gauges:度量

      Counters:计数器

      Histograms:直方图数据

      Meters:TPS计数器

      Timers:计数器

      Metrics中MetricRegistry是中心容器,它是程序中所有度量的容器,所有新的度量工具都要注册到一个MetricRegistry实例中才可以使用。这里需要说明的是,在一个应用中尽量让MetricRegistry保持单例

 

2、MetricRegistry容器

      配置MetricRegistry容器的代码如下:

 

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

 

3、Meters

      其实将Meters称作TPS计数器并不是那么准确,Meters工具会帮助我们统计系统中某个事件的速率。比如,每秒请求数TPS,每秒查询数QPS等等。这个指标能反应出当时系统的处理能力,帮助我们判断资源是否已经不足等等。Meters本身是一个计数器,并且是自增的。而获取Meters的一个对象meter,或者是实例,如下所示:

 

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

      在请求中调用mark()方法,来增加计数,代码如下所示。当然,我们可以在不同的请求中添加不同的meter,针对自己的系统完成定制的监控任务。

 

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

       引入meter后,运行应用,会在控制台输出如下信息:

 

-- 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
 

        从上面的console打印出的信息,可以看出meter是为我们提供平均速率,以及采样后的1分钟,5分钟,15分钟。

 

4、Histogram

       直方图是一种很常见的统计图表,Metrics通过Histogram这个类型提供的方便实时的数据绘制成数据直方图。和之前的Meters一样,我们通过在MetricRegistry中注册一个Histogram对象来获取一个对象,代码如下所示:

 

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

         在应用中,只要在需要统计的地方调用Histogram的update()方法。例如,我们需要统计某个网站的某个方法的流量情况:

 

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

在console上输出的信息如下:

 

-- 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提供了最小值、最大值和平均值等数据,利用这些数据,就可以绘制自定义的数据直方图了。

 

5、Counter 

Counter的本质是一个AtomicLong实例,可以增加或者减少值,可以用来统计队列中Job的总数。通过MetricRegistry注册一个Counter对象,如下所示:

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

 

在需要统计数据的位置调用inc()方法和dec()方法,同样地,在console中也会输出具体的信息,如下所示:

// 增加计数
pendingJobs.inc();
// 减去计数
pendingJobs.dec();
-- Counters --------------------------------------------------------------------
requestCount
             count = 21051

这里只是输出了当前度量的值。

 

6、Timer

       Timer其实是一个Meter和Histogram的组合。这个度量单位可以比较方便地统计请求的速率和处理时间。对于接口中调用的延迟等信息的统计就比较方便了。如果发现一个方法的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

7、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;
}

8、Reporter

       Metrics通过报表,将采集的数据展现到不同的位置,这里比如我们注册一个ConsoleReporter到MetricRegistry中,那么console中就会打印出对应的信息。

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

       除此之外Metrics还支持JMX、HTTP、Slf4j等等,可以访问 http://metrics.dropwizard.io/3.1.0/manual/core/#reporters 来查看Metrics提供的报表,如果还是不能满足自己的业务,也可以自己继承Metrics提供的ScheduledReporter类完成自定义的报表类。原文链接:http://www.jianshu.com/p/e4f70ddbc287。 

三、demo

1、配置类MetricConfig

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");
    }
}

2、接收请求的类MainController

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.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Random;
import javax.annotation.Resource;

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

    @Resource
    private Meter requestMeter;

    @Resource
    private Histogram responseSizes;

    @Resource
    private Counter pendingJobs;

    @Resource
    private Timer responses;

    @Resource
    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();
        }
    }
}

3、应用运行类DemoApplication

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);

    }
}

     

猜你喜欢

转载自zh-workhard-java.iteye.com/blog/2348533
今日推荐