blob: b7d2fe6a08cbcb8a8ac4a1f3a93f5af7b50bc80c (
plain) (
blame)
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
|
package kamon.system.jmx
import java.lang.management.{ GarbageCollectorMXBean, ManagementFactory }
import kamon.metric.{ Entity, MetricsExtension, GenericEntityRecorder }
import kamon.metric.instrument.{ DifferentialValueCollector, Time, InstrumentFactory }
import scala.collection.JavaConverters._
class GarbageCollectionMetrics(gc: GarbageCollectorMXBean, instrumentFactory: InstrumentFactory) extends GenericEntityRecorder(instrumentFactory) {
gauge("garbage-collection-count", DifferentialValueCollector(() ⇒ {
gc.getCollectionCount
}))
gauge("garbage-collection-time", Time.Milliseconds, DifferentialValueCollector(() ⇒ {
gc.getCollectionTime
}))
}
object GarbageCollectionMetrics {
def sanitizeCollectorName(name: String): String =
name.replaceAll("""[^\w]""", "-").toLowerCase
def register(metricsExtension: MetricsExtension): Unit = {
val instrumentFactory = metricsExtension.instrumentFactory("system-metric")
ManagementFactory.getGarbageCollectorMXBeans.asScala.filter(_.isValid) map { gc ⇒
val gcName = sanitizeCollectorName(gc.getName)
metricsExtension.register(Entity(s"$gcName-garbage-collector", "system-metric"), new GarbageCollectionMetrics(gc, instrumentFactory))
}
}
}
|