From 71b2b74b971e696e9034ed55906f8cd648fa2368 Mon Sep 17 00:00:00 2001 From: Hugh Simpson Date: Thu, 9 Nov 2023 11:07:50 +0000 Subject: [PATCH] oh, right. Figured out why ActorMonitorInstrumentation was throwing in scala 3. Patch it back in. --- .../akka_26/ActorMonitorInstrumentation.scala | 15 ++++--- .../src/common/resources/reference.conf | 1 + .../src/main/resources/reference.conf | 1 + .../ActorMonitorInstrumentation.scala | 44 +++++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 instrumentation/kamon-pekko/src/main/scala/kamon/instrumentation/pekko/instrumentations/ActorMonitorInstrumentation.scala diff --git a/instrumentation/kamon-akka/src/akka-2.6/scala/kamon/instrumentation/akka/instrumentations/akka_26/ActorMonitorInstrumentation.scala b/instrumentation/kamon-akka/src/akka-2.6/scala/kamon/instrumentation/akka/instrumentations/akka_26/ActorMonitorInstrumentation.scala index 44eb9b696..aa5529412 100644 --- a/instrumentation/kamon-akka/src/akka-2.6/scala/kamon/instrumentation/akka/instrumentations/akka_26/ActorMonitorInstrumentation.scala +++ b/instrumentation/kamon-akka/src/akka-2.6/scala/kamon/instrumentation/akka/instrumentations/akka_26/ActorMonitorInstrumentation.scala @@ -28,20 +28,21 @@ class MessageClassAdvice object MessageClassAdvice { private val logger = LoggerFactory.getLogger(classOf[MessageClassAdvice]) - @static def extractMessageClass(@Argument(0) envelope: Envelope): String = { + @static def extractMessageClass(@Argument(0) envelope: Any): String = { + val e = envelope.asInstanceOf[Envelope] try { - envelope.message match { + e.message match { case message: WrappedMessage => ActorCellInfo.simpleClassName(message.message.getClass) - case _ => ActorCellInfo.simpleClassName(envelope.message.getClass) + case _ => ActorCellInfo.simpleClassName(e.message.getClass) } } catch { // NoClassDefFound is thrown in early versions of akka 2.6 // so we can safely fallback to the original method case _: NoClassDefFoundError => - ActorCellInfo.simpleClassName(envelope.message.getClass) - case NonFatal(e) => - logger.info(s"Expected NoClassDefFoundError, got: ${e}") - ActorCellInfo.simpleClassName(envelope.message.getClass) + ActorCellInfo.simpleClassName(e.message.getClass) + case NonFatal(ex) => + logger.info(s"Expected NoClassDefFoundError, got: ${ex}") + ActorCellInfo.simpleClassName(e.message.getClass) } } } diff --git a/instrumentation/kamon-akka/src/common/resources/reference.conf b/instrumentation/kamon-akka/src/common/resources/reference.conf index de5ff51f1..be3029c48 100644 --- a/instrumentation/kamon-akka/src/common/resources/reference.conf +++ b/instrumentation/kamon-akka/src/common/resources/reference.conf @@ -154,6 +154,7 @@ kanela.modules { "kamon.instrumentation.akka.instrumentations.ActorRefInstrumentation", "kamon.instrumentation.akka.instrumentations.akka_25.DispatcherInstrumentation", "kamon.instrumentation.akka.instrumentations.akka_26.DispatcherInstrumentation", + "kamon.instrumentation.akka.instrumentations.akka_26.ActorMonitorInstrumentation", "kamon.instrumentation.akka.instrumentations.SchedulerInstrumentation", "kamon.instrumentation.akka.instrumentations.ClusterInstrumentation" ] diff --git a/instrumentation/kamon-pekko/src/main/resources/reference.conf b/instrumentation/kamon-pekko/src/main/resources/reference.conf index b93ac7971..283119e6c 100644 --- a/instrumentation/kamon-pekko/src/main/resources/reference.conf +++ b/instrumentation/kamon-pekko/src/main/resources/reference.conf @@ -153,6 +153,7 @@ kanela.modules { "kamon.instrumentation.pekko.instrumentations.EventStreamInstrumentation", "kamon.instrumentation.pekko.instrumentations.ActorRefInstrumentation", "kamon.instrumentation.pekko.instrumentations.DispatcherInstrumentation", + "kamon.instrumentation.pekko.instrumentations.ActorMonitorInstrumentation", "kamon.instrumentation.pekko.instrumentations.SchedulerInstrumentation", "kamon.instrumentation.pekko.instrumentations.ClusterInstrumentation" ] diff --git a/instrumentation/kamon-pekko/src/main/scala/kamon/instrumentation/pekko/instrumentations/ActorMonitorInstrumentation.scala b/instrumentation/kamon-pekko/src/main/scala/kamon/instrumentation/pekko/instrumentations/ActorMonitorInstrumentation.scala new file mode 100644 index 000000000..d90bb5c62 --- /dev/null +++ b/instrumentation/kamon-pekko/src/main/scala/kamon/instrumentation/pekko/instrumentations/ActorMonitorInstrumentation.scala @@ -0,0 +1,44 @@ +package kamon.instrumentation.pekko.instrumentations + +import org.apache.pekko.actor.WrappedMessage +import org.apache.pekko.dispatch.Envelope +import kamon.instrumentation.pekko.instrumentations.ActorCellInfo +import kanela.agent.api.instrumentation.InstrumentationBuilder +import kanela.agent.libs.net.bytebuddy.implementation.bind.annotation.Argument +import org.slf4j.LoggerFactory + +import scala.annotation.static +import scala.util.control.NonFatal + +class ActorMonitorInstrumentation extends InstrumentationBuilder { + /* + * Changes implementation of extractMessageClass for our ActorMonitor. + * In Pekko, all typed messages are converted to AdaptMessage, + * so we're forced to extract the original message type. + */ + onSubTypesOf("kamon.instrumentation.pekko.instrumentations.ActorMonitor") + .intercept(method("extractMessageClass"), classOf[MessageClassAdvice]) +} + +class MessageClassAdvice +object MessageClassAdvice { + private val logger = LoggerFactory.getLogger(classOf[MessageClassAdvice]) + + @static def extractMessageClass(@Argument(0) envelope: Any): String = { + val e = envelope.asInstanceOf[Envelope] + try { + e.message match { + case message: WrappedMessage => ActorCellInfo.simpleClassName(message.message.getClass) + case _ => ActorCellInfo.simpleClassName(e.message.getClass) + } + } catch { + // NoClassDefFound is thrown in early versions of akka 2.6 + // so we can safely fallback to the original method + case _: NoClassDefFoundError | _: ClassCastException | _: NullPointerException => + ActorCellInfo.simpleClassName(e.message.getClass) + case NonFatal(ex) => + logger.info(s"Expected NoClassDefFoundError, got: ${ex}") + ActorCellInfo.simpleClassName(e.message.getClass) + } + } +}