aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/akka/PoolMonitorAspect.scala
blob: 36861a93d72a267e321f5a6c3bfaf4dd072f302d (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
36
package akka

import org.aspectj.lang.annotation.{Pointcut, Before, Aspect}
import scala.concurrent.duration._
import com.newrelic.api.agent.NewRelic

@Aspect("perthis(poolMonitor())")
class PoolMonitorAspect extends ActorSystem {

  @Pointcut("execution(scala.concurrent.forkjoin.ForkJoinPool.new(..)) && !within(PoolMonitorAspect)")
  protected def poolMonitor:Unit = {}

  @Before("poolMonitor() && this(pool)")
  def beforePoolInstantiation(pool: scala.concurrent.forkjoin.ForkJoinPool) {
    actorSystem.scheduler.schedule(10 seconds, 6 second, new Runnable {
      def run() {
        val poolName = pool.getClass.getSimpleName

        println(s"Sending metrics to Newrelic PoolMonitor -> ${poolName}")
        PoolConverter.toMap(pool).map{case(k,v) => NewRelic.recordMetric(s"${poolName}:${k}",v)}
        }
      })
    }
}

object PoolConverter {
  def toMap(pool: scala.concurrent.forkjoin.ForkJoinPool):Map[String,Int] = Map[String,Int](
    "ActiveThreadCount" -> pool.getActiveThreadCount,
    "Parallelism" -> pool.getParallelism,
    "PoolSize" -> pool.getPoolSize,
    "QueuedSubmissionCount" -> pool.getQueuedSubmissionCount,
    "StealCount" -> pool.getStealCount.toInt,
    "QueuedTaskCount" -> pool.getQueuedTaskCount.toInt,
    "RunningThreadCount" -> pool.getRunningThreadCount
  )
}