chore(inputs.procstat): Convert bool tagging settings to a list (#14333)

This commit is contained in:
Sven Rebhan 2023-12-01 00:23:35 +01:00 committed by GitHub
parent 761dbce964
commit ebffaa3916
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 396 additions and 160 deletions

View File

@ -57,6 +57,63 @@ func migrate(tbl *ast.Table) ([]byte, string, error) {
delete(plugin, "supervisor_unit") delete(plugin, "supervisor_unit")
} }
// The tagging options both need the 'tag_with' setting
var tagwith []string
newTagWith, found := plugin["tag_with"]
if found {
ntw, ok := newTagWith.([]interface{})
if !ok {
return nil, "", fmt.Errorf("setting 'tag_with' has wrong type %T", newTagWith)
}
for _, raw := range ntw {
s, ok := raw.(string)
if !ok {
return nil, "", fmt.Errorf("setting 'tag_with' contains wrong type %T", raw)
}
if !choice.Contains(s, tagwith) {
tagwith = append(tagwith, s)
}
}
}
// Tagging with PID
if oldPidTag, found := plugin["pid_tag"]; found {
applied = true
pt, ok := oldPidTag.(bool)
if !ok {
return nil, "", fmt.Errorf("setting 'pid_tag' has wrong type %T", oldPidTag)
}
// Add the pid-tagging to 'tag_with' if requested
if pt && !choice.Contains("pid", tagwith) {
tagwith = append(tagwith, "pid")
plugin["tag_with"] = tagwith
}
// Remove deprecated setting
delete(plugin, "pid_tag")
}
// Tagging with command-line
if oldCmdlinedTag, found := plugin["cmdline_tag"]; found {
applied = true
ct, ok := oldCmdlinedTag.(bool)
if !ok {
return nil, "", fmt.Errorf("setting 'cmdline_tag' has wrong type %T", oldCmdlinedTag)
}
// Add the pid-tagging to 'tag_with' if requested
if ct && !choice.Contains("cmdline", tagwith) {
tagwith = append(tagwith, "cmdline")
plugin["tag_with"] = tagwith
}
// Remove deprecated setting
delete(plugin, "cmdline_tag")
}
// No options migrated so we can exit early // No options migrated so we can exit early
if !applied { if !applied {
return nil, "", migrations.ErrNotApplicable return nil, "", migrations.ErrNotApplicable

View File

@ -9,99 +9,12 @@ import (
"github.com/influxdata/telegraf/config" "github.com/influxdata/telegraf/config"
_ "github.com/influxdata/telegraf/migrations/inputs_procstat" // register migration _ "github.com/influxdata/telegraf/migrations/inputs_procstat" // register migration
_ "github.com/influxdata/telegraf/plugins/inputs/procstat" // register plugin "github.com/influxdata/telegraf/plugins/inputs/procstat" // register plugin
) )
func TestNoMigration(t *testing.T) { func TestNoMigration(t *testing.T) {
defaultCfg := []byte(` plugin := procstat.Procstat{}
# Read metrics from MQTT topic(s) defaultCfg := []byte(plugin.SampleConfig())
[[inputs.mqtt_consumer]]
## Broker URLs for the MQTT server or cluster. To connect to multiple
## clusters or standalone servers, use a separate plugin instance.
## example: servers = ["tcp://localhost:1883"]
## servers = ["ssl://localhost:1883"]
## servers = ["ws://localhost:1883"]
servers = ["tcp://127.0.0.1:1883"]
## Topics that will be subscribed to.
topics = [
"telegraf/host01/cpu",
"telegraf/+/mem",
"sensors/#",
]
## The message topic will be stored in a tag specified by this value. If set
## to the empty string no topic tag will be created.
# topic_tag = "topic"
## QoS policy for messages
## 0 = at most once
## 1 = at least once
## 2 = exactly once
##
## When using a QoS of 1 or 2, you should enable persistent_session to allow
## resuming unacknowledged messages.
# qos = 0
## Connection timeout for initial connection in seconds
# connection_timeout = "30s"
## Max undelivered messages
## This plugin uses tracking metrics, which ensure messages are read to
## outputs before acknowledging them to the original broker to ensure data
## is not lost. This option sets the maximum messages to read from the
## broker that have not been written by an output.
##
## This value needs to be picked with awareness of the agent's
## metric_batch_size value as well. Setting max undelivered messages too high
## can result in a constant stream of data batches to the output. While
## setting it too low may never flush the broker's messages.
# max_undelivered_messages = 1000
## Persistent session disables clearing of the client session on connection.
## In order for this option to work you must also set client_id to identify
## the client. To receive messages that arrived while the client is offline,
## also set the qos option to 1 or 2 and don't forget to also set the QoS when
## publishing.
# persistent_session = false
## If unset, a random client ID will be generated.
# client_id = ""
## Username and password to connect MQTT server.
# username = "telegraf"
# password = "metricsmetricsmetricsmetrics"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
## Client trace messages
## When set to true, and debug mode enabled in the agent settings, the MQTT
## client's messages are included in telegraf logs. These messages are very
## noisey, but essential for debugging issues.
# client_trace = false
## Data format to consume.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
data_format = "influx"
## Enable extracting tag values from MQTT topics
## _ denotes an ignored entry in the topic path
# [[inputs.mqtt_consumer.topic_parsing]]
# topic = ""
# measurement = ""
# tags = ""
# fields = ""
## Value supported is int, float, unit
# [[inputs.mqtt_consumer.topic.types]]
# key = type
`)
// Migrate and check that nothing changed // Migrate and check that nothing changed
output, n, err := config.ApplyMigrations(defaultCfg) output, n, err := config.ApplyMigrations(defaultCfg)

View File

@ -0,0 +1,3 @@
[[inputs.procstat]]
pid_file = "/var/run/nginx.pid"
supervisor_units = ["webserver", "proxy"]

View File

@ -0,0 +1,47 @@
# Monitor process cpu and memory usage
[[inputs.procstat]]
## PID file to monitor process
pid_file = "/var/run/nginx.pid"
## executable name (ie, pgrep <exe>)
# exe = "nginx"
## pattern as argument for pgrep (ie, pgrep -f <pattern>)
# pattern = "nginx"
## user as argument for pgrep (ie, pgrep -u <user>)
# user = "nginx"
## Systemd unit name, supports globs when include_systemd_children is set to true
# systemd_unit = "nginx.service"
# include_systemd_children = false
## CGroup name or path, supports globs
# cgroup = "systemd/system.slice/nginx.service"
## Supervisor service names of hypervisorctl management
supervisor_unit = ["webserver", "proxy"]
## Windows service name
# win_service = ""
## override for process_name
## This is optional; default is sourced from /proc/<pid>/status
# process_name = "bar"
## Field name prefix
# prefix = ""
## When true add the full cmdline as a tag.
cmdline_tag = false
## Mode to use when calculating CPU usage. Can be one of 'solaris' or 'irix'.
# mode = "irix"
## Add the PID as a tag instead of as a field. When collecting multiple
## processes with otherwise matching tags this setting should be enabled to
## ensure each process has a unique identity.
##
## Enabling this option may result in a large number of series, especially
## when processes have a short lifetime.
pid_tag = false
## Method to use when finding process IDs. Can be one of 'pgrep', or
## 'native'. The pgrep finder calls the pgrep executable in the PATH while
## the native finder performs the search directly in a manor dependent on the
## platform. Default is 'pgrep'
# pid_finder = "pgrep"

View File

@ -0,0 +1,4 @@
[[inputs.procstat]]
pid_file = "/var/run/nginx.pid"
supervisor_units = ["webserver", "proxy"]
tag_with = ["pid", "cmdline"]

View File

@ -0,0 +1,47 @@
# Monitor process cpu and memory usage
[[inputs.procstat]]
## PID file to monitor process
pid_file = "/var/run/nginx.pid"
## executable name (ie, pgrep <exe>)
# exe = "nginx"
## pattern as argument for pgrep (ie, pgrep -f <pattern>)
# pattern = "nginx"
## user as argument for pgrep (ie, pgrep -u <user>)
# user = "nginx"
## Systemd unit name, supports globs when include_systemd_children is set to true
# systemd_unit = "nginx.service"
# include_systemd_children = false
## CGroup name or path, supports globs
# cgroup = "systemd/system.slice/nginx.service"
## Supervisor service names of hypervisorctl management
supervisor_unit = ["webserver", "proxy"]
## Windows service name
# win_service = ""
## override for process_name
## This is optional; default is sourced from /proc/<pid>/status
# process_name = "bar"
## Field name prefix
# prefix = ""
## When true add the full cmdline as a tag.
cmdline_tag = true
## Mode to use when calculating CPU usage. Can be one of 'solaris' or 'irix'.
# mode = "irix"
## Add the PID as a tag instead of as a field. When collecting multiple
## processes with otherwise matching tags this setting should be enabled to
## ensure each process has a unique identity.
##
## Enabling this option may result in a large number of series, especially
## when processes have a short lifetime.
pid_tag = true
## Method to use when finding process IDs. Can be one of 'pgrep', or
## 'native'. The pgrep finder calls the pgrep executable in the PATH while
## the native finder performs the search directly in a manor dependent on the
## platform. Default is 'pgrep'
# pid_finder = "pgrep"

View File

@ -0,0 +1,4 @@
[[inputs.procstat]]
pid_file = "/var/run/nginx.pid"
supervisor_units = ["webserver", "proxy"]
tag_with = ["ppid", "pid", "cmdline"]

View File

@ -0,0 +1,49 @@
# Monitor process cpu and memory usage
[[inputs.procstat]]
## PID file to monitor process
pid_file = "/var/run/nginx.pid"
## executable name (ie, pgrep <exe>)
# exe = "nginx"
## pattern as argument for pgrep (ie, pgrep -f <pattern>)
# pattern = "nginx"
## user as argument for pgrep (ie, pgrep -u <user>)
# user = "nginx"
## Systemd unit name, supports globs when include_systemd_children is set to true
# systemd_unit = "nginx.service"
# include_systemd_children = false
## CGroup name or path, supports globs
# cgroup = "systemd/system.slice/nginx.service"
## Supervisor service names of hypervisorctl management
supervisor_unit = ["webserver", "proxy"]
## Windows service name
# win_service = ""
## override for process_name
## This is optional; default is sourced from /proc/<pid>/status
# process_name = "bar"
## Field name prefix
# prefix = ""
## When true add the full cmdline as a tag.
cmdline_tag = true
## Mode to use when calculating CPU usage. Can be one of 'solaris' or 'irix'.
# mode = "irix"
## Add the PID as a tag instead of as a field. When collecting multiple
## processes with otherwise matching tags this setting should be enabled to
## ensure each process has a unique identity.
##
## Enabling this option may result in a large number of series, especially
## when processes have a short lifetime.
pid_tag = true
## Method to use when finding process IDs. Can be one of 'pgrep', or
## 'native'. The pgrep finder calls the pgrep executable in the PATH while
## the native finder performs the search directly in a manor dependent on the
## platform. Default is 'pgrep'
# pid_finder = "pgrep"
tag_with = ["ppid"]

View File

@ -0,0 +1,4 @@
[[inputs.procstat]]
pid_file = "/var/run/nginx.pid"
supervisor_units = ["webserver", "proxy"]
tag_with = ["ppid", "cmdline", "pid"]

View File

@ -0,0 +1,49 @@
# Monitor process cpu and memory usage
[[inputs.procstat]]
## PID file to monitor process
pid_file = "/var/run/nginx.pid"
## executable name (ie, pgrep <exe>)
# exe = "nginx"
## pattern as argument for pgrep (ie, pgrep -f <pattern>)
# pattern = "nginx"
## user as argument for pgrep (ie, pgrep -u <user>)
# user = "nginx"
## Systemd unit name, supports globs when include_systemd_children is set to true
# systemd_unit = "nginx.service"
# include_systemd_children = false
## CGroup name or path, supports globs
# cgroup = "systemd/system.slice/nginx.service"
## Supervisor service names of hypervisorctl management
supervisor_unit = ["webserver", "proxy"]
## Windows service name
# win_service = ""
## override for process_name
## This is optional; default is sourced from /proc/<pid>/status
# process_name = "bar"
## Field name prefix
# prefix = ""
## When true add the full cmdline as a tag.
cmdline_tag = true
## Mode to use when calculating CPU usage. Can be one of 'solaris' or 'irix'.
# mode = "irix"
## Add the PID as a tag instead of as a field. When collecting multiple
## processes with otherwise matching tags this setting should be enabled to
## ensure each process has a unique identity.
##
## Enabling this option may result in a large number of series, especially
## when processes have a short lifetime.
pid_tag = true
## Method to use when finding process IDs. Can be one of 'pgrep', or
## 'native'. The pgrep finder calls the pgrep executable in the PATH while
## the native finder performs the search directly in a manor dependent on the
## platform. Default is 'pgrep'
# pid_finder = "pgrep"
tag_with = ["ppid", "cmdline"]

View File

@ -55,19 +55,22 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## Field name prefix ## Field name prefix
# prefix = "" # prefix = ""
## When true add the full cmdline as a tag.
# cmdline_tag = false
## Mode to use when calculating CPU usage. Can be one of 'solaris' or 'irix'. ## Mode to use when calculating CPU usage. Can be one of 'solaris' or 'irix'.
# mode = "irix" # mode = "irix"
## Add the PID as a tag instead of as a field. When collecting multiple ## Add the given information tag instead of a field
## processes with otherwise matching tags this setting should be enabled to ## This allows to create unique metrics/series when collecting processes with
## ensure each process has a unique identity. ## otherwise identical tags. However, please be careful as this can easily
## ## result in a large number of series, especially with short-lived processes,
## Enabling this option may result in a large number of series, especially ## creating high cardinality at the output.
## when processes have a short lifetime. ## Available options are:
# pid_tag = false ## cmdline -- full commandline
## pid -- ID of the process
## ppid -- ID of the process' parent
## status -- state of the process
## user -- username owning the process
# tag_with = []
## Method to use when finding process IDs. Can be one of 'pgrep', or ## Method to use when finding process IDs. Can be one of 'pgrep', or
## 'native'. The pgrep finder calls the pgrep executable in the PATH while ## 'native'. The pgrep finder calls the pgrep executable in the PATH while

View File

@ -3,6 +3,7 @@ package procstat
import ( import (
"fmt" "fmt"
"runtime" "runtime"
"strconv"
"time" "time"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
@ -15,7 +16,7 @@ type Process interface {
Name() (string, error) Name() (string, error)
SetTag(string, string) SetTag(string, string)
MemoryMaps(bool) (*[]process.MemoryMapsStat, error) MemoryMaps(bool) (*[]process.MemoryMapsStat, error)
Metric(prefix string, cmdLineTag, solarisMode bool) telegraf.Metric Metric(prefix string, tagging map[string]bool, solarisMode bool) telegraf.Metric
} }
type PIDFinder interface { type PIDFinder interface {
@ -64,42 +65,12 @@ func (p *Proc) percent(_ time.Duration) (float64, error) {
} }
// Add metrics a single Process // Add metrics a single Process
func (p *Proc) Metric(prefix string, cmdLineTag, solarisMode bool) telegraf.Metric { func (p *Proc) Metric(prefix string, tagging map[string]bool, solarisMode bool) telegraf.Metric {
if prefix != "" { if prefix != "" {
prefix += "_" prefix += "_"
} }
fields := make(map[string]interface{}) fields := make(map[string]interface{})
if _, nameInTags := p.tags["process_name"]; !nameInTags {
name, err := p.Name()
if err == nil {
p.tags["process_name"] = name
}
}
if _, ok := p.tags["user"]; !ok {
user, err := p.Username()
if err == nil {
p.tags["user"] = user
}
}
// If pid is not present as a tag, include it as a field.
if _, pidInTags := p.tags["pid"]; !pidInTags {
fields["pid"] = p.Pid
}
// Add the command line as a tag if the option is set
if cmdLineTag {
if _, ok := p.tags["cmdline"]; !ok {
cmdline, err := p.Cmdline()
if err == nil {
p.tags["cmdline"] = cmdline
}
}
}
numThreads, err := p.NumThreads() numThreads, err := p.NumThreads()
if err == nil { if err == nil {
fields[prefix+"num_threads"] = numThreads fields[prefix+"num_threads"] = numThreads
@ -206,14 +177,54 @@ func (p *Proc) Metric(prefix string, cmdLineTag, solarisMode bool) telegraf.Metr
} }
} }
// Add the tags as requested by the user
cmdline, err := p.Cmdline()
if err == nil {
if tagging["cmdline"] {
p.tags["cmdline"] = cmdline
} else {
fields[prefix+"cmdline"] = cmdline
}
}
if tagging["pid"] {
p.tags["pid"] = strconv.Itoa(int(p.Pid))
} else {
fields["pid"] = p.Pid
}
ppid, err := p.Ppid() ppid, err := p.Ppid()
if err == nil { if err == nil {
fields[prefix+"ppid"] = ppid if tagging["ppid"] {
p.tags["ppid"] = strconv.Itoa(int(ppid))
} else {
fields[prefix+"ppid"] = ppid
}
} }
status, err := p.Status() status, err := p.Status()
if err == nil { if err == nil {
fields[prefix+"status"] = status[0] if tagging["status"] {
p.tags["status"] = status[0]
} else {
fields[prefix+"status"] = status[0]
}
}
user, err := p.Username()
if err == nil {
if tagging["user"] {
p.tags["user"] = user
} else {
fields[prefix+"user"] = user
}
}
if _, exists := p.tags["process_name"]; !exists {
name, err := p.Name()
if err == nil {
p.tags["process_name"] = name
}
} }
return metric.New("procstat", p.tags, fields, time.Time{}) return metric.New("procstat", p.tags, fields, time.Time{})

View File

@ -24,6 +24,7 @@ var sampleConfig string
// execCommand is so tests can mock out exec.Command usage. // execCommand is so tests can mock out exec.Command usage.
var execCommand = exec.Command var execCommand = exec.Command
var availableTags = []string{"cmdline", "pid", "ppid", "status", "user"}
type PID int32 type PID int32
@ -33,7 +34,7 @@ type Procstat struct {
Exe string `toml:"exe"` Exe string `toml:"exe"`
Pattern string `toml:"pattern"` Pattern string `toml:"pattern"`
Prefix string `toml:"prefix"` Prefix string `toml:"prefix"`
CmdLineTag bool `toml:"cmdline_tag"` CmdLineTag bool `toml:"cmdline_tag" deprecated:"1.29.0;use 'tag_with' instead"`
ProcessName string `toml:"process_name"` ProcessName string `toml:"process_name"`
User string `toml:"user"` User string `toml:"user"`
SystemdUnits string `toml:"systemd_units"` SystemdUnits string `toml:"systemd_units"`
@ -41,14 +42,16 @@ type Procstat struct {
SupervisorUnits []string `toml:"supervisor_units"` SupervisorUnits []string `toml:"supervisor_units"`
IncludeSystemdChildren bool `toml:"include_systemd_children"` IncludeSystemdChildren bool `toml:"include_systemd_children"`
CGroup string `toml:"cgroup"` CGroup string `toml:"cgroup"`
PidTag bool `toml:"pid_tag"` PidTag bool `toml:"pid_tag" deprecated:"1.29.0;use 'tag_with' instead"`
WinService string `toml:"win_service"` WinService string `toml:"win_service"`
Mode string `toml:"mode"` Mode string `toml:"mode"`
TagWith []string `toml:"tag_with"`
Log telegraf.Logger `toml:"-"` Log telegraf.Logger `toml:"-"`
solarisMode bool solarisMode bool
finder PIDFinder finder PIDFinder
processes map[PID]Process processes map[PID]Process
tagging map[string]bool
createProcess func(PID) (Process, error) createProcess func(PID) (Process, error)
} }
@ -66,6 +69,23 @@ func (p *Procstat) Init() error {
// Check solaris mode // Check solaris mode
p.solarisMode = strings.ToLower(p.Mode) == "solaris" p.solarisMode = strings.ToLower(p.Mode) == "solaris"
// Keep the old settings for compatibility
if p.PidTag && !choice.Contains("pid", p.TagWith) {
p.TagWith = append(p.TagWith, "pid")
}
if p.CmdLineTag && !choice.Contains("cmdline", p.TagWith) {
p.TagWith = append(p.TagWith, "cmdline")
}
// Check tagging and setup LUT
if err := choice.CheckSlice(p.TagWith, availableTags); err != nil {
return fmt.Errorf("invalid tag_with setting: %w", err)
}
p.tagging = make(map[string]bool, len(p.TagWith))
for _, tag := range p.TagWith {
p.tagging[tag] = true
}
// Keep the old settings for compatibility // Keep the old settings for compatibility
for _, u := range p.SupervisorUnit { for _, u := range p.SupervisorUnit {
if !choice.Contains(u, p.SupervisorUnits) { if !choice.Contains(u, p.SupervisorUnits) {
@ -97,7 +117,7 @@ func (p *Procstat) Init() error {
// see https://github.com/shirou/gopsutil/blob/v3.23.10/process/process_darwin.go#L235 // see https://github.com/shirou/gopsutil/blob/v3.23.10/process/process_darwin.go#L235
requiresChildren := len(p.SupervisorUnits) > 0 && p.Pattern != "" requiresChildren := len(p.SupervisorUnits) > 0 && p.Pattern != ""
if requiresChildren && runtime.GOOS == "darwin" { if requiresChildren && runtime.GOOS == "darwin" {
return errors.New("configuration requires the 'pgrep' finder on you OS") return errors.New("configuration requires 'pgrep' finder on your OS")
} }
p.finder = &NativeFinder{} p.finder = &NativeFinder{}
case "test": case "test":
@ -159,17 +179,13 @@ func (p *Procstat) Gather(acc telegraf.Accumulator) error {
proc.SetTag(k, v) proc.SetTag(k, v)
} }
// Add pid tag if needed
if p.PidTag {
proc.SetTag("pid", strconv.Itoa(int(pid)))
}
if p.ProcessName != "" { if p.ProcessName != "" {
proc.SetTag("process_name", p.ProcessName) proc.SetTag("process_name", p.ProcessName)
} }
p.processes[pid] = proc p.processes[pid] = proc
} }
running[pid] = true running[pid] = true
m := proc.Metric(p.Prefix, p.CmdLineTag, p.solarisMode) m := proc.Metric(p.Prefix, p.tagging, p.solarisMode)
m.SetTime(now) m.SetTime(now)
acc.AddMetric(m) acc.AddMetric(m)
} }

View File

@ -6,6 +6,7 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
@ -116,8 +117,9 @@ type testProc struct {
tags map[string]string tags map[string]string
} }
func newTestProc(_ PID) (Process, error) { func newTestProc(pid PID) (Process, error) {
proc := &testProc{ proc := &testProc{
pid: pid,
tags: make(map[string]string), tags: make(map[string]string),
} }
return proc, nil return proc, nil
@ -139,14 +141,12 @@ func (p *testProc) MemoryMaps(bool) (*[]process.MemoryMapsStat, error) {
return &[]process.MemoryMapsStat{}, nil return &[]process.MemoryMapsStat{}, nil
} }
func (p *testProc) Metric(prefix string, cmdLineTag, _ bool) telegraf.Metric { func (p *testProc) Metric(prefix string, tagging map[string]bool, _ bool) telegraf.Metric {
if prefix != "" { if prefix != "" {
prefix += "_" prefix += "_"
} }
fields := map[string]interface{}{ fields := map[string]interface{}{
"pid": int32(p.pid),
"ppid": int32(0),
prefix + "num_fds": int32(0), prefix + "num_fds": int32(0),
prefix + "num_threads": int32(0), prefix + "num_threads": int32(0),
prefix + "voluntary_context_switches": int64(0), prefix + "voluntary_context_switches": int64(0),
@ -167,20 +167,46 @@ func (p *testProc) Metric(prefix string, cmdLineTag, _ bool) telegraf.Metric {
prefix + "memory_rss": uint64(0), prefix + "memory_rss": uint64(0),
prefix + "memory_vms": uint64(0), prefix + "memory_vms": uint64(0),
prefix + "memory_usage": float32(0), prefix + "memory_usage": float32(0),
prefix + "status": "running",
} }
tags := map[string]string{ tags := map[string]string{
"process_name": "test_proc", "process_name": "test_proc",
"user": "testuser",
} }
for k, v := range p.tags { for k, v := range p.tags {
tags[k] = v tags[k] = v
} }
if cmdLineTag { // Add the tags as requested by the user
if tagging["cmdline"] {
tags["cmdline"] = "test_proc" tags["cmdline"] = "test_proc"
} else {
fields[prefix+"cmdline"] = "test_proc"
} }
if tagging["pid"] {
tags["pid"] = strconv.Itoa(int(p.pid))
} else {
fields["pid"] = int32(p.pid)
}
if tagging["ppid"] {
tags["ppid"] = "0"
} else {
fields[prefix+"ppid"] = int32(0)
}
if tagging["status"] {
tags["status"] = "running"
} else {
fields[prefix+"status"] = "running"
}
if tagging["user"] {
tags["user"] = "testuser"
} else {
fields[prefix+"user"] = "testuser"
}
return metric.New("procstat", tags, fields, time.Time{}) return metric.New("procstat", tags, fields, time.Time{})
} }
@ -207,7 +233,7 @@ func TestInitRequiresChildDarwin(t *testing.T) {
PidFinder: "native", PidFinder: "native",
Log: testutil.Logger{}, Log: testutil.Logger{},
} }
require.ErrorContains(t, p.Init(), "requires the 'pgrep' finder") require.ErrorContains(t, p.Init(), "requires 'pgrep' finder")
} }
func TestInitMissingPidMethod(t *testing.T) { func TestInitMissingPidMethod(t *testing.T) {

View File

@ -26,19 +26,22 @@
## Field name prefix ## Field name prefix
# prefix = "" # prefix = ""
## When true add the full cmdline as a tag.
# cmdline_tag = false
## Mode to use when calculating CPU usage. Can be one of 'solaris' or 'irix'. ## Mode to use when calculating CPU usage. Can be one of 'solaris' or 'irix'.
# mode = "irix" # mode = "irix"
## Add the PID as a tag instead of as a field. When collecting multiple ## Add the given information tag instead of a field
## processes with otherwise matching tags this setting should be enabled to ## This allows to create unique metrics/series when collecting processes with
## ensure each process has a unique identity. ## otherwise identical tags. However, please be careful as this can easily
## ## result in a large number of series, especially with short-lived processes,
## Enabling this option may result in a large number of series, especially ## creating high cardinality at the output.
## when processes have a short lifetime. ## Available options are:
# pid_tag = false ## cmdline -- full commandline
## pid -- ID of the process
## ppid -- ID of the process' parent
## status -- state of the process
## user -- username owning the process
# tag_with = []
## Method to use when finding process IDs. Can be one of 'pgrep', or ## Method to use when finding process IDs. Can be one of 'pgrep', or
## 'native'. The pgrep finder calls the pgrep executable in the PATH while ## 'native'. The pgrep finder calls the pgrep executable in the PATH while