Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CmdLine generation and caching #107

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fix CmdLine generation and caching for system.process

## [0.7.0]

### Added
Expand Down
13 changes: 10 additions & 3 deletions metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) {
if procStats.skipExtended {
return status, true, nil
}

// Some OSes use the cache to avoid expensive system calls,
// cacheCmdLine reads from the cache.
status = procStats.cacheCmdLine(status)
belimawr marked this conversation as resolved.
Show resolved Hide resolved

// Filter based on user-supplied func
Expand All @@ -236,9 +239,7 @@ func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) {
if err != nil {
return status, true, fmt.Errorf("FillPidMetrics: %w", err)
}
if len(status.Args) > 0 && status.Cmdline == "" {
status.Cmdline = strings.Join(status.Args, " ")
}

if status.CPU.Total.Ticks.Exists() {
status.CPU.Total.Value = opt.FloatWith(metric.Round(float64(status.CPU.Total.Ticks.ValueOr(0))))
}
Expand Down Expand Up @@ -266,6 +267,12 @@ func (procStats *Stats) pidFill(pid int, filter bool) (ProcState, bool, error) {
return status, true, fmt.Errorf("FillMetricsRequiringMoreAccess: %w", err)
}

// Generate `status.Cmdline` here for compatibility because on Windows
// `status.Args` is set by `FillMetricsRequiringMoreAccess`.
if len(status.Args) > 0 && status.Cmdline == "" {
status.Cmdline = strings.Join(status.Args, " ")
}

// network data
if procStats.EnableNetwork {
procHandle, err := sysinfo.Process(pid)
Expand Down
2 changes: 2 additions & 0 deletions metric/system/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ func TestGetProcess(t *testing.T) {
case "linux":
assert.True(t, (len(process.Cwd) > 0))
}

assert.NotEmptyf(t, process.Cmdline, "cmdLine must be present")
}

func TestMatchProcs(t *testing.T) {
Expand Down