add percent helper method to metrics

This commit is contained in:
Bradley Cicenas 2017-06-10 13:00:54 +00:00
parent b49e174483
commit 389dee0f3c
5 changed files with 20 additions and 11 deletions

View File

@ -86,7 +86,7 @@ func (c *Docker) ReadCPU(stats *api.Stats) {
func (c *Docker) ReadMem(stats *api.Stats) {
c.MemUsage = int64(stats.MemoryStats.Usage)
c.MemLimit = int64(stats.MemoryStats.Limit)
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
c.MemPercent = percent(float64(c.MemUsage), float64(c.MemLimit))
}
func (c *Docker) ReadNet(stats *api.Stats) {

View File

@ -22,14 +22,14 @@ type Metrics struct {
func NewMetrics() Metrics {
return Metrics{
CPUUtil: -1,
NetTx: -1,
NetRx: -1,
MemUsage: -1,
MemPercent: -1,
IOBytesRead: -1,
IOBytesWrite: -1,
Pids: -1,
CPUUtil: -1,
NetTx: -1,
NetRx: -1,
MemUsage: -1,
MemPercent: -1,
IOBytesRead: -1,
IOBytesWrite: -1,
Pids: -1,
}
}
@ -43,3 +43,11 @@ type Collector interface {
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
// return rounded percentage
func percent(val float64, total float64) int {
if total <= 0 {
return 0
}
return round((val / total) * 100)
}

View File

@ -60,7 +60,7 @@ func (c *Mock) run() {
if c.MemUsage > c.MemLimit {
c.MemUsage = 0
}
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
c.MemPercent = percent(float64(c.MemUsage), float64(c.MemLimit))
c.stream <- c.Metrics
if c.done {
break

View File

@ -19,6 +19,7 @@ func getSysMemTotal() int64 {
return int64(stat.MemTotal * 1024)
}
// return cumulative system cpu usage in nanoseconds
func getSysCPUUsage() uint64 {
stat, err := linuxproc.ReadStat("/proc/stat")
if err != nil {

View File

@ -95,7 +95,7 @@ func (c *Runc) ReadMem(stats *cgroups.Stats) {
if c.MemLimit > sysMemTotal && sysMemTotal > 0 {
c.MemLimit = sysMemTotal
}
c.MemPercent = round((float64(c.MemUsage) / float64(c.MemLimit)) * 100)
c.MemPercent = percent(float64(c.MemUsage), float64(c.MemLimit))
}
func (c *Runc) ReadNet(interfaces []*libcontainer.NetworkInterface) {