Skip to content

Instantly share code, notes, and snippets.

@bfil
Created April 3, 2016 17:34
Show Gist options
  • Save bfil/1f0c1d6d6544ce526bbf2c217959e145 to your computer and use it in GitHub Desktop.
Save bfil/1f0c1d6d6544ce526bbf2c217959e145 to your computer and use it in GitHub Desktop.
Kamon Metrics Collector
import java.nio.LongBuffer
import akka.actor.{ActorLogging, Actor}
import kamon.metric._
import kamon.metric.instrument.{Histogram, CollectionContext}
class MetricsCollector extends Actor with ActorLogging {
import MetricsCollector._
def ms(nanos: Long) = nanos / 1000 / 1000
var histograms: Map[Entity, Histogram.Snapshot] = Map.empty
def extractMetrics(): Iterable[EntityMetrics] = histograms.map { case (entity, histogram) =>
EntityMetrics(entity.name, ms(histogram.min), ms(histogram.max), ms(histogram.sum / histogram.numberOfMeasurements))
}
def receive = {
case tickSnapshot: SubscriptionsDispatcher.TickMetricSnapshot =>
tickSnapshot.metrics.map { case (entity, snapshot) =>
snapshot.histogram("elapsed-time").map { newHistogram =>
val histogram = histograms.get(entity).getOrElse(Histogram.Snapshot.empty)
val mergedHistograms = histogram.merge(newHistogram, collectionContext)
histograms += (entity -> mergedHistograms)
}
}
case GetMetrics => sender ! extractMetrics()
}
val collectionContext = new CollectionContext {
val buffer: LongBuffer = LongBuffer.allocate(10000)
}
}
object MetricsCollector {
case class EntityMetrics(name: String, min: Long, max: Long, avg: Long)
case object GetMetrics
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment