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) +}