将metrics report给graphite(carbon-relay)

一、代码

1、pom.xml

         <!-- metrics-graphite -->
         <dependency>
             <groupId>io.dropwizard.metrics</groupId>
             <artifactId>metrics-graphite</artifactId>
         </dependency>

依托于springboot1.3.0,版本号还是3.1.2

2、controller

package com.xxx.secondboot.web;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;
import com.xxx.secondboot.metrics.TestGraphiteReporter;

import io.swagger.annotations.Api;

@Api("测试metrics")
@RestController
@RequestMapping("/metrics")
public class MetricsController {
    public static Queue<String> queue = new LinkedList<>();//队列

    @RequestMapping(value = "/test1", method = RequestMethod.GET)
    public String test1() {
        final MetricRegistry registry = new MetricRegistry();
        final Graphite graphite = new Graphite("10.0.0.1", 2013);//carbon-relay地址端口
        final GraphiteReporter reporter = GraphiteReporter.forRegistry(registry)
                                         .convertRatesTo(TimeUnit.SECONDS)
                                         .convertDurationsTo(TimeUnit.MILLISECONDS)
                                         .filter(MetricFilter.ALL)
                                         .prefixedWith("reporter.graphite")
                                         .build(graphite);
        reporter.start(1, TimeUnit.SECONDS);

        registry.register(MetricRegistry.name(TestGraphiteReporter.class, "queue", "size"), new Gauge<Integer>() {
            public Integer getValue() {
                return queue.size();
            }
        });

        while (true) {
            try {
                Thread.sleep(1000);
                queue.add("job - " + LocalDateTime.now());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

注意:

这里的carbon-relay的ip写你真正要发给的relay,该relay的监听接口的IP最好设置为0.0.0.0(carbon.conf),用来监听所有连接它的服务器。这里采用了text协议(TCP协议),也可以使用pickle协议(批量发送--量是可配置的),还可以是UDP协议。

final PickledGraphite graphitePickle = new PickledGraphite("10.0.40.63", 2013);//pickle协议
final GraphiteUDP graphiteUDP = new GraphiteUDP("10.0.40.63", 2013);//UDP

二、测试

启动swagger进行测试,查看relay的listen日志。

  • 04/10/2016 14:21:37 :: MetricLineReceiver connection with ip:56769 established(调用controller方法的时候出现)
  • 04/10/2016 14:24:38 :: MetricLineReceiver connection with ip:56769 closed cleanly(停止该方法的时候出现)

查看graphite-web中左侧的metric name有没有更新

【第三十六章】 metrics(4)- metrics-graphite-LMLPHP

参考:http://metrics.dropwizard.io/3.1.0/manual/graphite/

05-11 18:11