4
4
package host // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/awscontainerinsightreceiver/internal/host"
5
5
6
6
import (
7
- "fmt "
7
+ "context "
8
8
"os"
9
9
10
+ "github.com/shirou/gopsutil/v3/common"
10
11
"github.com/shirou/gopsutil/v3/cpu"
11
12
"github.com/shirou/gopsutil/v3/mem"
12
13
"go.uber.org/zap"
13
14
)
14
15
15
- const (
16
- goPSUtilProcDirEnv = "HOST_PROC"
17
- )
18
-
19
16
type nodeCapacityProvider interface {
20
17
getMemoryCapacity () int64
21
18
getNumCores () int64
@@ -30,8 +27,8 @@ type nodeCapacity struct {
30
27
osLstat func (name string ) (os.FileInfo , error )
31
28
// osSetenv sets the value of the environment variable named by the key
32
29
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 )
35
32
}
36
33
37
34
type nodeCapacityOption func (* nodeCapacity )
@@ -41,8 +38,8 @@ func newNodeCapacity(logger *zap.Logger, options ...nodeCapacityOption) (nodeCap
41
38
logger : logger ,
42
39
osLstat : os .Lstat ,
43
40
osSetenv : os .Setenv ,
44
- virtualMemory : mem .VirtualMemory ,
45
- cpuInfo : cpu .Info ,
41
+ virtualMemory : mem .VirtualMemoryWithContext ,
42
+ cpuInfo : cpu .InfoWithContext ,
46
43
}
47
44
48
45
for _ , opt := range options {
@@ -52,26 +49,25 @@ func newNodeCapacity(logger *zap.Logger, options ...nodeCapacityOption) (nodeCap
52
49
if _ , err := nc .osLstat (hostProc ); os .IsNotExist (err ) {
53
50
return nil , err
54
51
}
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 )
58
54
59
- nc .parseCPU ()
60
- nc .parseMemory ()
55
+ nc .parseCPU (ctx )
56
+ nc .parseMemory (ctx )
61
57
return nc , nil
62
58
}
63
59
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 {
66
62
nc .memCapacity = int64 (memStats .Total )
67
63
} else {
68
64
// If any error happen, then there will be no mem utilization metrics
69
65
nc .logger .Error ("NodeCapacity cannot get memStats from psUtil" , zap .Error (err ))
70
66
}
71
67
}
72
68
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 {
75
71
numCores := len (cpuInfos )
76
72
nc .cpuCapacity = int64 (numCores )
77
73
} else {
0 commit comments