首页 > 解决方案 > 石墨:如何以随机间隔发送石墨指标?

问题描述

我正在使用套接字向 Graphite 发送指标:

 String prefix = prefix
      + "." + metric + " " + value + " " + (System.currentTimeMillis() / 1000);
 socket = new Socket(host, port);
 out = new PrintWriter(socket.getOutputStream(), true);
 out.println(prefix);

这每毫秒左右发送一次指标。如何确保每 1 或 5 分钟或随机间隔发送一次?

标签: javagraphite

解决方案


把它放在一个while循环中并添加一个睡眠

while (true){
    String prefix = prefix
        + "." + metric + " " + value + " " + (System.currentTimeMillis() / 1000);
    socket = new Socket(host, port);
    out = new PrintWriter(socket.getOutputStream(), true);
    out.println(prefix);

    Thread.sleep(60);
}

为什么要以随机间隔进行?Graphite 需要精确的间隔。


推荐阅读