Skip to content

Instantly share code, notes, and snippets.

@trane
Last active August 29, 2015 14:27
Show Gist options
  • Save trane/5a651a1af705a7f54aa8 to your computer and use it in GitHub Desktop.
Save trane/5a651a1af705a7f54aa8 to your computer and use it in GitHub Desktop.
package com.lookout.borderpatrol.example
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import com.twitter.finagle.stats.{Counter, Gauge, Stat, StatsReceiver}
import java.nio.channels.DatagramChannel
import com.twitter.io.Buf
class StatsdStatReceiver(port: Int) extends StatsReceiver {
private[this] val addr = new InetSocketAddress((port))
private[this] val channel = DatagramChannel.open()
override val repr: AnyRef = this
def format(names: Seq[String], value: Long, term: String): String =
s"${names.mkString(".")}:$value|$term"
def buf(utf8: String): Buf =
Buf.Utf8(utf8)
def byteBuf(buf: Buf): ByteBuffer =
Buf.ByteBuffer.Owned.extract(buf)
def serialize(names: Seq[String], value: Long, term: String): ByteBuffer =
byteBuf(buf(format(names, value, term)))
def send(bytes: ByteBuffer): Unit =
channel.send(bytes, addr)
override def counter(name: String*): Counter = new Counter {
override def incr(delta: Int): Unit =
send(serialize(name, delta.toLong, "c"))
}
override def addGauge(name: String*)(f: => Float): Gauge = new Gauge {
override def remove(): Unit =
send(serialize(name, f.toLong, "g"))
}
override def stat(name: String*): Stat = new Stat {
override def add(value: Float): Unit =
send(serialize(name, value.toLong, "h"))
}
}
@maheshkelkar
Copy link

@trane Thanks. This looks great!. However, we will be shipping metric to datadog ONLY when counter/gauge is updated. We do not have any control over how to send it over periodically (say every 1 minute). Perhaps we should take this and store it in MetricsRegistry (like finagle-metrics do) and ship it to datadog using some reporter. Let me try cooking something up and send it for your review.

@trane
Copy link
Author

trane commented Aug 21, 2015

@maheshkelkar not sure what we gain by waiting to send metrics other than a potential for losing metrics. could you explain the benefit?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment