Skip to content

Commit

Permalink
Revert "move exported helpers to _common.go file"
Browse files Browse the repository at this point in the history
This reverts commit eb54ad9.
  • Loading branch information
fearful-symmetry committed Oct 12, 2022
1 parent 6e10948 commit a5ce78c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
47 changes: 47 additions & 0 deletions metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,65 @@
package process

import (
"context"
"fmt"
"os"
"sort"
"strings"
"time"

psutil "github.com/shirou/gopsutil/process"

"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"
)

// ListStates is a wrapper that returns a list of processess with only the basic PID info filled out.
func ListStates(hostfs resolve.Resolver) ([]ProcState, error) {
init := Stats{
Hostfs: hostfs,
Procs: []string{".*"},
EnableCgroups: false,
skipExtended: true,
}
err := init.Init()
if err != nil {
return nil, fmt.Errorf("error initializing process collectors: %w", err)
}

// actually fetch the PIDs from the OS-specific code
_, plist, err := init.FetchPids()
if err != nil {
return nil, fmt.Errorf("error gathering PIDs: %w", err)
}

return plist, nil
}

// GetPIDState returns the state of a given PID
// It will return ProcNotExist if the process was not found.
func GetPIDState(hostfs resolve.Resolver, pid int) (PidState, error) {
// This library still doesn't have a good cross-platform way to distinguish between "does not eixst" and other process errors.
// This is a fairly difficult problem to solve in a cross-platform way
exists, err := psutil.PidExistsWithContext(context.Background(), int32(pid))
if err != nil {
return "", fmt.Errorf("Error trying to find process: %d: %w", pid, err)
}
if !exists {
return "", ProcNotExist
}
//GetInfoForPid will return the smallest possible dataset for a PID
procState, err := GetInfoForPid(hostfs, pid)
if err != nil {
return "", fmt.Errorf("error getting state info for pid %d: %w", pid, err)
}

return procState.State, nil
}

// Get fetches the configured processes and returns a list of formatted events and root ECS fields
func (procStats *Stats) Get() ([]mapstr.M, []mapstr.M, error) {
//If the user hasn't configured any kind of process glob, return
Expand Down
45 changes: 0 additions & 45 deletions metric/system/process/process_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package process

import (
"context"
"errors"
"fmt"
"sync"
Expand All @@ -33,7 +32,6 @@ import (
"github.com/elastic/go-sysinfo/types"

sysinfo "github.com/elastic/go-sysinfo"
psutil "github.com/shirou/gopsutil/process"
)

// ProcNotExist indicates that a process was not found.
Expand Down Expand Up @@ -202,46 +200,3 @@ func (procStats *Stats) Init() error {
}
return nil
}

// ListStates is a wrapper that returns a list of processess with only the basic PID info filled out.
func ListStates(hostfs resolve.Resolver) ([]ProcState, error) {
init := Stats{
Hostfs: hostfs,
Procs: []string{".*"},
EnableCgroups: false,
skipExtended: true,
}
err := init.Init()
if err != nil {
return nil, fmt.Errorf("error initializing process collectors: %w", err)
}

// actually fetch the PIDs from the OS-specific code
_, plist, err := init.FetchPids()
if err != nil {
return nil, fmt.Errorf("error gathering PIDs: %w", err)
}

return plist, nil
}

// GetPIDState returns the state of a given PID
// It will return ProcNotExist if the process was not found.
func GetPIDState(hostfs resolve.Resolver, pid int) (PidState, error) {
// This library still doesn't have a good cross-platform way to distinguish between "does not eixst" and other process errors.
// This is a fairly difficult problem to solve in a cross-platform way
exists, err := psutil.PidExistsWithContext(context.Background(), int32(pid))
if err != nil {
return "", fmt.Errorf("Error truing to find process: %d: %w", pid, err)
}
if !exists {
return "", ProcNotExist
}
//GetInfoForPid will return the smallest possible dataset for a PID
procState, err := GetInfoForPid(hostfs, pid)
if err != nil {
return "", fmt.Errorf("error getting state info for pid %d: %w", pid, err)
}

return procState.State, nil
}

0 comments on commit a5ce78c

Please sign in to comment.