Skip to content

Commit 2c2e029

Browse files
atoulmejmsnll
authored andcommitted
[receiver/awscontainerinsights] HOST_PROC usage (open-telemetry#26477)
Remove the need to set the environment variable HOST_PROC as part of the awscontainerinsightsreceiver open-telemetry#24777
1 parent 4bb7bcb commit 2c2e029

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: receiver/awscontainerinsightsreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Remove the need to set an env var in the receiver to get CPU and memory info
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [24777]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

receiver/awscontainerinsightreceiver/internal/host/nodeCapacity.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44
package host // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/host"
55

66
import (
7-
"fmt"
7+
"context"
88
"os"
99

10+
"github.com/shirou/gopsutil/v3/common"
1011
"github.com/shirou/gopsutil/v3/cpu"
1112
"github.com/shirou/gopsutil/v3/mem"
1213
"go.uber.org/zap"
1314
)
1415

15-
const (
16-
goPSUtilProcDirEnv = "HOST_PROC"
17-
)
18-
1916
type nodeCapacityProvider interface {
2017
getMemoryCapacity() int64
2118
getNumCores() int64
@@ -30,8 +27,8 @@ type nodeCapacity struct {
3027
osLstat func(name string) (os.FileInfo, error)
3128
// osSetenv sets the value of the environment variable named by the key
3229
osSetenv func(key string, value string) error
33-
virtualMemory func() (*mem.VirtualMemoryStat, error)
34-
cpuInfo func() ([]cpu.InfoStat, error)
30+
virtualMemory func(ctx context.Context) (*mem.VirtualMemoryStat, error)
31+
cpuInfo func(ctx context.Context) ([]cpu.InfoStat, error)
3532
}
3633

3734
type nodeCapacityOption func(*nodeCapacity)
@@ -41,8 +38,8 @@ func newNodeCapacity(logger *zap.Logger, options ...nodeCapacityOption) (nodeCap
4138
logger: logger,
4239
osLstat: os.Lstat,
4340
osSetenv: os.Setenv,
44-
virtualMemory: mem.VirtualMemory,
45-
cpuInfo: cpu.Info,
41+
virtualMemory: mem.VirtualMemoryWithContext,
42+
cpuInfo: cpu.InfoWithContext,
4643
}
4744

4845
for _, opt := range options {
@@ -52,26 +49,25 @@ func newNodeCapacity(logger *zap.Logger, options ...nodeCapacityOption) (nodeCap
5249
if _, err := nc.osLstat(hostProc); os.IsNotExist(err) {
5350
return nil, err
5451
}
55-
if err := nc.osSetenv(goPSUtilProcDirEnv, hostProc); err != nil {
56-
return nil, fmt.Errorf("NodeCapacity cannot set goPSUtilProcDirEnv to %s: %w", hostProc, err)
57-
}
52+
envMap := common.EnvMap{common.HostProcEnvKey: hostProc}
53+
ctx := context.WithValue(context.Background(), common.EnvKey, envMap)
5854

59-
nc.parseCPU()
60-
nc.parseMemory()
55+
nc.parseCPU(ctx)
56+
nc.parseMemory(ctx)
6157
return nc, nil
6258
}
6359

64-
func (nc *nodeCapacity) parseMemory() {
65-
if memStats, err := nc.virtualMemory(); err == nil {
60+
func (nc *nodeCapacity) parseMemory(ctx context.Context) {
61+
if memStats, err := nc.virtualMemory(ctx); err == nil {
6662
nc.memCapacity = int64(memStats.Total)
6763
} else {
6864
// If any error happen, then there will be no mem utilization metrics
6965
nc.logger.Error("NodeCapacity cannot get memStats from psUtil", zap.Error(err))
7066
}
7167
}
7268

73-
func (nc *nodeCapacity) parseCPU() {
74-
if cpuInfos, err := nc.cpuInfo(); err == nil {
69+
func (nc *nodeCapacity) parseCPU(ctx context.Context) {
70+
if cpuInfos, err := nc.cpuInfo(ctx); err == nil {
7571
numCores := len(cpuInfos)
7672
nc.cpuCapacity = int64(numCores)
7773
} else {

receiver/awscontainerinsightreceiver/internal/host/nodeCapacity_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package host
55

66
import (
7+
"context"
78
"errors"
89
"os"
910
"testing"
@@ -31,28 +32,20 @@ func TestNodeCapacity(t *testing.T) {
3132
return nil, nil
3233
}
3334
}
34-
setEnvOption := func(nc *nodeCapacity) {
35-
nc.osSetenv = func(key, value string) error {
36-
return errors.New("error")
37-
}
38-
}
39-
nc, err = newNodeCapacity(zap.NewNop(), lstatOption, setEnvOption)
40-
assert.Nil(t, nc)
41-
assert.NotNil(t, err)
4235

4336
// can't parse cpu and mem info
44-
setEnvOption = func(nc *nodeCapacity) {
37+
setEnvOption := func(nc *nodeCapacity) {
4538
nc.osSetenv = func(key, value string) error {
4639
return nil
4740
}
4841
}
4942
virtualMemOption := func(nc *nodeCapacity) {
50-
nc.virtualMemory = func() (*mem.VirtualMemoryStat, error) {
43+
nc.virtualMemory = func(ctx context.Context) (*mem.VirtualMemoryStat, error) {
5144
return nil, errors.New("error")
5245
}
5346
}
5447
cpuInfoOption := func(nc *nodeCapacity) {
55-
nc.cpuInfo = func() ([]cpu.InfoStat, error) {
48+
nc.cpuInfo = func(ctx context.Context) ([]cpu.InfoStat, error) {
5649
return nil, errors.New("error")
5750
}
5851
}
@@ -64,14 +57,14 @@ func TestNodeCapacity(t *testing.T) {
6457

6558
// normal case where everything is working
6659
virtualMemOption = func(nc *nodeCapacity) {
67-
nc.virtualMemory = func() (*mem.VirtualMemoryStat, error) {
60+
nc.virtualMemory = func(ctx context.Context) (*mem.VirtualMemoryStat, error) {
6861
return &mem.VirtualMemoryStat{
6962
Total: 1024,
7063
}, nil
7164
}
7265
}
7366
cpuInfoOption = func(nc *nodeCapacity) {
74-
nc.cpuInfo = func() ([]cpu.InfoStat, error) {
67+
nc.cpuInfo = func(ctx context.Context) ([]cpu.InfoStat, error) {
7568
return []cpu.InfoStat{
7669
{},
7770
{},

0 commit comments

Comments
 (0)