title: Why are most UDP packets from a servlet filter not showing up in Graphite?
body: <p>I have the following in a servlet filter:</p>
  
  <pre class="lang-java prettyprint-override"><code>@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.");
      }
  }
  </code></pre>
  
  <p><code>reportResponseTimeToGraphite</code> simply sends a UDP packet to a Graphite server:</p>
  
  <pre class="lang-java prettyprint-override"><code>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();
  }
  </code></pre>
  
  <p>Strangely is this works <em>only some of the time</em>. Here's a sample section of the Graph, for a machine dealing with over 1000 requests per minute:</p>
  
  <p><img src="https://i.imgur.com/6X2sOUk.png" alt="Graph with partial data"></p>
  
  <p>A lot of the time, there are hour(s)-long stretches with no data points shown in the graph.</p>
  
  <p>If it's relevant, the Graphite server is running on EC2.</p>