Remove config option, percent option always activated. Fix review issues

This commit is contained in:
Mike Tonks
2016-02-11 10:49:48 +00:00
committed by Michele Fadda
parent 1ba3192de8
commit f6a0787b06
3 changed files with 65 additions and 110 deletions

View File

@@ -14,9 +14,8 @@ import (
)
type Docker struct {
Endpoint string
ContainerNames []string
CalculatePercentages bool
Endpoint string
ContainerNames []string
client *docker.Client
}
@@ -28,8 +27,6 @@ var sampleConfig = `
endpoint = "unix:///var/run/docker.sock"
### Only collect metrics for these containers, collect all if empty
container_names = []
# Add calculated percentages for mem and cpu, as per 'docker stats' command
calculate_percentages = false
`
func (d *Docker) Description() string {
@@ -135,7 +132,7 @@ func (d *Docker) gatherContainer(
tags[k] = v
}
gatherContainerStats(stat, acc, tags, d.CalculatePercentages)
gatherContainerStats(stat, acc, tags)
return nil
}
@@ -144,7 +141,6 @@ func gatherContainerStats(
stat *docker.Stats,
acc telegraf.Accumulator,
tags map[string]string,
calculate_percentages bool,
) {
now := stat.Read
@@ -182,9 +178,7 @@ func gatherContainerStats(
"pgfault": stat.MemoryStats.Stats.Pgfault,
"inactive_file": stat.MemoryStats.Stats.InactiveFile,
"total_pgpgin": stat.MemoryStats.Stats.TotalPgpgin,
}
if calculate_percentages {
memfields["usage_percent"] = float64(stat.MemoryStats.Usage) / float64(stat.MemoryStats.Limit) * 100.0
"usage_percent": calculateMemPercent(stat),
}
acc.AddFields("docker_mem", memfields, tags, now)
@@ -196,9 +190,7 @@ func gatherContainerStats(
"throttling_periods": stat.CPUStats.ThrottlingData.Periods,
"throttling_throttled_periods": stat.CPUStats.ThrottlingData.ThrottledPeriods,
"throttling_throttled_time": stat.CPUStats.ThrottlingData.ThrottledTime,
}
if calculate_percentages {
cpufields["usage_percent"] = calculateCPUPercent(stat)
"usage_percent": calculateCPUPercent(stat),
}
cputags := copyTags(tags)
cputags["cpu"] = "cpu-total"
@@ -230,14 +222,19 @@ func gatherContainerStats(
gatherBlockIOMetrics(stat, acc, tags, now)
}
func calculateMemPercent(stat *docker.Stats) float64 {
var memPercent = 0.0
if stat.MemoryStats.Limit > 0 {
memPercent = float64(stat.MemoryStats.Usage) / float64(stat.MemoryStats.Limit) * 100.0
}
return memPercent
}
func calculateCPUPercent(stat *docker.Stats) float64 {
var (
cpuPercent = 0.0
// calculate the change for the cpu usage of the container in between readings
cpuDelta = float64(stat.CPUStats.CPUUsage.TotalUsage) - float64(stat.PreCPUStats.CPUUsage.TotalUsage)
// calculate the change for the entire system between readings
systemDelta = float64(stat.CPUStats.SystemCPUUsage) - float64(stat.PreCPUStats.SystemCPUUsage)
)
var cpuPercent = 0.0
// calculate the change for the cpu and system usage of the container in between readings
cpuDelta := float64(stat.CPUStats.CPUUsage.TotalUsage) - float64(stat.PreCPUStats.CPUUsage.TotalUsage)
systemDelta := float64(stat.CPUStats.SystemCPUUsage) - float64(stat.PreCPUStats.SystemCPUUsage)
if systemDelta > 0.0 && cpuDelta > 0.0 {
cpuPercent = (cpuDelta / systemDelta) * float64(len(stat.CPUStats.CPUUsage.PercpuUsage)) * 100.0