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

Fix self metrix when containerised #6620 #6641

Merged
merged 1 commit into from
Mar 27, 2018
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
17 changes: 12 additions & 5 deletions libbeat/cmd/instance/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package instance

import (
"fmt"
"os"
"runtime"

"github.com/elastic/beats/libbeat/logp"
Expand Down Expand Up @@ -65,8 +64,12 @@ func reportMemStats(m monitoring.Mode, V monitoring.Visitor) {
}

func getRSSSize() (uint64, error) {
beatPID := os.Getpid()
state, err := beatProcessStats.GetOne(beatPID)
pid, err := process.GetSelfPid()
if err != nil {
return 0, fmt.Errorf("error getting PID for self process: %v", err)
}

state, err := beatProcessStats.GetOne(pid)
if err != nil {
return 0, fmt.Errorf("error retrieving process stats: %v", err)
}
Expand Down Expand Up @@ -121,8 +124,12 @@ func reportBeatCPU(_ monitoring.Mode, V monitoring.Visitor) {
}

func getCPUUsage() (float64, *process.Ticks, error) {
beatPID := os.Getpid()
state, err := beatProcessStats.GetOne(beatPID)
pid, err := process.GetSelfPid()
if err != nil {
return 0.0, nil, fmt.Errorf("error getting PID for self process: %v", err)
}

state, err := beatProcessStats.GetOne(pid)
if err != nil {
return 0.0, nil, fmt.Errorf("error retrieving process stats: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion libbeat/metric/system/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Process struct {
cpuTotalPctNorm float64
}

// Stats stores the stats of preocesses on the host.
// Stats stores the stats of processes on the host.
type Stats struct {
Procs []string
ProcsMap ProcsMap
Expand Down
20 changes: 20 additions & 0 deletions libbeat/metric/system/process/process_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package process
Copy link
Member

Choose a reason for hiding this comment

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

// +build linux

Copy link
Member

Choose a reason for hiding this comment

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

Oh, this won't be needed as the file has the _linux suffix.


import (
"os"
"path"
"strconv"

"github.com/elastic/gosigar"
)

// GetSelfPid returns the PID for this process
func GetSelfPid() (int, error) {
pid, err := os.Readlink(path.Join(gosigar.Procd, "self"))

if err != nil {
return 0, err
}

return strconv.Atoi(pid)
}
10 changes: 10 additions & 0 deletions libbeat/metric/system/process/process_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// +build !linux

package process

import "os"

// GetSelfPid returns the PID for this process
func GetSelfPid() (int, error) {
return os.Getpid(), nil
}
7 changes: 7 additions & 0 deletions libbeat/metric/system/process/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ func TestGetProcess(t *testing.T) {
}
}

// See https://github.com/elastic/beats/issues/6620
func TestGetSelfPid(t *testing.T) {
pid, err := GetSelfPid()
assert.NoError(t, err)
assert.Equal(t, os.Getpid(), pid)
}

func TestProcState(t *testing.T) {
assert.Equal(t, getProcState('R'), "running")
assert.Equal(t, getProcState('S'), "sleeping")
Expand Down