Skip to content

Commit

Permalink
Fix test for windows and darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed Nov 3, 2023
1 parent 4580a3f commit fd8948c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 deletions.
5 changes: 5 additions & 0 deletions plugins/inputs/procstat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
Preliminary support for Windows has been added, however you may prefer using
the `win_perf_counters` input plugin as a more mature alternative.

### Darwin specifics

If you use this plugin with `supervisor_units` *and* `pattern` on Darwin, you
**have to** use the `pgrep` finder as the underlying library relies on `pgrep`.

### Permissions

Some files or directories may require elevated permissions. As such a user may
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/procstat/native_finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func BenchmarkFullPattern(b *testing.B) {
}

func TestChildPattern(t *testing.T) {
if runtime.GOOS == "Windows" {
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
t.Skip("Skipping test on unsupported platform")
}

Expand Down
54 changes: 27 additions & 27 deletions plugins/inputs/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package procstat
import (
"bytes"
_ "embed"
"errors"
"fmt"
"os"
"os/exec"
Expand All @@ -22,11 +23,6 @@ import (
//go:embed sample.conf
var sampleConfig string

var (
defaultPIDFinder = NewPgrep
defaultProcess = NewProc
)

type PID int32

type Procstat struct {
Expand Down Expand Up @@ -65,22 +61,34 @@ func (*Procstat) SampleConfig() string {
return sampleConfig
}

func (p *Procstat) Gather(acc telegraf.Accumulator) error {
if p.createPIDFinder == nil {
switch p.PidFinder {
case "native":
p.createPIDFinder = NewNativeFinder
case "pgrep":
p.createPIDFinder = NewPgrep
default:
p.PidFinder = "pgrep"
p.createPIDFinder = defaultPIDFinder
}
func (p *Procstat) Init() error {
if strings.ToLower(p.Mode) == "solaris" {
p.solarisMode = true
}
if p.createProcess == nil {
p.createProcess = defaultProcess

switch p.PidFinder {
case "":
p.PidFinder = "pgrep"
p.createPIDFinder = NewPgrep
case "native":
p.createPIDFinder = NewNativeFinder
case "pgrep":
p.createPIDFinder = NewPgrep
default:
return fmt.Errorf("unknown pid_finder %q", p.PidFinder)
}

// gopsutil relies on pgrep when looking up children on darwin
// see https://github.com/shirou/gopsutil/blob/v3.23.10/process/process_darwin.go#L235
requiresChildren := len(p.SupervisorUnit) > 0 && p.Pattern != ""
if requiresChildren && p.PidFinder == "native" && runtime.GOOS == "darwin" {
return errors.New("configuration requires the 'pgrep' finder on you OS")
}

return nil
}

func (p *Procstat) Gather(acc telegraf.Accumulator) error {
pidCount := 0
now := time.Now()
newProcs := make(map[PID]Process, len(p.procs))
Expand Down Expand Up @@ -600,16 +608,8 @@ func (p *Procstat) winServicePIDs() ([]PID, error) {
return pids, nil
}

func (p *Procstat) Init() error {
if strings.ToLower(p.Mode) == "solaris" {
p.solarisMode = true
}

return nil
}

func init() {
inputs.Add("procstat", func() telegraf.Input {
return &Procstat{}
return &Procstat{createProcess: NewProc}
})
}
17 changes: 15 additions & 2 deletions plugins/inputs/procstat/procstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,19 @@ func (p *testProc) Status() ([]string, error) {
var pid = PID(42)
var exe = "foo"

func TestInitRequiresChildDarwin(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("Skipping test on non-darwin platform")
}

p := Procstat{
Pattern: "somepattern",
SupervisorUnit: []string{"a_unit"},
PidFinder: "native",
}
require.ErrorContains(t, p.Init(), "requires the 'pgrep' finder")
}

func TestGather_CreateProcessErrorOk(t *testing.T) {
var acc testutil.Accumulator

Expand Down Expand Up @@ -433,11 +446,11 @@ func TestGather_cgroupPIDs(t *testing.T) {
func TestProcstatLookupMetric(t *testing.T) {
p := Procstat{
createPIDFinder: pidFinder([]PID{543}),
createProcess: NewProc,
Exe: "-Gsys",
}
var acc testutil.Accumulator
err := acc.GatherError(p.Gather)
require.NoError(t, err)
require.NoError(t, acc.GatherError(p.Gather))
require.Len(t, acc.Metrics, len(p.procs)+1)
}

Expand Down

0 comments on commit fd8948c

Please sign in to comment.