1

我在 servlet 过滤器中有以下内容:

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {        
    long startTimeNano = System.nanoTime();

    chain.doFilter(request, response);

    long endTimeNano = System.nanoTime();
    long differenceNano = endTimeNano - startTimeNano;
    long differenceMilliseconds = differenceNano / 1000000;

    try {
        reportResponseTimeToGraphite(differenceMilliseconds);
    } catch(Exception e) {
        Logger.log(getClass(), "Response time measure failed.");
    }
}

reportResponseTimeToGraphite只需将 UDP 数据包发送到 Graphite 服务器:

private void reportResponseTimeToGraphite(long differenceMilliseconds) throws IOException {
    int currentUnixTimeSeconds = (int) (System.currentTimeMillis() / 1000L);

    String message = GRAPHITE_LABEL + " " + String.valueOf(differenceMilliseconds) + " " + String.valueOf(currentUnixTimeSeconds);

    DatagramSocket clientSocket = new DatagramSocket();
    InetAddress recipientIP = InetAddress.getByName(GRAPHITE_SERVER);
    byte[] sendData = message.getBytes();
    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, recipientIP, GRAPHITE_PORT);

    clientSocket.send(sendPacket);
    clientSocket.close();
}

奇怪的是,这仅在某些时候有效。这是图表的示例部分,用于每分钟处理超过 1000 个请求的机器:

带有部分数据的图表

很多时候,有几个小时的时间长度,图表中没有显示任何数据点。

如果相关,Graphite 服务器正在 EC2 上运行。

4

0 回答 0