blob: abdae99fa9b57c9b643a904c9d41c9303203786c (
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
35
|
package kamon.system.sigar
import kamon.metric.GenericEntityRecorder
import kamon.metric.instrument._
import org.hyperic.sigar.{ NetInterfaceStat, Sigar }
import scala.util.Try
class NetworkMetrics(instrumentFactory: InstrumentFactory) extends GenericEntityRecorder(instrumentFactory) with SigarMetric {
val receivedBytes = DiffRecordingHistogram(histogram("rx-bytes", Memory.Bytes))
val transmittedBytes = DiffRecordingHistogram(histogram("tx-bytes", Memory.Bytes))
val receiveErrors = DiffRecordingHistogram(histogram("rx-errors"))
val transmitErrors = DiffRecordingHistogram(histogram("tx-errors"))
val receiveDrops = DiffRecordingHistogram(histogram("rx-dropped"))
val transmitDrops = DiffRecordingHistogram(histogram("tx-dropped"))
def sumOfAllInterfaces(sigar: Sigar, thunk: NetInterfaceStat ⇒ Long): Long = Try {
val interfaces = sigar.getNetInterfaceList.toList.filter(_ != "lo")
interfaces.map(i ⇒ thunk(sigar.getNetInterfaceStat(i))).fold(0L)(_ + _)
} getOrElse (0L)
def update(sigar: Sigar): Unit = {
receivedBytes.record(sumOfAllInterfaces(sigar, _.getRxBytes))
transmittedBytes.record(sumOfAllInterfaces(sigar, _.getTxBytes))
receiveErrors.record(sumOfAllInterfaces(sigar, _.getRxErrors))
transmitErrors.record(sumOfAllInterfaces(sigar, _.getTxErrors))
receiveDrops.record(sumOfAllInterfaces(sigar, _.getRxDropped))
transmitDrops.record(sumOfAllInterfaces(sigar, _.getTxDropped))
}
}
object NetworkMetrics extends SigarMetricRecorderCompanion("network") {
def apply(instrumentFactory: InstrumentFactory): NetworkMetrics =
new NetworkMetrics(instrumentFactory)
}
|