Skip to content

Commit

Permalink
Procstat input plugin should use the same timestamp in all metrics in…
Browse files Browse the repository at this point in the history
… the same Gather() cycle. (#8658)

(cherry picked from commit 70d2b1f)
  • Loading branch information
ivorybilled authored and ssoroka committed Jan 27, 2021
1 parent d9d221f commit 98e6bdc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
12 changes: 7 additions & 5 deletions plugins/inputs/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
}

pids, tags, err := p.findPids(acc)
now := time.Now()

if err != nil {
fields := map[string]interface{}{
"pid_count": 0,
Expand All @@ -128,7 +130,7 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
"pid_finder": p.PidFinder,
"result": "lookup_error",
}
acc.AddFields("procstat_lookup", fields, tags)
acc.AddFields("procstat_lookup", fields, tags, now)
return err
}

Expand All @@ -140,7 +142,7 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
p.procs = procs

for _, proc := range p.procs {
p.addMetric(proc, acc)
p.addMetric(proc, acc, now)
}

fields := map[string]interface{}{
Expand All @@ -150,13 +152,13 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
}
tags["pid_finder"] = p.PidFinder
tags["result"] = "success"
acc.AddFields("procstat_lookup", fields, tags)
acc.AddFields("procstat_lookup", fields, tags, now)

return nil
}

// Add metrics a single Process
func (p *Procstat) addMetric(proc Process, acc telegraf.Accumulator) {
func (p *Procstat) addMetric(proc Process, acc telegraf.Accumulator, t time.Time) {
var prefix string
if p.Prefix != "" {
prefix = p.Prefix + "_"
Expand Down Expand Up @@ -309,7 +311,7 @@ func (p *Procstat) addMetric(proc Process, acc telegraf.Accumulator) {
}
}

acc.AddFields("procstat", fields, proc.Tags())
acc.AddFields("procstat", fields, proc.Tags(), t)
}

// Update monitored Processes
Expand Down
17 changes: 17 additions & 0 deletions plugins/inputs/procstat/procstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,20 @@ func TestProcstatLookupMetric(t *testing.T) {
require.NoError(t, err)
require.Equal(t, len(p.procs)+1, len(acc.Metrics))
}

func TestGather_SameTimestamps(t *testing.T) {
var acc testutil.Accumulator
pidfile := "/path/to/pidfile"

p := Procstat{
PidFile: pidfile,
createPIDFinder: pidFinder([]PID{pid}, nil),
createProcess: newTestProc,
}
require.NoError(t, acc.GatherError(p.Gather))

procstat, _ := acc.Get("procstat")
procstat_lookup, _ := acc.Get("procstat_lookup")

require.Equal(t, procstat.Time, procstat_lookup.Time)
}

0 comments on commit 98e6bdc

Please sign in to comment.