diff --git a/CHANGELOG.md b/CHANGELOG.md index 4afed48ac6..005887bcd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,12 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +## [0.4.3] + +## Fixed + +- Add IsZero to CPU Totals, report value for monitoring #40 + ## [0.4.2] ## Fixed diff --git a/metric/system/process/helpers.go b/metric/system/process/helpers.go index fd17cb2e43..362d7953d3 100644 --- a/metric/system/process/helpers.go +++ b/metric/system/process/helpers.go @@ -94,7 +94,6 @@ func GetProcCPUPercentage(s0, s1 ProcState) ProcState { s1.CPU.Total.Norm.Pct = opt.FloatWith(metric.Round(normalizedPct)) s1.CPU.Total.Pct = opt.FloatWith(metric.Round(pct)) - s1.CPU.Total.Value = opt.FloatWith(metric.Round(float64(s1.CPU.Total.Ticks.ValueOr(0)))) return s1 diff --git a/metric/system/process/process.go b/metric/system/process/process.go index 2ad89800da..895f22ea88 100644 --- a/metric/system/process/process.go +++ b/metric/system/process/process.go @@ -30,6 +30,7 @@ import ( "github.com/elastic/elastic-agent-libs/mapstr" "github.com/elastic/elastic-agent-libs/opt" "github.com/elastic/elastic-agent-libs/transform/typeconv" + "github.com/elastic/elastic-agent-system-metrics/metric" "github.com/elastic/elastic-agent-system-metrics/metric/system/resolve" ) @@ -133,7 +134,7 @@ func (procStats *Stats) GetSelf() (ProcState, error) { if err != nil { return ProcState{}, fmt.Errorf("error fetching PID %d: %w", self, err) } - + procStats.ProcsMap[self] = pidStat return pidStat, nil } @@ -202,6 +203,9 @@ func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) { } } // end cgroups processor + if status.CPU.Total.Ticks.Exists() { + status.CPU.Total.Value = opt.FloatWith(metric.Round(float64(status.CPU.Total.Ticks.ValueOr(0)))) + } if ok { status = GetProcCPUPercentage(last, status) } diff --git a/metric/system/process/process_test.go b/metric/system/process/process_test.go index 719b16a51d..cf8c786af3 100644 --- a/metric/system/process/process_test.go +++ b/metric/system/process/process_test.go @@ -28,6 +28,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent-libs/mapstr" @@ -118,6 +119,23 @@ func TestProcessList(t *testing.T) { } } +func TestSelfPersist(t *testing.T) { + stat, err := initTestResolver() + require.NoError(t, err, "Init()") + first, err := stat.GetSelf() + require.NoError(t, err, "First GetSelf()") + + // The first process fetch shouldn't have percentages, since we don't have >1 procs to compare + assert.False(t, first.CPU.Total.Pct.Exists(), "total.pct should not exist") + // Create a proper time delay so the CPU percentage delta calculations don't fail + time.Sleep(time.Millisecond * 5) + second, err := stat.GetSelf() + require.NoError(t, err, "Second GetSelf()") + + // now it should exist + assert.True(t, second.CPU.Total.Pct.Exists(), "total.pct should exist") +} + func TestGetProcess(t *testing.T) { stat, err := initTestResolver() assert.NoError(t, err, "Init()") @@ -227,7 +245,6 @@ func TestProcCpuPercentage(t *testing.T) { assert.EqualValues(t, 0.0721, normalizedTest) assert.EqualValues(t, 3.459, newState.CPU.Total.Pct.ValueOr(0)) - assert.EqualValues(t, 14841, newState.CPU.Total.Value.ValueOr(0)) } // BenchmarkGetProcess runs a benchmark of the GetProcess method with caching diff --git a/metric/system/process/process_types.go b/metric/system/process/process_types.go index 132b361983..426197a6a6 100644 --- a/metric/system/process/process_types.go +++ b/metric/system/process/process_types.go @@ -103,6 +103,10 @@ type ProcLimits struct { // Implementations +func (t CPUTotal) IsZero() bool { + return t.Value.IsZero() && t.Ticks.IsZero() && t.Pct.IsZero() && t.Norm.IsZero() +} + // IsZero returns true if the underlying value nil func (t CPUTicks) IsZero() bool { return t.Ticks.IsZero()