-
Notifications
You must be signed in to change notification settings - Fork 325
/
ActorInstrumentation.scala
147 lines (117 loc) · 5.35 KB
/
ActorInstrumentation.scala
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* =========================================================================================
* Copyright © 2013-2018 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.pekko.instrumentations
import org.apache.pekko.actor.{ActorRef, ActorSystem}
import kamon.Kamon
import kamon.context.Storage.Scope
import kamon.instrumentation.pekko.instrumentations.HasActorMonitor.actorMonitor
import kamon.instrumentation.context.{HasContext, HasTimestamp}
import kanela.agent.api.instrumentation.InstrumentationBuilder
import kanela.agent.libs.net.bytebuddy.asm.Advice
import kanela.agent.libs.net.bytebuddy.asm.Advice.{Argument, OnMethodEnter, OnMethodExit, This}
import org.apache.pekko.actor.instrumentation.ReplaceWithAdvice
class ActorInstrumentation extends InstrumentationBuilder {
/**
* This is where most of the Actor processing magic happens. Handling of messages, errors and system messages.
*/
onType("org.apache.pekko.actor.ActorCell")
.mixin(classOf[HasActorMonitor.Mixin])
.advise(isConstructor, ActorCellConstructorAdvice)
.advise(method("invoke"), classOf[ActorCellInvokeAdvice])
.advise(method("handleInvokeFailure"), HandleInvokeFailureMethodAdvice)
.advise(method("sendMessage").and(takesArguments(1)), SendMessageAdvice)
.advise(method("terminate"), TerminateMethodAdvice)
.advise(method("swapMailbox"), ActorCellSwapMailboxAdvice)
.advise(method("invokeAll$1"), InvokeAllMethodInterceptor)
/**
* Ensures that the Context is properly propagated when messages are temporarily stored on an UnstartedCell.
*/
onType("org.apache.pekko.actor.UnstartedCell")
.mixin(classOf[HasActorMonitor.Mixin])
.advise(isConstructor, RepointableActorCellConstructorAdvice)
.advise(method("sendMessage").and(takesArguments(1)), SendMessageAdvice)
.advise(method("replaceWith"), classOf[ReplaceWithAdvice])
}
trait HasActorMonitor {
def actorMonitor: ActorMonitor
def setActorMonitor(actorMonitor: ActorMonitor): Unit
}
object HasActorMonitor {
class Mixin(var actorMonitor: ActorMonitor) extends HasActorMonitor {
override def setActorMonitor(actorMonitor: ActorMonitor): Unit =
this.actorMonitor = actorMonitor
}
def actorMonitor(cell: Any): ActorMonitor =
cell.asInstanceOf[HasActorMonitor].actorMonitor
}
object ActorCellSwapMailboxAdvice {
@Advice.OnMethodEnter
def enter(@Advice.This cell: Any, @Advice.Argument(0) newMailbox: Any): Boolean = {
val isShuttingDown = PekkoPrivateAccess.isDeadLettersMailbox(cell, newMailbox)
if(isShuttingDown)
actorMonitor(cell).onTerminationStart()
isShuttingDown
}
@Advice.OnMethodExit
def exit(@Advice.This cell: Any, @Advice.Return oldMailbox: Any, @Advice.Enter isShuttingDown: Boolean): Unit = {
if(oldMailbox != null && isShuttingDown) {
actorMonitor(cell).onDroppedMessages(PekkoPrivateAccess.mailboxMessageCount(oldMailbox))
}
}
}
object InvokeAllMethodInterceptor {
@Advice.OnMethodEnter
def enter(@Advice.Argument(0) message: Any): Option[Scope] =
message match {
case m: HasContext => Some(Kamon.storeContext(m.context))
case _ => None
}
@Advice.OnMethodExit
def exit(@Advice.Enter scope: Option[Scope]): Unit =
scope.foreach(_.close())
}
object SendMessageAdvice {
@OnMethodEnter(suppress = classOf[Throwable])
def onEnter(@This cell: Any, @Argument(0) envelope: Object): Unit = {
val instrumentation = actorMonitor(cell)
envelope.asInstanceOf[HasContext].setContext(instrumentation.captureEnvelopeContext())
envelope.asInstanceOf[HasTimestamp].setTimestamp(instrumentation.captureEnvelopeTimestamp())
}
}
object RepointableActorCellConstructorAdvice {
@Advice.OnMethodExit(suppress = classOf[Throwable])
def onExit(@This cell: Any, @Argument(0) system: ActorSystem, @Argument(1) ref: ActorRef, @Argument(3) parent: ActorRef): Unit =
cell.asInstanceOf[HasActorMonitor].setActorMonitor(ActorMonitor.from(cell, ref, parent, system))
}
object ActorCellConstructorAdvice {
@OnMethodExit(suppress = classOf[Throwable])
def onExit(@This cell: Any, @Argument(0) system: ActorSystem, @Argument(1) ref: ActorRef, @Argument(4) parent: ActorRef): Unit =
cell.asInstanceOf[HasActorMonitor].setActorMonitor(ActorMonitor.from(cell, ref, parent, system))
}
object HandleInvokeFailureMethodAdvice {
@OnMethodEnter(suppress = classOf[Throwable])
def onEnter(@This cell: Any, @Argument(1) failure: Throwable): Unit =
actorMonitor(cell).onFailure(failure)
}
object TerminateMethodAdvice {
@OnMethodEnter(suppress = classOf[Throwable])
def onEnter(@This cell: Any): Unit = {
actorMonitor(cell).cleanup()
if (PekkoPrivateAccess.isRoutedActorCell(cell)) {
cell.asInstanceOf[HasRouterMonitor].routerMonitor.cleanup()
}
}
}