Skip to content

Commit

Permalink
add_process_metadata: container id: Do not cache whole cgroup file fo…
Browse files Browse the repository at this point in the history
…r each process (elastic#17351) (elastic#17361)

* Do not cache whole cgroup file for each process. Include kube-proxy cid too.

Co-authored-by: Jako Tinkus <jatinkus@microsoft.com>
(cherry picked from commit 76f4353)

Co-authored-by: jtinkus <35308202+jtinkus@users.noreply.github.com>
  • Loading branch information
Carlos Pérez-Aradros Herce and jtinkus committed Mar 31, 2020
1 parent 2ff5125 commit 1754537
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions libbeat/processors/add_process_metadata/gosigar_cid_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,44 @@ type gosigarCidProvider struct {
hostPath string
cgroupPrefixes []string
processCgroupPaths func(string, int) (map[string]string, error)
cgroupsCache *common.Cache
pidCidCache *common.Cache
}

func (p gosigarCidProvider) GetCid(pid int) (result string, err error) {

var cid string
var ok bool

// check from cache
if p.pidCidCache != nil {
if cid, ok = p.pidCidCache.Get(pid).(string); ok {
p.log.Debugf("Using cached container id for pid=%v", pid)
return cid, nil
}
}

cgroups, err := p.getProcessCgroups(pid)

if err != nil {
p.log.Debugf("failed to get cgroups for pid=%v: %v", pid, err)
}

cid := p.getCid(cgroups)
cid = p.getCid(cgroups)

// add pid and cid to cache
if p.pidCidCache != nil {
p.pidCidCache.Put(pid, cid)
}
return cid, nil
}

func newCidProvider(hostPath string, cgroupPrefixes []string, processCgroupPaths func(string, int) (map[string]string, error), cgroupsCache *common.Cache) gosigarCidProvider {
func newCidProvider(hostPath string, cgroupPrefixes []string, processCgroupPaths func(string, int) (map[string]string, error), pidCidCache *common.Cache) gosigarCidProvider {
return gosigarCidProvider{
log: logp.NewLogger(providerName),
hostPath: hostPath,
cgroupPrefixes: cgroupPrefixes,
processCgroupPaths: processCgroupPaths,
cgroupsCache: cgroupsCache,
pidCidCache: pidCidCache,
}
}

Expand All @@ -68,14 +83,6 @@ func newCidProvider(hostPath string, cgroupPrefixes []string, processCgroupPaths
func (p gosigarCidProvider) getProcessCgroups(pid int) (map[string]string, error) {

var cgroup map[string]string
var ok bool

if p.cgroupsCache != nil {
if cgroup, ok = p.cgroupsCache.Get(pid).(map[string]string); ok {
p.log.Debugf("Using cached cgroups for pid=%v", pid)
return cgroup, nil
}
}

cgroup, err := p.processCgroupPaths(p.hostPath, pid)
switch err.(type) {
Expand All @@ -87,10 +94,6 @@ func (p gosigarCidProvider) getProcessCgroups(pid int) (map[string]string, error
return cgroup, errors.Wrapf(err, "failed to read cgroups for pid=%v", pid)
}

if p.cgroupsCache != nil {
p.cgroupsCache.Put(pid, cgroup)
}

return cgroup, nil
}

Expand Down

0 comments on commit 1754537

Please sign in to comment.