aboutsummaryrefslogtreecommitdiff
path: root/kamon-core/src/main/scala/kamon/instrumentation/MessageQueueMetrics.scala
blob: da797fa1eb7b0435392c8ece55319a12f7a2f526 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* ===================================================
 * Copyright © 2013 the kamon project <http://kamon.io/>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== */
package kamon.instrumentation

import com.codahale.metrics.{ ExponentiallyDecayingReservoir, Histogram }
import akka.dispatch.{ UnboundedMessageQueueSemantics, Envelope, MessageQueue }
import org.aspectj.lang.annotation.{ Around, Pointcut, DeclareMixin, Aspect }
import akka.actor.{ ActorSystem, ActorRef }
import kamon.metric.{ Metrics, MetricDirectory }
import org.aspectj.lang.ProceedingJoinPoint

/**
 *  For Mailboxes we would like to track the queue size and message latency. Currently the latency
 *  will be gathered from the ActorCellMetrics.
 */

@Aspect
class MessageQueueInstrumentation {

  @Pointcut("execution(* akka.dispatch.MailboxType+.create(..)) && args(owner, system)")
  def messageQueueCreation(owner: Option[ActorRef], system: Option[ActorSystem]) = {}

  @Around("messageQueueCreation(owner, system)")
  def wrapMessageQueue(pjp: ProceedingJoinPoint, owner: Option[ActorRef], system: Option[ActorSystem]): MessageQueue = {
    val delegate = pjp.proceed.asInstanceOf[MessageQueue]

    // We are not interested in monitoring mailboxes if we don't know where they belong to.
    val monitoredMailbox = for (own  owner; sys  system) yield {
      val systemName = sys.name
      val ownerName = MetricDirectory.nameForActor(own)
      val mailBoxName = MetricDirectory.nameForMailbox(systemName, ownerName)

      val queueSizeHistogram = new Histogram(new ExponentiallyDecayingReservoir())
      Metrics.include(mailBoxName, queueSizeHistogram)

      new MonitoredMessageQueue(delegate, queueSizeHistogram)
    }

    monitoredMailbox match {
      case None       delegate
      case Some(mmb)  mmb
    }
  }
}

class MonitoredMessageQueue(val delegate: MessageQueue, val queueSizeHistogram: Histogram) extends MessageQueue with UnboundedMessageQueueSemantics {

  def enqueue(receiver: ActorRef, handle: Envelope) = {
    delegate.enqueue(receiver, handle)
    //queueSizeHistogram.update(numberOfMessages)
  }

  def dequeue(): Envelope = {
    val envelope = delegate.dequeue()
    //queueSizeHistogram.update(numberOfMessages)

    envelope
  }

  def numberOfMessages: Int = delegate.numberOfMessages
  def hasMessages: Boolean = delegate.hasMessages
  def cleanUp(owner: ActorRef, deadLetters: MessageQueue) = delegate.cleanUp(owner, deadLetters)
}