Class StatsdSink

java.lang.Object
org.microproxy.stats.StatsdSink
All Implemented Interfaces:
AutoCloseable

public final class StatsdSink extends Object implements AutoCloseable
Pushes a registry's metrics to a statsd server over UDP, every flush interval (5 seconds by default, as Envoy), from a virtual thread: counters as the increase since the last flush ( |c), gauges as their value (|g), by their flat names after the registry's prefix (envoy.cluster.direct.upstream_rq_total:3|c).

Histograms keep bucket counts rather than each value, so a histogram that recorded n values since the last flush is sent as one timer, their mean, sampled at 1/n ( envoy.http.ingress.downstream_rq_time:12.5|ms|@0.25): statsd counts it n times, which keeps the count and the mean (not the spread).

With DogStatsD tags, names are the tag-extracted ones and the tags follow (envoy.cluster.upstream_rq_xx:3|c|#envoy_cluster_name:direct, envoy_response_code_class:2). Lines are packed into datagrams of at most StatsdSink.Builder.maxPacketSize bytes. Sending is best effort: a failed send is logged and the next flush goes on.

  • Method Details

    • builder

      public static StatsdSink.Builder builder(StatsRegistry registry, String hostAndPort)
      Starts a sink of registry's metrics to the statsd server at host:port.
      Parameters:
      registry - the metrics
      hostAndPort - the server, such as 127.0.0.1:8125
      Returns:
      a builder
      Throws:
      IllegalArgumentException - if hostAndPort has no valid port
    • builder

      public static StatsdSink.Builder builder(StatsRegistry registry, InetSocketAddress target)
      Starts a sink of registry's metrics to the statsd server at target.
      Parameters:
      registry - the metrics
      target - the server's address
      Returns:
      a builder
    • start

      public StatsdSink start()
      Starts flushing every interval, on a virtual thread, until close().
      Returns:
      this sink
      Throws:
      IllegalStateException - if it was started or closed before
    • flush

      public int flush()
      Sends what changed since the last flush now.
      Returns:
      how many datagrams were sent
    • close

      public void close()
      Stops flushing, after a last flush, and closes the socket.
      Specified by:
      close in interface AutoCloseable
    • toString

      public String toString()
      Overrides:
      toString in class Object