mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-28 17:51:21 +00:00
* Revert "Revert "Add options to hide args and env vars (#2306)"" * Make linter happy
This commit is contained in:
@@ -2,6 +2,7 @@ package process
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/weaveworks/common/mtime"
|
||||
"github.com/weaveworks/scope/report"
|
||||
@@ -37,20 +38,22 @@ var (
|
||||
|
||||
// Reporter generates Reports containing the Process topology.
|
||||
type Reporter struct {
|
||||
scope string
|
||||
walker Walker
|
||||
jiffies Jiffies
|
||||
scope string
|
||||
walker Walker
|
||||
jiffies Jiffies
|
||||
noCommandLineArguments bool
|
||||
}
|
||||
|
||||
// Jiffies is the type for the function used to fetch the elapsed jiffies.
|
||||
type Jiffies func() (uint64, float64, error)
|
||||
|
||||
// NewReporter makes a new Reporter.
|
||||
func NewReporter(walker Walker, scope string, jiffies Jiffies) *Reporter {
|
||||
func NewReporter(walker Walker, scope string, jiffies Jiffies, noCommandLineArguments bool) *Reporter {
|
||||
return &Reporter{
|
||||
scope: scope,
|
||||
walker: walker,
|
||||
jiffies: jiffies,
|
||||
scope: scope,
|
||||
walker: walker,
|
||||
jiffies: jiffies,
|
||||
noCommandLineArguments: noCommandLineArguments,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +88,6 @@ func (r *Reporter) processTopology() (report.Topology, error) {
|
||||
for _, tuple := range []struct{ key, value string }{
|
||||
{PID, pidstr},
|
||||
{Name, p.Name},
|
||||
{Cmdline, p.Cmdline},
|
||||
{Threads, strconv.Itoa(p.Threads)},
|
||||
} {
|
||||
if tuple.value != "" {
|
||||
@@ -93,6 +95,14 @@ func (r *Reporter) processTopology() (report.Topology, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if p.Cmdline != "" {
|
||||
if r.noCommandLineArguments {
|
||||
node = node.WithLatests(map[string]string{Cmdline: strings.Split(p.Cmdline, " ")[0]})
|
||||
} else {
|
||||
node = node.WithLatests(map[string]string{Cmdline: p.Cmdline})
|
||||
}
|
||||
}
|
||||
|
||||
if p.PPID > 0 {
|
||||
node = node.WithLatests(map[string]string{PPID: strconv.Itoa(p.PPID)})
|
||||
}
|
||||
|
||||
@@ -21,80 +21,116 @@ func (m *mockWalker) Walk(f func(process.Process, process.Process)) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestReporter(t *testing.T) {
|
||||
processes := []process.Process{
|
||||
{PID: 1, PPID: 0, Name: "init"},
|
||||
{PID: 2, PPID: 1, Name: "bash"},
|
||||
{PID: 3, PPID: 1, Name: "apache", Threads: 2},
|
||||
{PID: 4, PPID: 2, Name: "ping", Cmdline: "ping foo.bar.local"},
|
||||
{PID: 5, PPID: 1, Cmdline: "tail -f /var/log/syslog"},
|
||||
}
|
||||
var processes = []process.Process{
|
||||
{PID: 1, PPID: 0, Name: "init"},
|
||||
{PID: 2, PPID: 1, Name: "bash"},
|
||||
{PID: 3, PPID: 1, Name: "apache", Threads: 2},
|
||||
{PID: 4, PPID: 2, Name: "ping", Cmdline: "ping foo.bar.local"},
|
||||
{PID: 5, PPID: 1, Cmdline: "tail -f /var/log/syslog"},
|
||||
}
|
||||
|
||||
func testReporter(t *testing.T, noCommandLineArguments bool, test func(report.Report)) {
|
||||
walker := &mockWalker{processes: processes}
|
||||
getDeltaTotalJiffies := func() (uint64, float64, error) { return 0, 0., nil }
|
||||
now := time.Now()
|
||||
mtime.NowForce(now)
|
||||
defer mtime.NowReset()
|
||||
|
||||
rpt, err := process.NewReporter(walker, "", getDeltaTotalJiffies).Report()
|
||||
rpt, err := process.NewReporter(walker, "", getDeltaTotalJiffies, noCommandLineArguments).Report()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// It reports the init process
|
||||
node, ok := rpt.Process.Nodes[report.MakeProcessNodeID("", "1")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 1 init")
|
||||
}
|
||||
if name, ok := node.Latest.Lookup(process.Name); !ok || name != processes[0].Name {
|
||||
t.Errorf("Expected %q got %q", processes[0].Name, name)
|
||||
}
|
||||
|
||||
// It reports plain processes (with parent pid, and metrics)
|
||||
node, ok = rpt.Process.Nodes[report.MakeProcessNodeID("", "2")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 2 bash")
|
||||
}
|
||||
if name, ok := node.Latest.Lookup(process.Name); !ok || name != processes[1].Name {
|
||||
t.Errorf("Expected %q got %q", processes[1].Name, name)
|
||||
}
|
||||
if ppid, ok := node.Latest.Lookup(process.PPID); !ok || ppid != fmt.Sprint(processes[1].PPID) {
|
||||
t.Errorf("Expected %d got %q", processes[1].PPID, ppid)
|
||||
}
|
||||
if memoryUsage, ok := node.Metrics[process.MemoryUsage]; !ok {
|
||||
t.Errorf("Expected memory usage metric, but not found")
|
||||
} else if sample, ok := memoryUsage.LastSample(); !ok {
|
||||
t.Errorf("Expected memory usage metric to have a sample, but there were none")
|
||||
} else if sample.Value != 0. {
|
||||
t.Errorf("Expected memory usage metric sample %f, got %f", 0., sample.Value)
|
||||
}
|
||||
|
||||
// It reports thread-counts
|
||||
node, ok = rpt.Process.Nodes[report.MakeProcessNodeID("", "3")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 3 apache")
|
||||
}
|
||||
if threads, ok := node.Latest.Lookup(process.Threads); !ok || threads != fmt.Sprint(processes[2].Threads) {
|
||||
t.Errorf("Expected %d got %q", processes[2].Threads, threads)
|
||||
}
|
||||
|
||||
// It reports the Cmdline
|
||||
node, ok = rpt.Process.Nodes[report.MakeProcessNodeID("", "4")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 4 ping")
|
||||
}
|
||||
if cmdline, ok := node.Latest.Lookup(process.Cmdline); !ok || cmdline != fmt.Sprint(processes[3].Cmdline) {
|
||||
t.Errorf("Expected %q got %q", processes[3].Cmdline, cmdline)
|
||||
}
|
||||
|
||||
// It reports processes without a Name
|
||||
node, ok = rpt.Process.Nodes[report.MakeProcessNodeID("", "5")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 5 tail")
|
||||
}
|
||||
if name, ok := node.Latest.Lookup(process.Name); ok {
|
||||
t.Errorf("Expected no name, but got %q", name)
|
||||
}
|
||||
if cmdline, ok := node.Latest.Lookup(process.Cmdline); !ok || cmdline != fmt.Sprint(processes[4].Cmdline) {
|
||||
t.Errorf("Expected %q got %q", processes[4].Cmdline, cmdline)
|
||||
}
|
||||
test(rpt)
|
||||
}
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
test := func(rpt report.Report) {
|
||||
node, ok := rpt.Process.Nodes[report.MakeProcessNodeID("", "1")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 1 init")
|
||||
}
|
||||
if name, ok := node.Latest.Lookup(process.Name); !ok || name != processes[0].Name {
|
||||
t.Errorf("Expected %q got %q", processes[0].Name, name)
|
||||
}
|
||||
}
|
||||
testReporter(t, false, test)
|
||||
}
|
||||
|
||||
func TestProcesses(t *testing.T) {
|
||||
test := func(rpt report.Report) {
|
||||
node, ok := rpt.Process.Nodes[report.MakeProcessNodeID("", "2")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 2 bash")
|
||||
}
|
||||
if name, ok := node.Latest.Lookup(process.Name); !ok || name != processes[1].Name {
|
||||
t.Errorf("Expected %q got %q", processes[1].Name, name)
|
||||
}
|
||||
if ppid, ok := node.Latest.Lookup(process.PPID); !ok || ppid != fmt.Sprint(processes[1].PPID) {
|
||||
t.Errorf("Expected %d got %q", processes[1].PPID, ppid)
|
||||
}
|
||||
if memoryUsage, ok := node.Metrics[process.MemoryUsage]; !ok {
|
||||
t.Errorf("Expected memory usage metric, but not found")
|
||||
} else if sample, ok := memoryUsage.LastSample(); !ok {
|
||||
t.Errorf("Expected memory usage metric to have a sample, but there were none")
|
||||
} else if sample.Value != 0. {
|
||||
t.Errorf("Expected memory usage metric sample %f, got %f", 0., sample.Value)
|
||||
}
|
||||
}
|
||||
|
||||
testReporter(t, false, test)
|
||||
}
|
||||
|
||||
func TestThreadCounts(t *testing.T) {
|
||||
test := func(rpt report.Report) {
|
||||
node, ok := rpt.Process.Nodes[report.MakeProcessNodeID("", "3")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 3 apache")
|
||||
}
|
||||
if threads, ok := node.Latest.Lookup(process.Threads); !ok || threads != fmt.Sprint(processes[2].Threads) {
|
||||
t.Errorf("Expected %d got %q", processes[2].Threads, threads)
|
||||
}
|
||||
}
|
||||
testReporter(t, false, test)
|
||||
}
|
||||
|
||||
func TestCmdline(t *testing.T) {
|
||||
test := func(rpt report.Report) {
|
||||
node, ok := rpt.Process.Nodes[report.MakeProcessNodeID("", "4")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 4 ping")
|
||||
}
|
||||
if cmdline, ok := node.Latest.Lookup(process.Cmdline); !ok || cmdline != fmt.Sprint(processes[3].Cmdline) {
|
||||
t.Errorf("Expected %q got %q", processes[3].Cmdline, cmdline)
|
||||
}
|
||||
}
|
||||
testReporter(t, false, test)
|
||||
}
|
||||
|
||||
func TestAnonymous(t *testing.T) {
|
||||
test := func(rpt report.Report) {
|
||||
node, ok := rpt.Process.Nodes[report.MakeProcessNodeID("", "5")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 5 tail")
|
||||
}
|
||||
if name, ok := node.Latest.Lookup(process.Name); ok {
|
||||
t.Errorf("Expected no name, but got %q", name)
|
||||
}
|
||||
if cmdline, ok := node.Latest.Lookup(process.Cmdline); !ok || cmdline != fmt.Sprint(processes[4].Cmdline) {
|
||||
t.Errorf("Expected %q got %q", processes[4].Cmdline, cmdline)
|
||||
}
|
||||
}
|
||||
testReporter(t, false, test)
|
||||
}
|
||||
|
||||
func TestCmdlineRemoval(t *testing.T) {
|
||||
test := func(rpt report.Report) {
|
||||
node, ok := rpt.Process.Nodes[report.MakeProcessNodeID("", "4")]
|
||||
if !ok {
|
||||
t.Errorf("Expected report to include the pid 4 ping")
|
||||
}
|
||||
if cmdline, ok := node.Latest.Lookup(process.Cmdline); !ok || cmdline != fmt.Sprint("ping") {
|
||||
t.Errorf("Expected %q got %q", "ping", cmdline)
|
||||
}
|
||||
}
|
||||
testReporter(t, true, test)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user