Skip to content

Commit 7b5eca6

Browse files
Merge pull request #57 from fearful-symmetry/revert-process-file-shuffle
Revert "move exported helpers to _common.go file"
2 parents 6e10948 + a5ce78c commit 7b5eca6

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

metric/system/process/process.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,65 @@
2121
package process
2222

2323
import (
24+
"context"
2425
"fmt"
2526
"os"
2627
"sort"
2728
"strings"
2829
"time"
2930

31+
psutil "github.com/shirou/gopsutil/process"
32+
3033
"github.com/elastic/elastic-agent-libs/mapstr"
3134
"github.com/elastic/elastic-agent-libs/opt"
3235
"github.com/elastic/elastic-agent-libs/transform/typeconv"
3336
"github.com/elastic/elastic-agent-system-metrics/metric"
37+
"github.com/elastic/elastic-agent-system-metrics/metric/system/resolve"
3438
)
3539

40+
// ListStates is a wrapper that returns a list of processess with only the basic PID info filled out.
41+
func ListStates(hostfs resolve.Resolver) ([]ProcState, error) {
42+
init := Stats{
43+
Hostfs: hostfs,
44+
Procs: []string{".*"},
45+
EnableCgroups: false,
46+
skipExtended: true,
47+
}
48+
err := init.Init()
49+
if err != nil {
50+
return nil, fmt.Errorf("error initializing process collectors: %w", err)
51+
}
52+
53+
// actually fetch the PIDs from the OS-specific code
54+
_, plist, err := init.FetchPids()
55+
if err != nil {
56+
return nil, fmt.Errorf("error gathering PIDs: %w", err)
57+
}
58+
59+
return plist, nil
60+
}
61+
62+
// GetPIDState returns the state of a given PID
63+
// It will return ProcNotExist if the process was not found.
64+
func GetPIDState(hostfs resolve.Resolver, pid int) (PidState, error) {
65+
// This library still doesn't have a good cross-platform way to distinguish between "does not eixst" and other process errors.
66+
// This is a fairly difficult problem to solve in a cross-platform way
67+
exists, err := psutil.PidExistsWithContext(context.Background(), int32(pid))
68+
if err != nil {
69+
return "", fmt.Errorf("Error trying to find process: %d: %w", pid, err)
70+
}
71+
if !exists {
72+
return "", ProcNotExist
73+
}
74+
//GetInfoForPid will return the smallest possible dataset for a PID
75+
procState, err := GetInfoForPid(hostfs, pid)
76+
if err != nil {
77+
return "", fmt.Errorf("error getting state info for pid %d: %w", pid, err)
78+
}
79+
80+
return procState.State, nil
81+
}
82+
3683
// Get fetches the configured processes and returns a list of formatted events and root ECS fields
3784
func (procStats *Stats) Get() ([]mapstr.M, []mapstr.M, error) {
3885
//If the user hasn't configured any kind of process glob, return

metric/system/process/process_common.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
package process
2222

2323
import (
24-
"context"
2524
"errors"
2625
"fmt"
2726
"sync"
@@ -33,7 +32,6 @@ import (
3332
"github.com/elastic/go-sysinfo/types"
3433

3534
sysinfo "github.com/elastic/go-sysinfo"
36-
psutil "github.com/shirou/gopsutil/process"
3735
)
3836

3937
// ProcNotExist indicates that a process was not found.
@@ -202,46 +200,3 @@ func (procStats *Stats) Init() error {
202200
}
203201
return nil
204202
}
205-
206-
// ListStates is a wrapper that returns a list of processess with only the basic PID info filled out.
207-
func ListStates(hostfs resolve.Resolver) ([]ProcState, error) {
208-
init := Stats{
209-
Hostfs: hostfs,
210-
Procs: []string{".*"},
211-
EnableCgroups: false,
212-
skipExtended: true,
213-
}
214-
err := init.Init()
215-
if err != nil {
216-
return nil, fmt.Errorf("error initializing process collectors: %w", err)
217-
}
218-
219-
// actually fetch the PIDs from the OS-specific code
220-
_, plist, err := init.FetchPids()
221-
if err != nil {
222-
return nil, fmt.Errorf("error gathering PIDs: %w", err)
223-
}
224-
225-
return plist, nil
226-
}
227-
228-
// GetPIDState returns the state of a given PID
229-
// It will return ProcNotExist if the process was not found.
230-
func GetPIDState(hostfs resolve.Resolver, pid int) (PidState, error) {
231-
// This library still doesn't have a good cross-platform way to distinguish between "does not eixst" and other process errors.
232-
// This is a fairly difficult problem to solve in a cross-platform way
233-
exists, err := psutil.PidExistsWithContext(context.Background(), int32(pid))
234-
if err != nil {
235-
return "", fmt.Errorf("Error truing to find process: %d: %w", pid, err)
236-
}
237-
if !exists {
238-
return "", ProcNotExist
239-
}
240-
//GetInfoForPid will return the smallest possible dataset for a PID
241-
procState, err := GetInfoForPid(hostfs, pid)
242-
if err != nil {
243-
return "", fmt.Errorf("error getting state info for pid %d: %w", pid, err)
244-
}
245-
246-
return procState.State, nil
247-
}

0 commit comments

Comments
 (0)