第三十六章 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 @RestController
23 @RequestMapping("/metrics")
24 public class MetricsController {
25     public static Queue<String> queue = new LinkedList<>();//隊列
26 
27     @RequestMapping(value = "/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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM