Last active
August 29, 2015 14:27
-
-
Save trane/5a651a1af705a7f54aa8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@maheshkelkar not sure what we gain by waiting to send metrics other than a potential for losing metrics. could you explain the benefit?