Skip to content

Commit

Permalink
Can turn pid into tag instead of field
Browse files Browse the repository at this point in the history
closes #1843
fixes  #1668
  • Loading branch information
rikaardhosein authored and sparrc committed Dec 13, 2016
1 parent cab1118 commit bf8e1b5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [#1949](https://github.com/influxdata/telegraf/issues/1949): Fix windows `net` plugin.
- [#1775](https://github.com/influxdata/telegraf/issues/1775): Cache & expire metrics for delivery to prometheus.
- [#2146](https://github.com/influxdata/telegraf/issues/2146): Fix potential panic in aggregator plugin metric maker.
- [#1843](https://github.com/influxdata/telegraf/pull/1843) & [#1668](https://github.com/influxdata/telegraf/issues/1668): Add optional ability to define PID as a tag.

## v1.1.2 [2016-12-12]

Expand Down
6 changes: 6 additions & 0 deletions plugins/inputs/procstat/procstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Procstat struct {
Prefix string
ProcessName string
User string
PidTag bool

// pidmap maps a pid to a process object, so we don't recreate every gather
pidmap map[int32]*process.Process
Expand Down Expand Up @@ -53,6 +54,8 @@ var sampleConfig = `
prefix = ""
## comment this out if you want raw cpu_time stats
fielddrop = ["cpu_time_*"]
## This is optional; moves pid into a tag instead of a field
pid_tag = false
`

func (_ *Procstat) SampleConfig() string {
Expand All @@ -70,6 +73,9 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
p.Exe, p.PidFile, p.Pattern, p.User, err.Error())
} else {
for pid, proc := range p.pidmap {
if p.PidTag {
p.tagmap[pid]["pid"] = fmt.Sprint(pid)
}
p := NewSpecProcessor(p.ProcessName, p.Prefix, pid, acc, proc, p.tagmap[pid])
p.pushMetrics()
}
Expand Down
7 changes: 6 additions & 1 deletion plugins/inputs/procstat/spec_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func (p *SpecProcessor) pushMetrics() {
if p.Prefix != "" {
prefix = p.Prefix + "_"
}
fields := map[string]interface{}{"pid": p.pid}
fields := map[string]interface{}{}

//If pid is not present as a tag, include it as a field.
if _, pidInTags := p.tags["pid"]; !pidInTags {
fields["pid"] = p.pid
}

numThreads, err := p.proc.NumThreads()
if err == nil {
Expand Down

0 comments on commit bf8e1b5

Please sign in to comment.