diff --git a/probe/process/reporter.go b/probe/process/reporter.go index 6fd3f55116..a200b54b4c 100644 --- a/probe/process/reporter.go +++ b/probe/process/reporter.go @@ -85,35 +85,34 @@ func (r *Reporter) processTopology() (report.Topology, error) { pidstr := strconv.Itoa(p.PID) nodeID := report.MakeProcessNodeID(r.scope, pidstr) node := report.MakeNode(nodeID) - for _, tuple := range []struct{ key, value string }{ - {PID, pidstr}, - {Name, p.Name}, - {Threads, strconv.Itoa(p.Threads)}, - } { - if tuple.value != "" { - node = node.WithLatests(map[string]string{tuple.key: tuple.value}) - } + node = node.WithLatest(PID, now, pidstr) + node = node.WithLatest(Threads, now, strconv.Itoa(p.Threads)) + if p.Name != "" { + node = node.WithLatest(Name, now, p.Name) } if p.Cmdline != "" { if r.noCommandLineArguments { - node = node.WithLatests(map[string]string{Cmdline: strings.Split(p.Cmdline, " ")[0]}) + node = node.WithLatest(Cmdline, now, strings.Split(p.Cmdline, " ")[0]) } else { - node = node.WithLatests(map[string]string{Cmdline: p.Cmdline}) + node = node.WithLatest(Cmdline, now, p.Cmdline) } } if p.PPID > 0 { - node = node.WithLatests(map[string]string{PPID: strconv.Itoa(p.PPID)}) + node = node.WithLatest(PPID, now, strconv.Itoa(p.PPID)) } + var metrics = report.Metrics{ + MemoryUsage: report.MakeSingletonMetric(now, float64(p.RSSBytes)).WithMax(float64(p.RSSBytesLimit)), + OpenFilesCount: report.MakeSingletonMetric(now, float64(p.OpenFilesCount)).WithMax(float64(p.OpenFilesLimit)), + } if deltaTotal > 0 { cpuUsage := float64(p.Jiffies-prev.Jiffies) / float64(deltaTotal) * 100. - node = node.WithMetric(CPUUsage, report.MakeSingletonMetric(now, cpuUsage).WithMax(maxCPU)) + metrics[CPUUsage] = report.MakeSingletonMetric(now, cpuUsage).WithMax(maxCPU) } - node = node.WithMetric(MemoryUsage, report.MakeSingletonMetric(now, float64(p.RSSBytes)).WithMax(float64(p.RSSBytesLimit))) - node = node.WithMetric(OpenFilesCount, report.MakeSingletonMetric(now, float64(p.OpenFilesCount)).WithMax(float64(p.OpenFilesLimit))) + node = node.WithMetrics(metrics) t.AddNode(node) }) diff --git a/probe/process/reporter_test.go b/probe/process/reporter_test.go index c741874e74..ea1c6da3ba 100644 --- a/probe/process/reporter_test.go +++ b/probe/process/reporter_test.go @@ -134,3 +134,17 @@ func TestCmdlineRemoval(t *testing.T) { } testReporter(t, true, test) } + +func BenchmarkReporter(t *testing.B) { + walker := &mockWalker{processes: processes} + getDeltaTotalJiffies := func() (uint64, float64, error) { return 0, 0., nil } + reporter := process.NewReporter(walker, "", getDeltaTotalJiffies, false) + t.ResetTimer() + + for i := 0; i < t.N; i++ { + _, err := reporter.Report() + if err != nil { + t.Error(err) + } + } +} diff --git a/report/controls.go b/report/controls.go index 6a9f06541a..6f151bfdc5 100644 --- a/report/controls.go +++ b/report/controls.go @@ -20,6 +20,12 @@ type Control struct { // Merge merges other with cs, returning a fresh Controls. func (cs Controls) Merge(other Controls) Controls { + if len(other) > len(cs) { + cs, other = other, cs + } + if len(other) == 0 { + return cs + } result := cs.Copy() for k, v := range other { result[k] = v diff --git a/report/metadata_template.go b/report/metadata_template.go index 054766fe5a..a6a4464633 100644 --- a/report/metadata_template.go +++ b/report/metadata_template.go @@ -121,12 +121,12 @@ func (e MetadataTemplates) Copy() MetadataTemplates { // Merge merges two sets of MetadataTemplates so far just ignores based // on duplicate id key func (e MetadataTemplates) Merge(other MetadataTemplates) MetadataTemplates { - if e == nil && other == nil { - return nil - } if len(other) > len(e) { e, other = other, e } + if len(other) == 0 { + return e + } result := e.Copy() for k, v := range other { if existing, ok := result[k]; !ok || existing.Priority < v.Priority { diff --git a/report/metric_template.go b/report/metric_template.go index 70381ba597..f33b4e4dfe 100644 --- a/report/metric_template.go +++ b/report/metric_template.go @@ -76,6 +76,9 @@ func (e MetricTemplates) Merge(other MetricTemplates) MetricTemplates { if len(other) > len(e) { e, other = other, e } + if len(other) == 0 { + return e + } result := e.Copy() for k, v := range other { if existing, ok := result[k]; !ok || existing.Priority < v.Priority { diff --git a/report/metrics.go b/report/metrics.go index 3e9025dae6..15ca6d21ba 100644 --- a/report/metrics.go +++ b/report/metrics.go @@ -22,6 +22,9 @@ func (m Metrics) Merge(other Metrics) Metrics { if len(other) > len(m) { m, other = other, m } + if len(other) == 0 { + return m + } result := m.Copy() for k, v := range other { if rv, ok := result[k]; ok { diff --git a/report/topology.go b/report/topology.go index df08220a70..d461f11ca4 100644 --- a/report/topology.go +++ b/report/topology.go @@ -177,6 +177,9 @@ func (n Nodes) Merge(other Nodes) Nodes { if len(other) > len(n) { n, other = other, n } + if len(other) == 0 { + return n + } cp := n.Copy() for k, v := range other { if n, ok := cp[k]; ok { // don't overwrite