Skip to content

Commit

Permalink
add cgroup functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Dec 5, 2023
1 parent f32a61c commit df42199
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 16 deletions.
27 changes: 27 additions & 0 deletions .chloggen/cgroups.yaml
Original file line number Diff line number Diff line change
@@ -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: []
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit df42199

Please sign in to comment.