首页 > 代码库 > 第三十六章 metrics(4)- metrics-graphite

第三十六章 metrics(4)- metrics-graphite

将metrics report给graphite(carbon-relay)

一、代码

1、pom.xml

1         <!-- metrics-graphite -->2         <dependency>3             <groupId>io.dropwizard.metrics</groupId>4             <artifactId>metrics-graphite</artifactId>5         </dependency>

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

2、controller

 1 package com.xxx.secondboot.web; 2  3 import java.time.LocalDateTime; 4 import java.util.LinkedList; 5 import java.util.Queue; 6 import java.util.concurrent.TimeUnit; 7  8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestMethod;10 import org.springframework.web.bind.annotation.RestController;11 12 import com.codahale.metrics.Gauge;13 import com.codahale.metrics.MetricFilter;14 import com.codahale.metrics.MetricRegistry;15 import com.codahale.metrics.graphite.Graphite;16 import com.codahale.metrics.graphite.GraphiteReporter;17 import com.xxx.secondboot.metrics.TestGraphiteReporter;18 19 import io.swagger.annotations.Api;20 21 @Api("测试metrics")22 @RestController23 @RequestMapping("/metrics")24 public class MetricsController {25     public static Queue<String> queue = new LinkedList<>();//队列26 27     @RequestMapping(value = "http://www.mamicode.com/test1", method = RequestMethod.GET)28     public String test1() {29         final MetricRegistry registry = new MetricRegistry();30         final Graphite graphite = new Graphite("10.0.0.1", 2013);//carbon-relay地址端口31         final GraphiteReporter reporter = GraphiteReporter.forRegistry(registry)32                                          .convertRatesTo(TimeUnit.SECONDS)33                                          .convertDurationsTo(TimeUnit.MILLISECONDS)34                                          .filter(MetricFilter.ALL)35                                          .prefixedWith("reporter.graphite")36                                          .build(graphite);37         reporter.start(1, TimeUnit.SECONDS);38 39         registry.register(MetricRegistry.name(TestGraphiteReporter.class, "queue", "size"), new Gauge<Integer>() {40             public Integer getValue() {41                 return queue.size();42             }43         });44 45         while (true) {46             try {47                 Thread.sleep(1000);48                 queue.add("job - " + LocalDateTime.now());49             } catch (InterruptedException e) {50                 e.printStackTrace();51             }52         }53     }54 }

注意:

这里的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有没有更新

技术分享

 

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

第三十六章 metrics(4)- metrics-graphite