Procstat: don't cache PIDs (#2206)

* Procstat: don't cache PIDs

Changed the procstat input plugin to not cache PIDs. Solves #1636.
The logic of creating a process by pid was moved from `procstat.go` to
`spec_processor.go`.

* Procstat: go fmt

* procstat: modify changelog for #2206
This commit is contained in:
Yaron de Leeuw
2017-02-02 09:12:22 -05:00
committed by Cameron Sparr
parent 55d3f70771
commit 0ce44648cf
4 changed files with 40 additions and 61 deletions

View File

@@ -1,6 +1,7 @@
package procstat
import (
"fmt"
"time"
"github.com/shirou/gopsutil/process"
@@ -9,12 +10,13 @@ import (
)
type SpecProcessor struct {
Prefix string
pid int32
tags map[string]string
fields map[string]interface{}
acc telegraf.Accumulator
proc *process.Process
ProcessName string
Prefix string
pid int32
tags map[string]string
fields map[string]interface{}
acc telegraf.Accumulator
proc *process.Process
}
func NewSpecProcessor(
@@ -22,29 +24,35 @@ func NewSpecProcessor(
prefix string,
pid int32,
acc telegraf.Accumulator,
p *process.Process,
tags map[string]string,
) *SpecProcessor {
if processName != "" {
tags["process_name"] = processName
} else {
name, err := p.Name()
if err == nil {
tags["process_name"] = name
}
}
return &SpecProcessor{
Prefix: prefix,
pid: pid,
tags: tags,
fields: make(map[string]interface{}),
acc: acc,
proc: p,
ProcessName: processName,
Prefix: prefix,
pid: pid,
tags: tags,
fields: make(map[string]interface{}),
acc: acc,
}
}
func (p *SpecProcessor) pushMetrics() {
func (p *SpecProcessor) pushMetrics() error {
var prefix string
proc, err := process.NewProcess(p.pid)
if err != nil {
return fmt.Errorf("Failed to open process with pid '%d'. Error: '%s'",
p.pid, err)
}
p.proc = proc
if p.ProcessName != "" {
p.tags["process_name"] = p.ProcessName
} else {
name, err := p.proc.Name()
if err == nil {
p.tags["process_name"] = name
}
}
if p.Prefix != "" {
prefix = p.Prefix + "_"
}
@@ -107,4 +115,5 @@ func (p *SpecProcessor) pushMetrics() {
}
p.acc.AddFields("procstat", fields, p.tags)
return nil
}