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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
package kamon.khronus
import akka.actor._
import akka.event.Logging
import com.despegar.khronus.jclient.KhronusClient
import kamon.Kamon
import kamon.metric.{ Entity, EntitySnapshot }
import kamon.metric.SubscriptionsDispatcher.TickMetricSnapshot
import kamon.metric.instrument.{ Counter, Histogram }
import scala.util.Try
object MetricReporter extends ExtensionId[MetricReporterExtension] with ExtensionIdProvider {
override def lookup(): ExtensionId[_ <: Extension] = MetricReporter
override def createExtension(system: ExtendedActorSystem): MetricReporterExtension = new MetricReporterExtension(system)
}
class MetricReporterExtension(system: ExtendedActorSystem) extends Kamon.Extension {
val log = Logging(system, classOf[MetricReporterExtension])
log.info("Starting the Kamon(Khronus) extension")
val subscriber = system.actorOf(Props[MetricReporterSubscriber], "kamon-khronus")
Kamon.metrics.subscribe("histogram", "**", subscriber, permanently = true)
Kamon.metrics.subscribe("counter", "**", subscriber, permanently = true)
Kamon.metrics.subscribe("gauge", "**", subscriber, permanently = true)
Kamon.metrics.subscribe("trace", "**", subscriber, permanently = true)
Kamon.metrics.subscribe("executor-service", "**", subscriber, permanently = true)
}
class MetricReporterSubscriber extends Actor with ActorLogging {
import context._
lazy val khronusClient: Try[KhronusClient] = {
val kc =
for {
config ← Try(Kamon.config.getConfig("kamon.khronus"))
host ← Try(config.getString("host"))
appName ← Try(config.getString("app-name"))
interval ← Try(config.getLong("interval"))
measures ← Try(config.getInt("max-measures"))
kc ← Try(new KhronusClient.Builder()
.withApplicationName(appName)
.withSendIntervalMillis(interval)
.withMaximumMeasures(measures)
.withHosts(host)
.build)
} yield kc
kc.failed.foreach(ex ⇒ log.error(s"Khronus metrics reporting inoperative: {}", ex))
kc
}
override def preStart() = khronusClient.foreach(_ ⇒ become(operative))
def receive = { case _ ⇒ }
val operative: Receive = {
case tick: TickMetricSnapshot ⇒ reportMetrics(tick)
}
def reportMetrics(tick: TickMetricSnapshot): Unit = {
// Group all the user metrics together.
val histograms = Map.newBuilder[String, Option[Histogram.Snapshot]]
val traces = Map.newBuilder[String, Option[Histogram.Snapshot]]
val counters = Map.newBuilder[String, Option[Counter.Snapshot]]
val gauges = Map.newBuilder[String, Option[Histogram.Snapshot]]
tick.metrics foreach {
case (entity, snapshot) if entity.category == "histogram" ⇒ histograms += (entity.name -> snapshot.histogram("histogram"))
case (entity, snapshot) if entity.category == "counter" ⇒ counters += (entity.name -> snapshot.counter("counter"))
case (entity, snapshot) if entity.category == "gauge" ⇒ gauges += (entity.name -> snapshot.gauge("gauge"))
case (entity, snapshot) if entity.category == "trace" ⇒ traces += (entity.name -> snapshot.histogram("elapsed-time"))
case (entity, snapshot) if entity.category == "executor-service" ⇒ pushExecutorMetrics(entity, snapshot)
case ignoreEverythingElse ⇒
}
pushToKhronus(histograms.result(), counters.result(), gauges.result(), traces.result())
}
def pushToKhronus(histograms: Map[String, Option[Histogram.Snapshot]],
counters: Map[String, Option[Counter.Snapshot]],
gauges: Map[String, Option[Histogram.Snapshot]],
traces: Map[String, Option[Histogram.Snapshot]]): Unit = {
counters.foreach {
case (name, Some(snapshot)) ⇒
pushCounter(name, snapshot)
case _ ⇒
}
gauges.foreach {
case (name, Some(snapshot)) ⇒
pushGauge(name, snapshot)
case _ ⇒
}
histograms.foreach {
case (name, Some(snapshot)) ⇒
pushSnapshot(name, snapshot)
case _ ⇒
}
traces.foreach {
case (name, Some(snapshot)) ⇒
pushSnapshot(name, snapshot)
case _ ⇒
}
}
def pushExecutorMetrics(entity: Entity, snapshot: EntitySnapshot): Unit = entity.tags.get("executor-type") match {
case Some("fork-join-pool") ⇒ pushForkJoinPoolMetrics(entity.name, snapshot)
case Some("thread-pool-executor") ⇒ pushThreadPoolExecutorMetrics(entity.name, snapshot)
case ignoreOthers ⇒
}
def pushForkJoinPoolMetrics(name: String, forkJoinMetrics: EntitySnapshot): Unit = {
for {
paralellism ← forkJoinMetrics.minMaxCounter("parallelism")
poolSize ← forkJoinMetrics.gauge("pool-size")
activeThreads ← forkJoinMetrics.gauge("active-threads")
runningThreads ← forkJoinMetrics.gauge("running-threads")
queuedTaskCount ← forkJoinMetrics.gauge("queued-task-count")
} {
pushSnapshot(s"$name.parallelism", paralellism)
pushSnapshot(s"$name.pool-size", poolSize)
pushSnapshot(s"$name.active-threads", activeThreads)
pushSnapshot(s"$name.running-threads", runningThreads)
pushSnapshot(s"$name.queued-task-count", queuedTaskCount)
}
}
def pushThreadPoolExecutorMetrics(name: String, threadPoolMetrics: EntitySnapshot): Unit = {
for {
corePoolSize ← threadPoolMetrics.gauge("core-pool-size")
maxPoolSize ← threadPoolMetrics.gauge("max-pool-size")
poolSize ← threadPoolMetrics.gauge("pool-size")
activeThreads ← threadPoolMetrics.gauge("active-threads")
processedTasks ← threadPoolMetrics.gauge("processed-tasks")
} {
pushSnapshot(s"$name.core-pool-size", corePoolSize)
pushSnapshot(s"$name.max-pool-size", maxPoolSize)
pushSnapshot(s"$name.pool-size", poolSize)
pushSnapshot(s"$name.active-threads", activeThreads)
pushSnapshot(s"$name.processed-tasks", processedTasks)
}
}
def pushSnapshot(name: String, snapshot: Histogram.Snapshot): Unit = {
khronusClient.foreach { kc ⇒
snapshot.recordsIterator.foreach { record ⇒
for (i ← 1L to record.count)
kc.recordTime(name, record.level)
}
}
}
def pushGauge(name: String, snapshot: Histogram.Snapshot): Unit = {
khronusClient.foreach { kc ⇒
snapshot.recordsIterator.foreach { record ⇒
for (i ← 1L to record.count)
kc.recordGauge(name, record.level)
}
}
}
def pushCounter(name: String, snapshot: Counter.Snapshot): Unit = {
khronusClient.foreach { kc ⇒
kc.recordGauge(name, snapshot.count)
}
}
}
|