vendredi 14 janvier 2022

[Akka]: Spawn an Actor at runtime with Scala Reflection and monitor its behavior using Grafana and Prometheus

I set up Grafana and Prometheus in Akka to monitor the behaviour of my system. If I spawn Actors at compile time it works and I can see them on the dashboard.

Now I'd like to compile an Actor at runtime and monitor its behaviour. In order to achieve that, I run

  val toolbox = currentMirror.mkToolBox()
  // Class instance
  val actorCode = q"""
  import akka.actor._
  object HelloActor {
   def props(name : String) = Props(new HelloActor(name))
  }
class HelloActor(myName: String) extends Actor {
  def receive = {
    case "hello" => println("hello from %s".format(myName))
    case _       => println("'huh?', said %s".format(myName))
  }
}
  return HelloActor.props("Jane")
"""

Then I compile the Actor, I get the Props and send a message to it in this way

  val compiledCode = toolbox.compile(actorCode)()
  val actorSystem = ActorSystem("firstActorSystem")
  val myProps = compiledCode.asInstanceOf[Props]
  val helloActor = actorSystem.actorOf(myProps)
  helloActor ! "hello"

Everything works fine, but if I go to the Prometheus dashboard I can not see the Actor instance and the messages that have been sent.

Any tips to solve this issue ?





Aucun commentaire:

Enregistrer un commentaire