Skip to content

Commit bcfc41e

Browse files
authored
Merge pull request #898 from SimunKaracic/akka-typed-instrumentation
Akka typed actor tag and auto-grouping changes
2 parents 2e7ac74 + e0879d4 commit bcfc41e

File tree

5 files changed

+38
-30
lines changed

5 files changed

+38
-30
lines changed

instrumentation/kamon-akka/src/common/scala/kamon/instrumentation/akka/AkkaMetrics.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kamon.instrumentation.akka
22

33
import kamon.Kamon
4+
import kamon.instrumentation.akka.instrumentations.ActorCellInfo
45
import kamon.metric.InstrumentGroup
56
import kamon.tag.TagSet
67

@@ -35,13 +36,14 @@ object AkkaMetrics {
3536
description = "Counts the number of processing errors experienced by an Actor"
3637
)
3738

38-
def forActor(path: String, system: String, dispatcher: String, actorClass: String): ActorInstruments =
39-
new ActorInstruments(TagSet.builder()
39+
def forActor(path: String, system: String, dispatcher: String, actorClass: Class[_]): ActorInstruments = {
40+
val tags = TagSet.builder()
4041
.add("path", path)
4142
.add("system", system)
4243
.add("dispatcher", dispatcher)
43-
.add("class", actorClass)
44-
.build())
44+
if (!ActorCellInfo.isTyped(actorClass)) tags.add("class", actorClass.getName)
45+
new ActorInstruments(tags.build())
46+
}
4547

4648
class ActorInstruments(tags: TagSet) extends InstrumentGroup(tags) {
4749
val timeInMailbox = register(ActorTimeInMailbox)
@@ -85,14 +87,16 @@ object AkkaMetrics {
8587
description = "Counts the number of processing errors experienced by the routees of a router"
8688
)
8789

88-
def forRouter(path: String, system: String, dispatcher: String, routerClass: String, routeeClass: String): RouterInstruments =
89-
new RouterInstruments(TagSet.builder()
90+
def forRouter(path: String, system: String, dispatcher: String, routerClass: Class[_], routeeClass: String): RouterInstruments = {
91+
val tags = TagSet.builder()
9092
.add("path", path)
9193
.add("system", system)
9294
.add("dispatcher", dispatcher)
93-
.add("routerClass", routerClass)
9495
.add("routeeClass", routeeClass)
95-
.build())
96+
if (!ActorCellInfo.isTyped(routerClass)) tags.add("routerClass", routerClass.getName)
97+
new RouterInstruments(tags.build())
98+
99+
}
96100

97101
class RouterInstruments(tags: TagSet) extends InstrumentGroup(tags) {
98102
val routingTime = register(RouterRoutingTime)

instrumentation/kamon-akka/src/common/scala/kamon/instrumentation/akka/instrumentations/ActorCellInfo.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ object ActorCellInfo {
8484
}}
8585
}
8686

87+
def isTyped(className: Class[_]): Boolean = {
88+
simpleClassName(className) == "ActorAdapter"
89+
}
90+
8791
private def hasRouterProps(props: Props): Boolean =
8892
props.deploy.routerConfig != NoRouter
8993

instrumentation/kamon-akka/src/common/scala/kamon/instrumentation/akka/instrumentations/ActorMonitor.scala

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ object ActorMonitor {
9696
val trackingGroups: Seq[ActorGroupInstruments] = if (cell.isRootSupervisor) List() else {
9797
val configuredMatchingGroups = AkkaInstrumentation.matchingActorGroups(cell.path)
9898

99-
if (configuredMatchingGroups.isEmpty && !isTracked && settings.autoGrouping && !cell.isRouter && !cell.isRoutee) {
99+
if (configuredMatchingGroups.isEmpty && !isTracked
100+
&& settings.autoGrouping && !cell.isRouter
101+
&& !cell.isRoutee && !ActorCellInfo.isTyped(cell.actorOrRouterClass)) {
100102
if (!trackedFilter.excludes(cell.path) && Kamon.filter(TrackAutoGroupFilterName).accept(autoGroupingPath))
101103
List(AkkaMetrics.forGroup(autoGroupingPath, system.name))
102104
else
@@ -130,7 +132,7 @@ object ActorMonitor {
130132
cellInfo.path,
131133
cellInfo.systemName,
132134
cellInfo.dispatcherName,
133-
cellInfo.actorOrRouterClass.getName
135+
cellInfo.actorOrRouterClass
134136
))
135137
}
136138

@@ -149,8 +151,8 @@ object ActorMonitor {
149151
cellInfo.path,
150152
cellInfo.systemName,
151153
cellInfo.dispatcherName,
152-
cellInfo.actorOrRouterClass.getName,
153-
cellInfo.routeeClass.map(_.getName).getOrElse("Unknown")
154+
cellInfo.actorOrRouterClass,
155+
cellInfo.routeeClass.filterNot(ActorCellInfo.isTyped).map(_.getName).getOrElse("Unknown")
154156
)
155157

156158
new TrackedRoutee(routerMetrics, groupMetrics, cellInfo)
@@ -177,8 +179,6 @@ object ActorMonitor {
177179
* Wraps another ActorMonitor implementation and provides tracing capabilities on top of it.
178180
*/
179181
class TracedMonitor(cellInfo: ActorCellInfo, startsTrace: Boolean, monitor: ActorMonitor) extends ActorMonitor {
180-
private val _actorClassName = cellInfo.actorOrRouterClass.getName
181-
private val _actorSimpleClassName = ActorCellInfo.simpleClassName(cellInfo.actorOrRouterClass)
182182

183183
override def captureEnvelopeTimestamp(): Long =
184184
monitor.captureEnvelopeTimestamp()
@@ -225,23 +225,24 @@ object ActorMonitor {
225225
val messageClass = ActorCellInfo.simpleClassName(envelope.message.getClass)
226226
val parentSpan = context.get(Span.Key)
227227

228-
Kamon.internalSpanBuilder(operationName(messageClass, envelope.sender), "akka.actor")
228+
val spanBuilder = Kamon.internalSpanBuilder(operationName(messageClass, envelope.sender), "akka.actor")
229229
.asChildOf(parentSpan)
230230
.doNotTrackMetrics()
231231
.tag("akka.system", cellInfo.systemName)
232232
.tag("akka.actor.path", cellInfo.path)
233-
.tag("akka.actor.class", _actorClassName)
234233
.tag("akka.actor.message-class", messageClass)
235-
.delay(Kamon.clock().toInstant(envelopeTimestamp))
234+
if (!ActorCellInfo.isTyped(cellInfo.actorOrRouterClass)) {
235+
spanBuilder.tag("akka.actor.class", cellInfo.actorOrRouterClass.getName)
236+
}
237+
spanBuilder.delay(Kamon.clock().toInstant(envelopeTimestamp))
236238
}
237239

238240
private def operationName(messageClass: String, sender: ActorRef): String = {
239-
val operationType = if(AkkaPrivateAccess.isPromiseActorRef(sender)) "ask(" else "tell("
241+
val operationType = if(AkkaPrivateAccess.isPromiseActorRef(sender)) "ask" else "tell"
240242

241243
StringBuilder.newBuilder
242244
.append(operationType)
243-
.append(_actorSimpleClassName)
244-
.append(", ")
245+
.append("(")
245246
.append(messageClass)
246247
.append(")")
247248
.result()

instrumentation/kamon-akka/src/common/scala/kamon/instrumentation/akka/instrumentations/RouterMonitor.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ object RouterMonitor {
5454
cell.path,
5555
cell.systemName,
5656
cell.dispatcherName,
57-
cell.actorOrRouterClass.getName,
58-
cell.routeeClass.map(_.getName).getOrElse("Unknown")
57+
cell.actorOrRouterClass,
58+
cell.routeeClass.filterNot(ActorCellInfo.isTyped).map(_.getName).getOrElse("Unknown")
5959
)
6060
)
6161
else NoOpRouterMonitor

instrumentation/kamon-akka/src/test-common/scala/kamon/instrumentation/akka/MessageTracingSpec.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class MessageTracingSpec extends TestKit(ActorSystem("MessageTracing")) with Wor
3434
val span = testSpanReporter.nextSpan().value
3535
val spanTags = stringTag(span) _
3636
spanTags("component") shouldBe "akka.actor"
37-
span.operationName shouldBe("tell(TracingTestActor, String)")
37+
span.operationName shouldBe("tell(String)")
3838
spanTags("akka.actor.path") shouldNot include ("filteredout")
3939
spanTags("akka.actor.path") should be ("MessageTracing/user/traced-probe-1")
4040
}
@@ -48,7 +48,7 @@ class MessageTracingSpec extends TestKit(ActorSystem("MessageTracing")) with Wor
4848
eventually(timeout(2 seconds)) {
4949
val span = testSpanReporter.nextSpan().value
5050
val spanTags = stringTag(span) _
51-
span.operationName shouldBe("tell(TracingTestActor, String)")
51+
span.operationName shouldBe("tell(String)")
5252
spanTags("component") shouldBe "akka.actor"
5353
spanTags("akka.system") shouldBe "MessageTracing"
5454
spanTags("akka.actor.path") shouldBe "MessageTracing/user/traced"
@@ -62,7 +62,7 @@ class MessageTracingSpec extends TestKit(ActorSystem("MessageTracing")) with Wor
6262
eventually(timeout(2 seconds)) {
6363
val span = testSpanReporter.nextSpan().value
6464
val spanTags = stringTag(span) _
65-
span.operationName shouldBe("ask(TracingTestActor, String)")
65+
span.operationName shouldBe("ask(String)")
6666
spanTags("component") shouldBe "akka.actor"
6767
spanTags("akka.system") shouldBe "MessageTracing"
6868
spanTags("akka.actor.path") shouldBe "MessageTracing/user/traced"
@@ -83,7 +83,6 @@ class MessageTracingSpec extends TestKit(ActorSystem("MessageTracing")) with Wor
8383
val span = testSpanReporter.nextSpan().value
8484
val spanTags = stringTag(span) _
8585

86-
span.operationName should include("tell(TracingTestActor")
8786
spanTags("component") shouldBe "akka.actor"
8887
spanTags("akka.system") shouldBe "MessageTracing"
8988
spanTags("akka.actor.path") shouldBe "MessageTracing/user/traced-first"
@@ -97,7 +96,7 @@ class MessageTracingSpec extends TestKit(ActorSystem("MessageTracing")) with Wor
9796
val span = testSpanReporter.nextSpan().value
9897
val spanTags = stringTag(span) _
9998
span.parentId shouldBe firstSpanID
100-
span.operationName should include("tell(TracingTestActor, String)")
99+
span.operationName should include("tell(String)")
101100
spanTags("component") shouldBe "akka.actor"
102101
spanTags("akka.system") shouldBe "MessageTracing"
103102
spanTags("akka.actor.path") shouldBe "MessageTracing/user/traced-second"
@@ -118,7 +117,7 @@ class MessageTracingSpec extends TestKit(ActorSystem("MessageTracing")) with Wor
118117
val firstSpanID = eventually(timeout(2 seconds)) {
119118
val span = testSpanReporter.nextSpan().value
120119
val spanTags = stringTag(span) _
121-
span.operationName shouldBe("tell(TracingTestActor, Tuple2)")
120+
span.operationName shouldBe("tell(Tuple2)")
122121
spanTags("component") shouldBe "akka.actor"
123122
spanTags("akka.system") shouldBe "MessageTracing"
124123
spanTags("akka.actor.path") shouldBe "MessageTracing/user/traced-chain-first"
@@ -133,7 +132,7 @@ class MessageTracingSpec extends TestKit(ActorSystem("MessageTracing")) with Wor
133132
val span = testSpanReporter.nextSpan().value
134133
val spanTags = stringTag(span) _
135134
span.parentId shouldBe firstSpanID
136-
span.operationName shouldBe("tell(TracingTestActor, String)")
135+
span.operationName shouldBe("tell(String)")
137136
spanTags("component") shouldBe "akka.actor"
138137
spanTags("akka.system") shouldBe "MessageTracing"
139138
spanTags("akka.actor.path") shouldBe "MessageTracing/user/traced-chain-last"
@@ -199,4 +198,4 @@ class TracingTestActor extends Actor {
199198
Thread.sleep(50)
200199
sender ! "pong"
201200
}
202-
}
201+
}

0 commit comments

Comments
 (0)