Skip to content

Commit 6253bf8

Browse files
authored
process: Add flag to control the export of the process cmdline (#2153)
1 parent 6c2380b commit 6253bf8

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

docs/collector.process.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ Disabled by default, and can be enabled with `--collector.process.iis`. NOTE: Ju
4242
Version of the process collector to use. 1 for Process V1, 2 for Process V2.
4343
Defaults to 0 which will use the latest version available.
4444

45+
### `--collector.process.cmdline`
46+
47+
Enables the `cmdline` label for the process metrics.
48+
This label contains the command line used to start the process.
49+
Enabled by default, and can be turned off with `--no-collector.process.cmdline`.
4550

4651
### Example
4752
To match all firefox processes: `--collector.process.include="firefox.*"`.

internal/collector/process/process.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Config struct {
4444
ProcessInclude *regexp.Regexp `yaml:"include"`
4545
ProcessExclude *regexp.Regexp `yaml:"exclude"`
4646
EnableWorkerProcess bool `yaml:"iis"`
47+
EnableCMDLine bool `yaml:"cmdline"`
4748
CounterVersion uint8 `yaml:"counter-version"`
4849
}
4950

@@ -52,6 +53,7 @@ var ConfigDefaults = Config{
5253
ProcessInclude: types.RegExpAny,
5354
ProcessExclude: types.RegExpEmpty,
5455
EnableWorkerProcess: false,
56+
EnableCMDLine: true,
5557
CounterVersion: 0,
5658
}
5759

@@ -131,6 +133,11 @@ func NewWithFlags(app *kingpin.Application) *Collector {
131133
"Enable IIS collectWorker process name queries. May cause the collector to leak memory.",
132134
).Default(strconv.FormatBool(c.config.EnableWorkerProcess)).BoolVar(&c.config.EnableWorkerProcess)
133135

136+
app.Flag(
137+
"collector.process.cmdline",
138+
"If enabled, the full cmdline is exposed to the windows_process_info metrics.",
139+
).Default(strconv.FormatBool(c.config.EnableCMDLine)).BoolVar(&c.config.EnableCMDLine)
140+
134141
app.Flag(
135142
"collector.process.counter-version",
136143
"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
415422
return "", 0, fmt.Errorf("failed to read process memory: %w", err)
416423
}
417424

418-
cmdLineUTF16 := make([]uint16, processParameters.CommandLine.Length)
425+
var cmdLine string
419426

420-
err = windows.ReadProcessMemory(hProcess,
421-
uintptr(unsafe.Pointer(processParameters.CommandLine.Buffer)),
422-
(*byte)(unsafe.Pointer(&cmdLineUTF16[0])),
423-
uintptr(processParameters.CommandLine.Length),
424-
nil,
425-
)
426-
if err != nil {
427-
return "", processParameters.ProcessGroupId, fmt.Errorf("failed to read process memory: %w", err)
427+
if c.config.EnableCMDLine {
428+
cmdLineUTF16 := make([]uint16, processParameters.CommandLine.Length)
429+
430+
err = windows.ReadProcessMemory(hProcess,
431+
uintptr(unsafe.Pointer(processParameters.CommandLine.Buffer)),
432+
(*byte)(unsafe.Pointer(&cmdLineUTF16[0])),
433+
uintptr(processParameters.CommandLine.Length),
434+
nil,
435+
)
436+
if err != nil {
437+
return "", processParameters.ProcessGroupId, fmt.Errorf("failed to read process memory: %w", err)
438+
}
439+
440+
cmdLine = strings.TrimSpace(windows.UTF16ToString(cmdLineUTF16))
428441
}
429442

430-
return strings.TrimSpace(windows.UTF16ToString(cmdLineUTF16)), processParameters.ProcessGroupId, nil
443+
return cmdLine, processParameters.ProcessGroupId, nil
431444
}
432445

433446
func (c *Collector) getProcessOwner(logger *slog.Logger, hProcess windows.Handle) (string, error) {

0 commit comments

Comments
 (0)