From 19a5f291697e23225c345064b62da7a329908a7b Mon Sep 17 00:00:00 2001 From: Paulo Janotti Date: Wed, 12 Feb 2025 16:00:31 -0800 Subject: [PATCH] [receiver/awscontainerinsight] Add HOST_PROC env var support (#37694) #### Description Add support to HOST_PROC env var support. At this point I'm not sure if the default should be changed or not, but, with the env var one can at least experiment with the receiver on different images. #### Link to tracking issue Fixes #35862 #### Testing Added respective unit test. #### Documentation Changelog --- ...-env-var-on-awscontainerinsightreceiver.yaml | 11 +++++++++++ .../internal/host/nodeCapacity.go | 9 +++++++-- .../internal/host/nodeCapacity_test.go | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .chloggen/support-host-proc-env-var-on-awscontainerinsightreceiver.yaml diff --git a/.chloggen/support-host-proc-env-var-on-awscontainerinsightreceiver.yaml b/.chloggen/support-host-proc-env-var-on-awscontainerinsightreceiver.yaml new file mode 100644 index 000000000000..4b86edd2ba78 --- /dev/null +++ b/.chloggen/support-host-proc-env-var-on-awscontainerinsightreceiver.yaml @@ -0,0 +1,11 @@ +change_type: enhancement + +component: awscontainerinsightreceiver + +note: Add support for HOST_PROC environment variable in AWS Container Insight Receiver. + +issues: [35862] + +subtext: + +change_logs: [user] diff --git a/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity.go b/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity.go index 7cf5092b77b3..ed4b84022efa 100644 --- a/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity.go +++ b/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity.go @@ -43,10 +43,15 @@ func newNodeCapacity(logger *zap.Logger, options ...nodeCapacityOption) (nodeCap opt(nc) } - if _, err := nc.osLstat(hostProc); os.IsNotExist(err) { + actualHostProc, ok := os.LookupEnv(string(common.HostProcEnvKey)) + if !ok { + actualHostProc = hostProc + } + + if _, err := nc.osLstat(actualHostProc); os.IsNotExist(err) { return nil, err } - envMap := common.EnvMap{common.HostProcEnvKey: hostProc} + envMap := common.EnvMap{common.HostProcEnvKey: actualHostProc} ctx := context.WithValue(context.Background(), common.EnvKey, envMap) nc.parseCPU(ctx) diff --git a/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity_test.go b/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity_test.go index d54f0dbb1f49..b574c48a7ffd 100644 --- a/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity_test.go +++ b/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity_test.go @@ -9,6 +9,7 @@ import ( "os" "testing" + "github.com/shirou/gopsutil/v4/common" "github.com/shirou/gopsutil/v4/cpu" "github.com/shirou/gopsutil/v4/mem" "github.com/stretchr/testify/assert" @@ -71,3 +72,19 @@ func TestNodeCapacity(t *testing.T) { assert.Equal(t, int64(1024), nc.getMemoryCapacity()) assert.Equal(t, int64(2), nc.getNumCores()) } + +func TestNodeCapacity_ReadsHostProcEnvVar(t *testing.T) { + const customHostProc = "/custom/host/proc" + t.Setenv(string(common.HostProcEnvKey), customHostProc) + + lstatOption := func(nc *nodeCapacity) { + nc.osLstat = func(name string) (os.FileInfo, error) { + assert.Equal(t, customHostProc, name) + return nil, nil + } + } + + nc, err := newNodeCapacity(zap.NewNop(), lstatOption) + assert.NoError(t, err) + assert.NotNil(t, nc) +}