From df42199c3a44f3563536d5ed4d7a8952ae26ca16 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Fri, 1 Dec 2023 14:36:05 -0800 Subject: [PATCH] add cgroup functionality --- .chloggen/cgroups.yaml | 27 ++++++++++++++++ .../scraper/processscraper/process.go | 1 - .../processscraper/process_scraper_darwin.go | 9 ++---- .../processscraper/process_scraper_linux.go | 31 +++++++++++++++++-- .../processscraper/process_scraper_windows.go | 7 +---- 5 files changed, 59 insertions(+), 16 deletions(-) create mode 100755 .chloggen/cgroups.yaml diff --git a/.chloggen/cgroups.yaml b/.chloggen/cgroups.yaml new file mode 100755 index 000000000000..411709f3b8b9 --- /dev/null +++ b/.chloggen/cgroups.yaml @@ -0,0 +1,27 @@ +# Use this changelog template to create an entry for release notes. + +# 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. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Add support for cgroup information as a new resource attribute on process metrics, as `process.cgroup` + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [29282] + +# (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. +# Use pipe (|) for multiline entries. +subtext: + +# If your change doesn't affect end users or the exported elements of any package, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process.go index 6c079c518ae3..32d4c63cf4b1 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process.go @@ -75,7 +75,6 @@ type processHandles interface { type processHandle interface { NameWithContext(context.Context) (string, error) - CgroupWithContext(context.Context) (string, error) ExeWithContext(context.Context) (string, error) UsernameWithContext(context.Context) (string, error) CmdlineWithContext(context.Context) (string, error) diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_darwin.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_darwin.go index 2f06e6421753..69d1dcabad93 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_darwin.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_darwin.go @@ -38,13 +38,8 @@ func getProcessName(ctx context.Context, proc processHandle, _ string) (string, return name, nil } -func getProcessCgroup(ctx context.Context, proc processHandle) (string, error) { - cgroup, err := proc.CgroupWithContext(ctx) - if err != nil { - return "", err - } - - return cgroup, nil +func getProcessCgroup(_ context.Context, _ processHandle) (string, error) { + return "", nil } func getProcessExecutable(ctx context.Context, proc processHandle) (string, error) { diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_linux.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_linux.go index 17e5ac592976..d917fc67aff3 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_linux.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_linux.go @@ -8,8 +8,14 @@ package processscraper // import "github.com/open-telemetry/opentelemetry-collec import ( "context" + "os" + "path/filepath" + "strconv" + "strings" + "github.com/shirou/gopsutil/v3/common" "github.com/shirou/gopsutil/v3/cpu" + "github.com/shirou/gopsutil/v3/process" "go.opentelemetry.io/collector/pdata/pcommon" "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper/internal/metadata" @@ -47,12 +53,33 @@ func getProcessExecutable(ctx context.Context, proc processHandle) (string, erro } func getProcessCgroup(ctx context.Context, proc processHandle) (string, error) { - cgroup, err := proc.CgroupWithContext(ctx) + pid := proc.(*process.Process).Pid + statPath := getEnvWithContext(ctx, string(common.HostProcEnvKey), "/proc", strconv.Itoa(int(pid)), "cgroup") + contents, err := os.ReadFile(statPath) if err != nil { return "", err } - return cgroup, nil + return strings.TrimSuffix(string(contents), "\n"), 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. +func getEnvWithContext(ctx context.Context, key string, dfault string, combineWith ...string) string { + var value string + if env, ok := ctx.Value(common.EnvKey).(common.EnvMap); ok { + value = env[common.EnvKeyType(key)] + } + if value == "" { + value = os.Getenv(key) + } + if value == "" { + value = dfault + } + segments := append([]string{value}, combineWith...) + + return filepath.Join(segments...) } func getProcessCommand(ctx context.Context, proc processHandle) (*commandMetadata, error) { diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_windows.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_windows.go index 79ec41924f60..d107f653db82 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_windows.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper_windows.go @@ -38,12 +38,7 @@ func getProcessName(_ context.Context, _ processHandle, exePath string) (string, } func getProcessCgroup(ctx context.Context, proc processHandle) (string, error) { - cgroup, err := proc.CgroupWithContext(ctx) - if err != nil { - return "", err - } - - return cgroup, nil + return "", nil } func getProcessExecutable(ctx context.Context, proc processHandle) (string, error) {