-
Notifications
You must be signed in to change notification settings - Fork 0
/
MetricsClusterListener.kt
56 lines (47 loc) · 1.78 KB
/
MetricsClusterListener.kt
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
package pl.beone.promena.cluster.listener
import akka.actor.AbstractActor
import akka.cluster.Cluster
import akka.cluster.ClusterEvent.CurrentClusterState
import akka.cluster.metrics.ClusterMetricsChanged
import akka.cluster.metrics.ClusterMetricsExtension
import akka.cluster.metrics.NodeMetrics
import akka.cluster.metrics.StandardMetrics
import akka.event.Logging
class MetricsClusterListener : AbstractActor() {
private var log = Logging.getLogger(context.system(), this)
private var cluster = Cluster.get(context.system())
private val extension = ClusterMetricsExtension.get(context.system)
override fun preStart() {
extension.subscribe(self)
}
override fun postStop() {
extension.unsubscribe(self)
}
override fun createReceive(): Receive {
return receiveBuilder()
.match(ClusterMetricsChanged::class.java) {
for (nodeMetrics in it.nodeMetrics) {
if (nodeMetrics.address() == cluster.selfAddress()) {
logHeap(nodeMetrics)
logCpu(nodeMetrics)
}
}
}
.match(CurrentClusterState::class.java) {
// ignore
}
.build()
}
private fun logHeap(nodeMetrics: NodeMetrics) {
val heap = StandardMetrics.extractHeapMemory(nodeMetrics)
if (heap != null) {
log.debug("Used heap: {} MB", heap.used().toDouble() / 1024.0 / 1024.0)
}
}
private fun logCpu(nodeMetrics: NodeMetrics) {
val cpu = StandardMetrics.extractCpu(nodeMetrics)
if (cpu != null && cpu.systemLoadAverage().isDefined) {
log.debug("Load: {} ({} processors)", cpu.systemLoadAverage().get(), cpu.processors())
}
}
}