将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有没有更新

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

【第三十六章】 metrics(4)- metrics-graphite的更多相关文章

  1. Gradle 1.12用户指南翻译——第三十六章. Sonar Runner 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  2. “全栈2019”Java第三十六章:类

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

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

    将metrics report给graphite(carbon-relay) 一.代码 1.pom.xml <!-- metrics-graphite --> <dependency ...

  4. “全栈2019”Java多线程第三十六章:如何设置线程的等待截止时间

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 下一章 "全栈2019"J ...

  5. 【WPF学习】第三十六章 样式基础

    前面三章介绍了WPF资源系统,使用资源可在一个地方定义对象而在整个标记中重用他们.尽管可使用资源存储各种对象,但使用资源最常见的原因之一是通过他们的保存样式. 样式是可应用于元素的属性值集合.WPF样 ...

  6. 第三十六章 Linux常用性能检测的指令

    作为一个Linux运维人员,介绍下常用的性能检测指令! 一.uptime 命令返回的信息: 19:08:17              //系统当前时间 up 127 days,  3:00     ...

  7. SpringBoot | 第三十六章:集成多CacheManager

    前言 今天有网友咨询了一个问题:如何在一个工程中使用多种缓存进行差异化缓存,即实现多个cacheManager灵活切换.原来没有遇见这种场景,今天下班抽空试了下,以下就把如何实现的简单记录下. 一点知 ...

  8. 第三十六章、PyQt输入部件:QAbstractSpinBox派生类QSpinBox、 QDoubleSpinBox、QDateTimeEdit、QDateEdit和QTimeEdit

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 Designer输入部件中的Spin B ...

  9. 程序员编程艺术第三十六~三十七章、搜索智能提示suggestion,附近点搜索

    第三十六~三十七章.搜索智能提示suggestion,附近地点搜索 作者:July.致谢:caopengcs.胡果果.时间:二零一三年九月七日. 题记 写博的近三年,整理了太多太多的笔试面试题,如微软 ...

随机推荐

  1. django时间的时区问题

    在用django1.8版本做项目的时候遇到时间的存储与读取不一致的问题,网上找了很多帖子,但都没有讲明白.本文将在项目中遇到的问题及如何解决的尽可能详细的记录下来,当然本文参考了网上大量相关文章. 在 ...

  2. 7 jmeter之参数化

    badboy里参数化(前面4 jmeter badboy脚本开发技术详解已讲过) jmeter里参数化-1 用户参数 1.打开badboy工具,点击红色按钮开始录制,在地址栏目中输入地址:www.so ...

  3. 微软官方出的各种dll丢失的修复工具

    例如 :因为计算机中丢失 api-ms-win-crt-runtime-l1-1-0.dll.尝试重新安装该程序以解决此问题. 软件名称: Visual C++ Redistributable for ...

  4. caffe学习记录

    结论: caffe网络的prototxt训练与测试的时候用的是不同的,训练的时候用的prototxt里面有test只是为了测试网络的训练程度,里面的测试集是验证集,并不是真正我们测试的时候用的网络定义 ...

  5. Spark SQL初始化和创建DataFrame的几种方式

    一.前述       1.SparkSQL介绍 Hive是Shark的前身,Shark是SparkSQL的前身,SparkSQL产生的根本原因是其完全脱离了Hive的限制. SparkSQL支持查询原 ...

  6. 畅通工程续 (SPFA模板Floy模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1874 SPFA #include <iostream> #include <stdio.h&g ...

  7. spring的bean容器加载

    1.在单独使用的时候可以通过ClassPathXmlApplicationContext(“配置文件.xml”);来启动容器. 2.在MVC下是通过启动servlet容器,初始化DispatcherS ...

  8. Andrew Ng-ML-第十章-应用机器学习的建议

    1.如何改进性能不好的学习算法 图1.运用到测试集上效果不佳 当进行一个正则化线性回归时,最小化了代价函数得到参数,但是运用到新的测试集上,发现效果不好,那么如何改进? 1).增加训练集.但是实际上花 ...

  9. PAT Radix[二分][进制转换][难]

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  10. Amazon OA

    Remove Duplicates from unsorted array,它的错误在于9-10行k out of bound,改成下面这样就没问题了 public class removeDupli ...