Skip to content

[receiver/hostmetrics] Cheaper parent PID and number of threads retrieval on Windows #38589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Config struct {
Include MatchConfig `mapstructure:"include"`
Exclude MatchConfig `mapstructure:"exclude"`

// Exclude getting the parent process ID since it can be a costly operation on Windows.
ExcludeParentPid bool `mapstructure:"exclude_parent_pid,omitempty"`

// MuteProcessAllErrors is a flag that will mute all the errors encountered when trying to read metrics of a process.
// When this flag is enabled, there is no need to activate any other error suppression flags.
MuteProcessAllErrors bool `mapstructure:"mute_process_all_errors,omitempty"`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build windows

package processscraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"

import (
"context"
"fmt"
"unsafe"

"github.com/shirou/gopsutil/v4/process"
"golang.org/x/sys/windows"
)

func getProcessHandlesInternalNew(ctx context.Context) (processHandles, error) {
snap, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
return nil, fmt.Errorf("could not create snapshot: %w", err)
}
defer func() {
_ = windows.CloseHandle(snap)
}()

var pe32 windows.ProcessEntry32
pe32.Size = uint32(unsafe.Sizeof(pe32))
if err = windows.Process32First(snap, &pe32); err != nil {
return nil, fmt.Errorf("could not get first process: %w", err)
}

wrappedProcesses := make([]wrappedProcessHandle, 0, 64)
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
p, _ := process.NewProcess(int32(pe32.ProcessID))
if p != nil {
wrappedProcess := wrappedProcessHandle{
Process: p,
ppid: int32(pe32.ParentProcessID),
threads: int32(pe32.Threads),
}
wrappedProcesses = append(wrappedProcesses, wrappedProcess)
}
}

if err = windows.Process32Next(snap, &pe32); err != nil {
break
}
}

return &gopsProcessHandles{handles: wrappedProcesses}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func (p *gopsProcessHandles) Len() int {

type wrappedProcessHandle struct {
*process.Process
ppid int32
threads int32
}

func (p wrappedProcessHandle) CgroupWithContext(ctx context.Context) (string, error) {
Expand All @@ -131,6 +133,28 @@ func (p wrappedProcessHandle) CgroupWithContext(ctx context.Context) (string, er
return strings.TrimSuffix(string(contents), "\n"), nil
}

func (p wrappedProcessHandle) PpidWithContext(ctx context.Context) (int32, error) {
if p.ppid == -1 {
ppid, err := p.Process.PpidWithContext(ctx)
if err != nil {
return 0, err
}
p.ppid = ppid
}
return p.ppid, nil
}

func (p wrappedProcessHandle) NumThreadsWithContext(ctx context.Context) (int32, error) {
if p.threads == -1 {
threads, err := p.Process.NumThreadsWithContext(ctx)
if err != nil {
return 0, err
}
p.threads = threads
}
return p.threads, nil
}

// copied from gopsutil:
// GetEnvWithContext retrieves the environment variable key. If it does not exist it returns the default.
// The context may optionally contain a map superseding os.EnvKey.
Expand All @@ -157,7 +181,11 @@ func getProcessHandlesInternal(ctx context.Context) (processHandles, error) {
}
wrapped := make([]wrappedProcessHandle, len(processes))
for i, p := range processes {
wrapped[i] = wrappedProcessHandle{Process: p}
wrapped[i] = wrappedProcessHandle{
Process: p,
ppid: -1,
threads: -1,
}
}

return &gopsProcessHandles{handles: wrapped}, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build windows

package processscraper
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file could be removed when we remove the featureflag to fallback to the old behavior.


import (
"context"
"testing"

"go.opentelemetry.io/collector/scraper"
)

func BenchmarkGetProcessMetadata(b *testing.B) {
ctx := context.Background()
config := &Config{
MuteProcessExeError: true,
MuteProcessNameError: true,
ExcludeParentPid: true,
MuteProcessAllErrors: true, // Only way to pass the benchmark
}

scraper, err := newProcessScraper(scraper.Settings{}, config)
if err != nil {
b.Fatalf("Failed to create process scraper: %v", err)
}

benchmarks := []struct {
name string
getFunc func(context.Context) (processHandles, error)
excludeParentPid bool
}{
{
name: "Old-IncludeParentPid",
getFunc: getProcessHandlesInternal,
},
{
name: "New-IncludeParentPid",
getFunc: getProcessHandlesInternalNew,
},
{
name: "Old-ExcludeParentPid",
getFunc: getProcessHandlesInternal,
excludeParentPid: true,
},
{
name: "New-ExcludeParentPid",
getFunc: getProcessHandlesInternalNew,
excludeParentPid: true,
},
}

for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
scraper.getProcessHandles = bm.getFunc
scraper.config.ExcludeParentPid = bm.excludeParentPid

for i := 0; i < b.N; i++ {
_, err := scraper.getProcessMetadata(ctx)
if err != nil {
b.Fatalf("Failed to get process metadata: %v", err)
}
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (s *processScraper) getProcessMetadata(ctx context.Context) ([]*processMeta
}

command, err := getProcessCommand(ctx, handle)
if err != nil {
if err != nil && !s.config.MuteProcessAllErrors {
Copy link
Member

@dmitryax dmitryax Mar 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we add muting here but the related change on line 277 still unmuted? Maybe remove this from this PR to fix consistently as a separate bug_fix if needed? Just filing an issue is fine as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point that is a leftover from benchmarking...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errs.AddPartial(0, fmt.Errorf("error reading command for process %q (pid %v): %w", executable.name, pid, err))
}

Expand All @@ -271,14 +271,17 @@ func (s *processScraper) getProcessMetadata(ctx context.Context) ([]*processMeta
continue
}

parentPid, err := parentPid(ctx, handle, pid)
if err != nil {
errs.AddPartial(0, fmt.Errorf("error reading parent pid for process %q (pid %v): %w", executable.name, pid, err))
parentProcessID := int32(0)
if !s.config.ExcludeParentPid {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right that we don't need a specific config for this. I would like to still make the check conditional, but we can make it conditional on whether the process.parent_pid resource attribute is enabled or not.

While on Windows with the new process enumeration method getting ppid no longer costs anything, on other platforms it will still be the old style of doing a certain operation per-process. It's cheap on other platforms, but as a matter of principle I do think any work this scraper does should be conditional on whether the user actually enabled that attribute/metric.

parentProcessID, err = parentPid(ctx, handle, pid)
if err != nil {
errs.AddPartial(0, fmt.Errorf("error reading parent pid for process %q (pid %v): %w", executable.name, pid, err))
}
}

md := &processMetadata{
pid: pid,
parentPid: parentPid,
parentPid: parentProcessID,
executable: executable,
command: command,
username: username,
Expand Down
Loading