diff --git a/plugins/system/cpu.go b/plugins/system/cpu.go index f49340da2..081a8ac73 100644 --- a/plugins/system/cpu.go +++ b/plugins/system/cpu.go @@ -47,7 +47,7 @@ func (s *CPUStats) Gather(acc plugins.Accumulator) error { "cpu": cts.CPU, } - busy, total := busyAndTotalCpuTime(cts) + total := totalCpuTime(cts) // Add total cpu numbers add(acc, "user", cts.User, tags) @@ -59,9 +59,7 @@ func (s *CPUStats) Gather(acc plugins.Accumulator) error { add(acc, "softirq", cts.Softirq, tags) add(acc, "steal", cts.Steal, tags) add(acc, "guest", cts.Guest, tags) - add(acc, "guestNice", cts.GuestNice, tags) - add(acc, "stolen", cts.Stolen, tags) - add(acc, "busy", busy, tags) + add(acc, "guest_nice", cts.GuestNice, tags) // Add in percentage if len(s.lastStats) == 0 { @@ -69,8 +67,7 @@ func (s *CPUStats) Gather(acc plugins.Accumulator) error { continue } lastCts := s.lastStats[i] - lastBusy, lastTotal := busyAndTotalCpuTime(lastCts) - busyDelta := busy - lastBusy + lastTotal := totalCpuTime(lastCts) totalDelta := total - lastTotal if totalDelta < 0 { @@ -81,19 +78,18 @@ func (s *CPUStats) Gather(acc plugins.Accumulator) error { continue } - add(acc, "percentageUser", 100*(cts.User-lastCts.User)/totalDelta, tags) - add(acc, "percentageSystem", 100*(cts.System-lastCts.System)/totalDelta, tags) - add(acc, "percentageIdle", 100*(cts.Idle-lastCts.Idle)/totalDelta, tags) - add(acc, "percentageNice", 100*(cts.Nice-lastCts.Nice)/totalDelta, tags) - add(acc, "percentageIowait", 100*(cts.Iowait-lastCts.Iowait)/totalDelta, tags) - add(acc, "percentageIrq", 100*(cts.Irq-lastCts.Irq)/totalDelta, tags) - add(acc, "percentageSoftirq", 100*(cts.Softirq-lastCts.Softirq)/totalDelta, tags) - add(acc, "percentageSteal", 100*(cts.Steal-lastCts.Steal)/totalDelta, tags) - add(acc, "percentageGuest", 100*(cts.Guest-lastCts.Guest)/totalDelta, tags) - add(acc, "percentageGuestNice", 100*(cts.GuestNice-lastCts.GuestNice)/totalDelta, tags) - add(acc, "percentageStolen", 100*(cts.Stolen-lastCts.Stolen)/totalDelta, tags) - - add(acc, "percentageBusy", 100*busyDelta/totalDelta, tags) + usage_idle := 100 * (cts.Idle - lastCts.Idle) / totalDelta + add(acc, "usage_user", 100*(cts.User-lastCts.User)/totalDelta, tags) + add(acc, "usage_system", 100*(cts.System-lastCts.System)/totalDelta, tags) + add(acc, "usage_idle", usage_idle, tags) + add(acc, "usage_nice", 100*(cts.Nice-lastCts.Nice)/totalDelta, tags) + add(acc, "usage_iowait", 100*(cts.Iowait-lastCts.Iowait)/totalDelta, tags) + add(acc, "usage_irq", 100*(cts.Irq-lastCts.Irq)/totalDelta, tags) + add(acc, "usage_softirq", 100*(cts.Softirq-lastCts.Softirq)/totalDelta, tags) + add(acc, "usage_steal", 100*(cts.Steal-lastCts.Steal)/totalDelta, tags) + add(acc, "usage_guest", 100*(cts.Guest-lastCts.Guest)/totalDelta, tags) + add(acc, "usage_guest_nice", 100*(cts.GuestNice-lastCts.GuestNice)/totalDelta, tags) + add(acc, "usage_busy", 100.0-usage_idle, tags) } @@ -102,11 +98,10 @@ func (s *CPUStats) Gather(acc plugins.Accumulator) error { return nil } -func busyAndTotalCpuTime(t cpu.CPUTimesStat) (float64, float64) { - busy := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal + - t.Guest + t.GuestNice + t.Stolen - - return busy, busy + t.Idle +func totalCpuTime(t cpu.CPUTimesStat) float64 { + total := t.User + t.System + t.Nice + t.Iowait + t.Irq + t.Softirq + t.Steal + + t.Guest + t.GuestNice + t.Idle + return total } func init() { diff --git a/plugins/system/docker.go b/plugins/system/docker.go index 5e22dc88e..463c1f317 100644 --- a/plugins/system/docker.go +++ b/plugins/system/docker.go @@ -42,8 +42,7 @@ func (s *DockerStats) Gather(acc plugins.Accumulator) error { acc.Add("softirq", cts.Softirq, tags) acc.Add("steal", cts.Steal, tags) acc.Add("guest", cts.Guest, tags) - acc.Add("guestNice", cts.GuestNice, tags) - acc.Add("stolen", cts.Stolen, tags) + acc.Add("guest_nice", cts.GuestNice, tags) acc.Add("cache", cont.Mem.Cache, tags) acc.Add("rss", cont.Mem.RSS, tags) diff --git a/plugins/system/docker_test.go b/plugins/system/docker_test.go index e102fd115..1fbf76d10 100644 --- a/plugins/system/docker_test.go +++ b/plugins/system/docker_test.go @@ -31,7 +31,6 @@ func TestDockerStats_GenerateStats(t *testing.T) { Steal: 0.0001, Guest: 8.1, GuestNice: 0.324, - Stolen: 0.051, }, Mem: &docker.CgroupMemStat{ ContainerID: "blah", @@ -85,8 +84,7 @@ func TestDockerStats_GenerateStats(t *testing.T) { assert.True(t, acc.CheckTaggedValue("softirq", 0.11, dockertags)) assert.True(t, acc.CheckTaggedValue("steal", 0.0001, dockertags)) assert.True(t, acc.CheckTaggedValue("guest", 8.1, dockertags)) - assert.True(t, acc.CheckTaggedValue("guestNice", 0.324, dockertags)) - assert.True(t, acc.CheckTaggedValue("stolen", 0.051, dockertags)) + assert.True(t, acc.CheckTaggedValue("guest_nice", 0.324, dockertags)) assert.True(t, acc.CheckTaggedValue("cache", uint64(1), dockertags)) assert.True(t, acc.CheckTaggedValue("rss", uint64(2), dockertags)) diff --git a/plugins/system/system_test.go b/plugins/system/system_test.go index 5eb13733b..62f2e264d 100644 --- a/plugins/system/system_test.go +++ b/plugins/system/system_test.go @@ -30,10 +30,9 @@ func TestSystemStats_GenerateStats(t *testing.T) { Iowait: 0.2, Irq: 0.1, Softirq: 0.11, - Steal: 0.0001, + Steal: 0.0511, Guest: 8.1, GuestNice: 0.324, - Stolen: 0.051, } cts2 := cpu.CPUTimesStat{ @@ -45,10 +44,9 @@ func TestSystemStats_GenerateStats(t *testing.T) { Iowait: 0.7, // increased by 0.5 Irq: 1.2, // increased by 1.1 Softirq: 0.31, // increased by 0.2 - Steal: 0.0002, // increased by 0.0001 + Steal: 0.2812, // increased by 0.0001 Guest: 12.9, // increased by 4.8 GuestNice: 2.524, // increased by 2.2 - Stolen: 0.281, // increased by 0.23 } mps.On("CPUTimes").Return([]cpu.CPUTimesStat{cts}, nil) @@ -130,7 +128,7 @@ func TestSystemStats_GenerateStats(t *testing.T) { require.NoError(t, err) numCPUPoints := len(acc.Points) - preCPUPoints - expectedCPUPoints := 12 + expectedCPUPoints := 10 assert.Equal(t, numCPUPoints, expectedCPUPoints) // Computed values are checked with delta > 0 becasue of floating point arithmatic @@ -142,11 +140,9 @@ func TestSystemStats_GenerateStats(t *testing.T) { assertContainsTaggedFloat(t, acc, "iowait", 0.2, 0, cputags) assertContainsTaggedFloat(t, acc, "irq", 0.1, 0, cputags) assertContainsTaggedFloat(t, acc, "softirq", 0.11, 0, cputags) - assertContainsTaggedFloat(t, acc, "steal", 0.0001, 0, cputags) + assertContainsTaggedFloat(t, acc, "steal", 0.0511, 0, cputags) assertContainsTaggedFloat(t, acc, "guest", 8.1, 0, cputags) - assertContainsTaggedFloat(t, acc, "guestNice", 0.324, 0, cputags) - assertContainsTaggedFloat(t, acc, "stolen", 0.051, 0, cputags) - assertContainsTaggedFloat(t, acc, "busy", 21.4851, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "guest_nice", 0.324, 0, cputags) mps2 := MockPS{} mps2.On("CPUTimes").Return([]cpu.CPUTimesStat{cts2}, nil) @@ -157,7 +153,7 @@ func TestSystemStats_GenerateStats(t *testing.T) { require.NoError(t, err) numCPUPoints = len(acc.Points) - (preCPUPoints + numCPUPoints) - expectedCPUPoints = 24 + expectedCPUPoints = 21 assert.Equal(t, numCPUPoints, expectedCPUPoints) assertContainsTaggedFloat(t, acc, "user", 11.4, 0, cputags) @@ -167,24 +163,21 @@ func TestSystemStats_GenerateStats(t *testing.T) { assertContainsTaggedFloat(t, acc, "iowait", 0.7, 0, cputags) assertContainsTaggedFloat(t, acc, "irq", 1.2, 0, cputags) assertContainsTaggedFloat(t, acc, "softirq", 0.31, 0, cputags) - assertContainsTaggedFloat(t, acc, "steal", 0.0002, 0, cputags) + assertContainsTaggedFloat(t, acc, "steal", 0.2812, 0, cputags) assertContainsTaggedFloat(t, acc, "guest", 12.9, 0, cputags) - assertContainsTaggedFloat(t, acc, "guestNice", 2.524, 0, cputags) - assertContainsTaggedFloat(t, acc, "stolen", 0.281, 0, cputags) - assertContainsTaggedFloat(t, acc, "busy", 42.7152, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "guest_nice", 2.524, 0, cputags) - assertContainsTaggedFloat(t, acc, "percentageUser", 8.3, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageSystem", 2.7, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageIdle", 78.7699, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageNice", 1.2, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageIowait", 0.5, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageIrq", 1.1, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageSoftirq", 0.2, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageSteal", 0.0001, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageGuest", 4.8, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageGuestNice", 2.2, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageStolen", 0.23, 0.0005, cputags) - assertContainsTaggedFloat(t, acc, "percentageBusy", 21.2301, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_user", 8.3, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_system", 2.7, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_idle", 78.7699, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_nice", 1.2, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_iowait", 0.5, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_irq", 1.1, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_softirq", 0.2, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_steal", 0.2301, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_guest", 4.8, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_guest_nice", 2.2, 0.0005, cputags) + assertContainsTaggedFloat(t, acc, "usage_busy", 21.2301, 0.0005, cputags) err = (&DiskStats{&mps}).Gather(&acc) require.NoError(t, err) @@ -288,20 +281,25 @@ func assertContainsTaggedFloat( delta float64, tags map[string]string, ) { + var actualValue float64 for _, pt := range acc.Points { if pt.Measurement == measurement { if (tags == nil) || reflect.DeepEqual(pt.Tags, tags) { if value, ok := pt.Values["value"].(float64); ok { + actualValue = value if (value >= expectedValue-delta) && (value <= expectedValue+delta) { // Found the point, return without failing return } } else { - assert.Fail(t, fmt.Sprintf("Measurement \"%s\" does not have type float64", measurement)) + assert.Fail(t, fmt.Sprintf("Measurement \"%s\" does not have type float64", + measurement)) } } } } - assert.Fail(t, fmt.Sprintf("Could not find measurement \"%s\" with requested tags within %f of %f", measurement, delta, expectedValue)) + msg := fmt.Sprintf("Could not find measurement \"%s\" with requested tags within %f of %f, Actual: %f", + measurement, delta, expectedValue, actualValue) + assert.Fail(t, msg) }