diff options
Diffstat (limited to 'kamon-system-metrics/src/main/scala/kamon/system/sigar/CpuMetrics.scala')
-rw-r--r-- | kamon-system-metrics/src/main/scala/kamon/system/sigar/CpuMetrics.scala | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/kamon-system-metrics/src/main/scala/kamon/system/sigar/CpuMetrics.scala b/kamon-system-metrics/src/main/scala/kamon/system/sigar/CpuMetrics.scala index 0e9a5b53..54b95ab7 100644 --- a/kamon-system-metrics/src/main/scala/kamon/system/sigar/CpuMetrics.scala +++ b/kamon-system-metrics/src/main/scala/kamon/system/sigar/CpuMetrics.scala @@ -16,6 +16,7 @@ package kamon.system.sigar +import akka.event.LoggingAdapter import kamon.metric.GenericEntityRecorder import kamon.metric.instrument.InstrumentFactory import org.hyperic.sigar.Sigar @@ -28,7 +29,7 @@ import org.hyperic.sigar.Sigar * - idle: Total percentage of system cpu idle time * - stolen: Total percentage of system cpu involuntary wait time. @see [[https://www.datadoghq.com/2013/08/understanding-aws-stolen-cpu-and-how-it-affects-your-apps/ "Understanding Stolen Cpu"]] */ -class CpuMetrics(sigar: Sigar, instrumentFactory: InstrumentFactory) extends GenericEntityRecorder(instrumentFactory) with SigarMetric { +class CpuMetrics(sigar: Sigar, instrumentFactory: InstrumentFactory, logger: LoggingAdapter) extends GenericEntityRecorder(instrumentFactory) with SigarMetric { val user = histogram("cpu-user") val system = histogram("cpu-system") val Wait = histogram("cpu-wait") @@ -36,18 +37,29 @@ class CpuMetrics(sigar: Sigar, instrumentFactory: InstrumentFactory) extends Gen val stolen = histogram("cpu-stolen") def update(): Unit = { - val cpuPerc = sigar.getCpuPerc + import SigarSafeRunner._ - user.record((cpuPerc.getUser * 100L).toLong) - system.record((cpuPerc.getSys * 100L).toLong) - Wait.record((cpuPerc.getWait * 100L).toLong) - idle.record((cpuPerc.getIdle * 100L).toLong) - stolen.record((cpuPerc.getStolen * 100L).toLong) + def cpuPerc = { + val cpuPerc = sigar.getCpuPerc + ((cpuPerc.getUser * 100L).toLong, + (cpuPerc.getSys * 100L).toLong, + (cpuPerc.getWait * 100L).toLong, + (cpuPerc.getIdle * 100L).toLong, + (cpuPerc.getStolen * 100L).toLong) + } + + val (cpuUser, cpuSys, cpuWait, cpuIdle, cpuStolen) = runSafe(cpuPerc, (0L, 0L, 0L, 0L, 0L), "cpu", logger) + + user.record(cpuUser) + system.record(cpuSys) + Wait.record(cpuWait) + idle.record(cpuIdle) + stolen.record(cpuStolen) } } object CpuMetrics extends SigarMetricRecorderCompanion("cpu") { - def apply(sigar: Sigar, instrumentFactory: InstrumentFactory): CpuMetrics = - new CpuMetrics(sigar, instrumentFactory) + def apply(sigar: Sigar, instrumentFactory: InstrumentFactory, logger: LoggingAdapter): CpuMetrics = + new CpuMetrics(sigar, instrumentFactory, logger) } |