aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/akka/MailboxMetrics.scala
blob: 6bf65cc7a70c1dcd976574664cb58ac6b24341c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package akka

import akka.dispatch.Mailbox
import com.newrelic.api.agent.NewRelic

case class MailboxMetrics(mailboxes:Map[String,Mailbox])


object  MailboxMetrics {
  def apply(mailboxes: List[Mailbox]) = {
    new MailboxMetrics(mailboxes.take(mailboxes.length - 1).map{m => (m.actor.self.path.toString -> m)}.toMap) //TODO:research why collect an ActorSystemImpl
  }

  def toMap(mb: Mailbox):Map[String,Int] = Map[String,Int](
    "NumberOfMessages" -> mb.numberOfMessages,
    "MailboxDispatcherThroughput" -> mb.dispatcher.throughput,
    "SuspendCount" -> mb.suspendCount
  )
}

class MailboxSenderMetrics(mailboxes:List[Mailbox]) extends Runnable {
  def run() {
    val mbm = MailboxMetrics(mailboxes)
    mbm.mailboxes.map { case(actorName,mb) => {
      println(s"Sending metrics to Newrelic MailBoxMonitor for Actor -> ${actorName}")

      MailboxMetrics.toMap(mb).map {case(property, value) =>
          NewRelic.recordMetric(s"${actorName}:Mailbox:${property}", value)
      }
    }
  }
 }
}