Remove Metric Add() method

* Helps reduce garbage (MakeMetric() now takes a slice and there's a shorter version MakeSingletonMetric())
* Fixes bug computing Max (Min) in samples since using MakeMetric()
  was causing a default Max/Min of zero.
* Simplifies code a bit
This commit is contained in:
Alfonso Acosta
2016-08-01 16:58:11 +00:00
parent 3e000662f4
commit 8a950a59d6
15 changed files with 152 additions and 193 deletions
+18 -11
View File
@@ -360,21 +360,27 @@ func (c *container) NetworkInfo(localAddrs []net.IP) report.Sets {
}
func (c *container) memoryUsageMetric(stats []docker.Stats) report.Metric {
result := report.MakeMetric()
for _, s := range stats {
result = result.Add(s.Read, float64(s.MemoryStats.Usage)).WithMax(float64(s.MemoryStats.Limit))
var max float64
samples := make([]report.Sample, len(stats))
for i, s := range stats {
samples[i].Timestamp = s.Read
samples[i].Value = float64(s.MemoryStats.Usage)
if float64(s.MemoryStats.Limit) > max {
max = float64(s.MemoryStats.Limit)
}
}
return result
return report.MakeMetric(samples).WithMax(max)
}
func (c *container) cpuPercentMetric(stats []docker.Stats) report.Metric {
result := report.MakeMetric()
if len(stats) < 2 {
return result
return report.MakeMetric(nil)
}
samples := make([]report.Sample, len(stats)-1)
var max float64
previous := stats[0]
for _, s := range stats[1:] {
for i, s := range stats[1:] {
// Copies from docker/api/client/stats.go#L205
cpuDelta := float64(s.CPUStats.CPUUsage.TotalUsage - previous.CPUStats.CPUUsage.TotalUsage)
systemDelta := float64(s.CPUStats.SystemCPUUsage - previous.CPUStats.SystemCPUUsage)
@@ -382,14 +388,15 @@ func (c *container) cpuPercentMetric(stats []docker.Stats) report.Metric {
if systemDelta > 0.0 && cpuDelta > 0.0 {
cpuPercent = (cpuDelta / systemDelta) * float64(len(s.CPUStats.CPUUsage.PercpuUsage)) * 100.0
}
result = result.Add(s.Read, cpuPercent)
samples[i].Timestamp = s.Read
samples[i].Value = cpuPercent
available := float64(len(s.CPUStats.CPUUsage.PercpuUsage)) * 100.0
if available >= result.Max {
result.Max = available
if available >= max {
max = available
}
previous = s
}
return result
return report.MakeMetric(samples).WithMax(max)
}
func (c *container) metrics() report.Metrics {
+2 -2
View File
@@ -92,8 +92,8 @@ func TestContainer(t *testing.T) {
docker.RestartContainer, docker.StopContainer, docker.PauseContainer,
docker.AttachContainer, docker.ExecContainer,
).WithMetrics(report.Metrics{
"docker_cpu_total_usage": report.MakeMetric(),
"docker_memory_usage": report.MakeMetric().Add(now, 12345).WithMax(45678),
"docker_cpu_total_usage": report.MakeMetric(nil),
"docker_memory_usage": report.MakeSingletonMetric(now, 12345).WithMax(45678),
}).WithParents(report.EmptySets.
Add(report.ContainerImage, report.MakeStringSet(report.MakeContainerImageNodeID("baz"))),
)
+2 -2
View File
@@ -125,9 +125,9 @@ func (r *Reporter) Report() (report.Report, error) {
now := mtime.Now()
metrics := GetLoad(now)
cpuUsage, max := GetCPUUsagePercent()
metrics[CPUUsage] = report.MakeMetric().Add(now, cpuUsage).WithMax(max)
metrics[CPUUsage] = report.MakeSingletonMetric(now, cpuUsage).WithMax(max)
memoryUsage, max := GetMemoryUsageBytes()
metrics[MemoryUsage] = report.MakeMetric().Add(now, memoryUsage).WithMax(max)
metrics[MemoryUsage] = report.MakeSingletonMetric(now, memoryUsage).WithMax(max)
rep.Host.AddNode(
report.MakeNodeWith(report.MakeHostNodeID(r.hostID), map[string]string{
+3 -3
View File
@@ -20,9 +20,9 @@ func TestReporter(t *testing.T) {
hostname = "hostname"
timestamp = time.Now()
metrics = report.Metrics{
host.Load1: report.MakeMetric().Add(timestamp, 1.0),
host.CPUUsage: report.MakeMetric().Add(timestamp, 30.0).WithMax(100.0),
host.MemoryUsage: report.MakeMetric().Add(timestamp, 60.0).WithMax(100.0),
host.Load1: report.MakeSingletonMetric(timestamp, 1.0),
host.CPUUsage: report.MakeSingletonMetric(timestamp, 30.0).WithMax(100.0),
host.MemoryUsage: report.MakeSingletonMetric(timestamp, 60.0).WithMax(100.0),
}
uptime = "278h55m43s"
kernel = "release version"
+1 -1
View File
@@ -45,7 +45,7 @@ var GetLoad = func(now time.Time) report.Metrics {
return nil
}
return report.Metrics{
Load1: report.MakeMetric().Add(now, one),
Load1: report.MakeSingletonMetric(now, one),
}
}
+1 -1
View File
@@ -45,7 +45,7 @@ var GetLoad = func(now time.Time) report.Metrics {
return nil
}
return report.Metrics{
Load1: report.MakeMetric().Add(now, one),
Load1: report.MakeSingletonMetric(now, one),
}
}
+3 -3
View File
@@ -99,11 +99,11 @@ func (r *Reporter) processTopology() (report.Topology, error) {
if deltaTotal > 0 {
cpuUsage := float64(p.Jiffies-prev.Jiffies) / float64(deltaTotal) * 100.
node = node.WithMetric(CPUUsage, report.MakeMetric().Add(now, cpuUsage).WithMax(maxCPU))
node = node.WithMetric(CPUUsage, report.MakeSingletonMetric(now, cpuUsage).WithMax(maxCPU))
}
node = node.WithMetric(MemoryUsage, report.MakeMetric().Add(now, float64(p.RSSBytes)).WithMax(float64(p.RSSBytesLimit)))
node = node.WithMetric(OpenFilesCount, report.MakeMetric().Add(now, float64(p.OpenFilesCount)).WithMax(float64(p.OpenFilesLimit)))
node = node.WithMetric(MemoryUsage, report.MakeSingletonMetric(now, float64(p.RSSBytes)).WithMax(float64(p.RSSBytesLimit)))
node = node.WithMetric(OpenFilesCount, report.MakeSingletonMetric(now, float64(p.OpenFilesCount)).WithMax(float64(p.OpenFilesLimit)))
t.AddNode(node)
})