Message in wait queue - can we log the time it is in the queue?

Hi everyone,

Our messages are long processing messages and we want to be able to track where the message is spending time in. As such, we want to be able to log how long a message is in the queue also. Is there way to do it? Currently, I am tracking the queue size with this wrapping class. The message object I am sending has a transaction id. One way I was thinking is that perhaps I can log the message’s transaction id as I enqueue and dequeue. However, the parameter of the enqueue is an Envelope, not my object. Could you advise?

Thanks in advance for your suggestion,

class LoggingMailbox(owner: ActorRef, system: ActorSystem, sizeLimit: Int)
extends UnboundedMailbox.MessageQueue {

private lazy val log = Logging(system, classOf[LoggingMailbox])
private val path = owner.path.toString
private val queueSize = new AtomicInteger
private val dequeueOperation = “dequeue”
private val enqueueOperation = “enqueue”

override def dequeue(): Envelope = {
val x = super.dequeue()
if (x ne null) {
val size = queueSize.decrementAndGet()
logSize(size, dequeueOperation)

override def enqueue(receiver: ActorRef, handle: Envelope): Unit = {
super.enqueue(receiver, handle)
val size = queueSize.incrementAndGet()
logSize(size, enqueueOperation)


It will probably be too much overhead to log for each message. I have a similar gist:

For systems in production I’d recommend Lightbend Telemetry (Cinnamon).

Thanks Patrik. Yes, I got the code from your github and modify a little. Our jobs are long running jobs so it might be okay to log each message… i think.