process: Add flag to control the export of the process cmdline (#2153)

This commit is contained in:
Jan-Otto Kröpke
2025-08-03 20:09:03 +02:00
committed by GitHub
parent 6c2380bd04
commit 6253bf812d
2 changed files with 28 additions and 10 deletions

View File

@@ -42,6 +42,11 @@ Disabled by default, and can be enabled with `--collector.process.iis`. NOTE: Ju
Version of the process collector to use. 1 for Process V1, 2 for Process V2. Version of the process collector to use. 1 for Process V1, 2 for Process V2.
Defaults to 0 which will use the latest version available. Defaults to 0 which will use the latest version available.
### `--collector.process.cmdline`
Enables the `cmdline` label for the process metrics.
This label contains the command line used to start the process.
Enabled by default, and can be turned off with `--no-collector.process.cmdline`.
### Example ### Example
To match all firefox processes: `--collector.process.include="firefox.*"`. To match all firefox processes: `--collector.process.include="firefox.*"`.

View File

@@ -44,6 +44,7 @@ type Config struct {
ProcessInclude *regexp.Regexp `yaml:"include"` ProcessInclude *regexp.Regexp `yaml:"include"`
ProcessExclude *regexp.Regexp `yaml:"exclude"` ProcessExclude *regexp.Regexp `yaml:"exclude"`
EnableWorkerProcess bool `yaml:"iis"` EnableWorkerProcess bool `yaml:"iis"`
EnableCMDLine bool `yaml:"cmdline"`
CounterVersion uint8 `yaml:"counter-version"` CounterVersion uint8 `yaml:"counter-version"`
} }
@@ -52,6 +53,7 @@ var ConfigDefaults = Config{
ProcessInclude: types.RegExpAny, ProcessInclude: types.RegExpAny,
ProcessExclude: types.RegExpEmpty, ProcessExclude: types.RegExpEmpty,
EnableWorkerProcess: false, EnableWorkerProcess: false,
EnableCMDLine: true,
CounterVersion: 0, CounterVersion: 0,
} }
@@ -131,6 +133,11 @@ func NewWithFlags(app *kingpin.Application) *Collector {
"Enable IIS collectWorker process name queries. May cause the collector to leak memory.", "Enable IIS collectWorker process name queries. May cause the collector to leak memory.",
).Default(strconv.FormatBool(c.config.EnableWorkerProcess)).BoolVar(&c.config.EnableWorkerProcess) ).Default(strconv.FormatBool(c.config.EnableWorkerProcess)).BoolVar(&c.config.EnableWorkerProcess)
app.Flag(
"collector.process.cmdline",
"If enabled, the full cmdline is exposed to the windows_process_info metrics.",
).Default(strconv.FormatBool(c.config.EnableCMDLine)).BoolVar(&c.config.EnableCMDLine)
app.Flag( app.Flag(
"collector.process.counter-version", "collector.process.counter-version",
"Version of the process collector to use. 1 for Process V1, 2 for Process V2. Defaults to 0 which will use the latest version available.", "Version of the process collector to use. 1 for Process V1, 2 for Process V2. Defaults to 0 which will use the latest version available.",
@@ -415,19 +422,25 @@ func (c *Collector) getExtendedProcessInformation(hProcess windows.Handle) (stri
return "", 0, fmt.Errorf("failed to read process memory: %w", err) return "", 0, fmt.Errorf("failed to read process memory: %w", err)
} }
cmdLineUTF16 := make([]uint16, processParameters.CommandLine.Length) var cmdLine string
err = windows.ReadProcessMemory(hProcess, if c.config.EnableCMDLine {
uintptr(unsafe.Pointer(processParameters.CommandLine.Buffer)), cmdLineUTF16 := make([]uint16, processParameters.CommandLine.Length)
(*byte)(unsafe.Pointer(&cmdLineUTF16[0])),
uintptr(processParameters.CommandLine.Length), err = windows.ReadProcessMemory(hProcess,
nil, uintptr(unsafe.Pointer(processParameters.CommandLine.Buffer)),
) (*byte)(unsafe.Pointer(&cmdLineUTF16[0])),
if err != nil { uintptr(processParameters.CommandLine.Length),
return "", processParameters.ProcessGroupId, fmt.Errorf("failed to read process memory: %w", err) nil,
)
if err != nil {
return "", processParameters.ProcessGroupId, fmt.Errorf("failed to read process memory: %w", err)
}
cmdLine = strings.TrimSpace(windows.UTF16ToString(cmdLineUTF16))
} }
return strings.TrimSpace(windows.UTF16ToString(cmdLineUTF16)), processParameters.ProcessGroupId, nil return cmdLine, processParameters.ProcessGroupId, nil
} }
func (c *Collector) getProcessOwner(logger *slog.Logger, hProcess windows.Handle) (string, error) { func (c *Collector) getProcessOwner(logger *slog.Logger, hProcess windows.Handle) (string, error) {