Skip to content

Commit

Permalink
oh, right. Figured out why ActorMonitorInstrumentation was throwing i…
Browse files Browse the repository at this point in the history
…n scala 3. Patch it back in.
  • Loading branch information
hughsimpson committed Nov 9, 2023
1 parent 83ff191 commit 71b2b74
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 71b2b74

Please sign in to comment.