Skip to content

Commit

Permalink
Merge pull request #6641 from ewgRa/containerised-self-metric-6620
Browse files Browse the repository at this point in the history
Fix self metrix when containerised #6620
  • Loading branch information
jsoriano authored Mar 27, 2018
2 parents 32c60c1 + dd70722 commit 88998f9
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
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

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

0 comments on commit 88998f9

Please sign in to comment.