Skip to content
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

[receiver/hostmetricsreceiver] add process.parent_pid attribute to process metrics #12637

Merged
merged 7 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -33,6 +33,7 @@ metrics:
| process.executable.name | The name of the process executable. On Linux based systems, can be set to the Name in proc/[pid]/status. On Windows, can be set to the base name of GetProcessImageFileNameW. | String |
| process.executable.path | The full path to the process executable. On Linux based systems, can be set to the target of proc/[pid]/exe. On Windows, can be set to the result of GetProcessImageFileNameW. | String |
| process.owner | The username of the user that owns the process. | String |
| process.parent_pid | Parent Process identifier (PPID). | Int |
| process.pid | Process identifier (PID). | Int |

## Metric attributes
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ resource_attributes:
process.pid:
description: Process identifier (PID).
type: int
process.parent_pid:
description: Parent Process identifier (PPID).
type: int
process.executable.name:
description: >-
The name of the process executable. On Linux based systems, can be set to the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

type processMetadata struct {
pid int32
parentPid int32
executable *executableMetadata
command *commandMetadata
username string
Expand All @@ -50,6 +51,7 @@ func (m *processMetadata) resourceOptions() []metadata.ResourceMetricsOption {
opts := make([]metadata.ResourceMetricsOption, 0, 6)
opts = append(opts,
metadata.WithProcessPid(int64(m.pid)),
metadata.WithProcessParentPid(int64(m.parentPid)),
metadata.WithProcessExecutableName(m.executable.name),
metadata.WithProcessExecutablePath(m.executable.path),
)
Expand Down Expand Up @@ -88,6 +90,7 @@ type processHandle interface {
MemoryInfo() (*process.MemoryInfoStat, error)
IOCounters() (*process.IOCountersStat, error)
CreateTime() (int64, error)
Parent() (*process.Process, error)
}

type gopsProcessHandles struct {
Expand All @@ -114,3 +117,23 @@ func getProcessHandlesInternal() (processHandles, error) {

return &gopsProcessHandles{handles: processes}, nil
}

func parentPid(handle processHandle, pid int32) (int32, error) {
// special case for pid 0
if pid == 0 {
dmitryax marked this conversation as resolved.
Show resolved Hide resolved
return 0, nil
}
parent, err := handle.Parent()

if err != nil {
// return pid of -1 along with error for all other problems retrieving parent pid
return -1, err
}

// if a process does not have a parent return 0
if parent == nil {
return 0, nil
}

return parent.Pid, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,14 @@ func (s *scraper) getProcessMetadata() ([]*processMetadata, error) {
continue
}

parentPid, err := parentPid(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,
executable: executable,
command: command,
username: username,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func assertProcessResourceAttributesExist(t *testing.T, resourceMetrics pmetric.
internal.AssertContainsAttribute(t, attr, conventions.AttributeProcessCommand)
internal.AssertContainsAttribute(t, attr, conventions.AttributeProcessCommandLine)
internal.AssertContainsAttribute(t, attr, conventions.AttributeProcessOwner)
internal.AssertContainsAttribute(t, attr, "process.parent_pid")
dmitryax marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -310,6 +311,11 @@ func (p *processHandleMock) CreateTime() (int64, error) {
return args.Get(0).(int64), args.Error(1)
}

func (p *processHandleMock) Parent() (*process.Process, error) {
args := p.MethodCalled("Parent")
return args.Get(0).(*process.Process), args.Error(1)
}

func newDefaultHandleMock() *processHandleMock {
handleMock := &processHandleMock{}
handleMock.On("Username").Return("username", nil)
Expand All @@ -318,6 +324,7 @@ func newDefaultHandleMock() *processHandleMock {
handleMock.On("Times").Return(&cpu.TimesStat{}, nil)
handleMock.On("MemoryInfo").Return(&process.MemoryInfoStat{}, nil)
handleMock.On("IOCounters").Return(&process.IOCountersStat{}, nil)
handleMock.On("Parent").Return(&process.Process{Pid: 2}, nil)
return handleMock
}

Expand Down Expand Up @@ -460,6 +467,7 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) {
memoryInfoError error
ioCountersError error
createTimeError error
parentPidError error
expectedError string
}

Expand Down Expand Up @@ -505,6 +513,11 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) {
ioCountersError: errors.New("err7"),
expectedError: `error reading disk usage for process "test" (pid 1): err7`,
},
{
name: "Parent PID Error",
parentPidError: errors.New("err8"),
expectedError: `error reading parent pid for process "test" (pid 1): err8`,
},
{
name: "Multiple Errors",
cmdlineError: errors.New("err2"),
Expand Down Expand Up @@ -548,6 +561,7 @@ func TestScrapeMetrics_ProcessErrors(t *testing.T) {
handleMock.On("MemoryInfo").Return(&process.MemoryInfoStat{}, test.memoryInfoError)
handleMock.On("IOCounters").Return(&process.IOCountersStat{}, test.ioCountersError)
handleMock.On("CreateTime").Return(int64(0), test.createTimeError)
handleMock.On("Parent").Return(&process.Process{Pid: 2}, test.parentPidError)

scraper.getProcessHandles = func() (processHandles, error) {
return &processHandlesMock{handles: []*processHandleMock{handleMock}}, nil
Expand Down
15 changes: 15 additions & 0 deletions unreleased/hostmetrics-receiver-ppid-attribute.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: hostmetricsreceiver

# A brief description of the change
note: Add new resource attribute `process.parent_pid`. The value of the attribute is the parent PID

# One or more tracking issues related to the change
issues: [12290]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
subtext: