Skip to content

Commit

Permalink
Fix CPU utilization percentage for Windows nodes (amazon-contributing…
Browse files Browse the repository at this point in the history
…#161)

* 1. Fix CPU cores in Windows

2. Sum fs usage field

* fix fs type

* fix fs issue

* fix fs issue
  • Loading branch information
KlwntSingh authored Feb 20, 2024
1 parent fc380a1 commit f9a1ae3
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
17 changes: 17 additions & 0 deletions receiver/awscontainerinsightreceiver/internal/host/nodeCapacity.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (nc *nodeCapacity) parseMemory(ctx context.Context) {
}

func (nc *nodeCapacity) parseCPU(ctx context.Context) {
if runtime.GOOS == "windows" {
nc.parseCPUWindows(ctx)
return
}
if cpuInfos, err := nc.cpuInfo(ctx); err == nil {
numCores := len(cpuInfos)
nc.cpuCapacity = int64(numCores)
Expand All @@ -80,6 +84,19 @@ func (nc *nodeCapacity) parseCPU(ctx context.Context) {
}
}

func (nc *nodeCapacity) parseCPUWindows(ctx context.Context) {
if cpuInfos, err := nc.cpuInfo(ctx); err == nil {
var coreCount int32
for _, cpuInfo := range cpuInfos {
coreCount += cpuInfo.Cores
}
nc.cpuCapacity = int64(coreCount)
} else {
// If any error happen, then there will be no cpu utilization metrics
nc.logger.Error("NodeCapacity cannot get cpuInfo from psUtil", zap.Error(err))
}
}

func (nc *nodeCapacity) getNumCores() int64 {
return nc.cpuCapacity
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func TestNodeCapacity(t *testing.T) {
cpuInfoOption = func(nc *nodeCapacity) {
nc.cpuInfo = func(ctx context.Context) ([]cpu.InfoStat, error) {
return []cpu.InfoStat{
{},
{},
{Cores: 1},
{Cores: 1},
}, nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type MemoryStat struct {
// FileSystemStat for Container and Node.
type FileSystemStat struct {
Time time.Time
Name string
AvailableBytes uint64
CapacityBytes uint64
UsedBytes uint64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,14 @@ func ConvertContainerToRaw(containerStat stats.ContainerStats, podStat stats.Pod

rawMetic.FileSystemStats = []FileSystemStat{}
if containerStat.Rootfs != nil {
rawMetic.FileSystemStats = append(rawMetic.FileSystemStats, convertFileSystemStats(*containerStat.Rootfs))
fStat := convertFileSystemStats(*containerStat.Rootfs)
fStat.Name = "rootfs"
rawMetic.FileSystemStats = append(rawMetic.FileSystemStats, fStat)
}
if containerStat.Logs != nil {
rawMetic.FileSystemStats = append(rawMetic.FileSystemStats, convertFileSystemStats(*containerStat.Logs))
fStat := convertFileSystemStats(*containerStat.Logs)
fStat.Name = "logfs"
rawMetic.FileSystemStats = append(rawMetic.FileSystemStats, fStat)
}

return rawMetic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (f *FileSystemMetricExtractor) GetValue(rawMetric RawMetric, _ cExtractor.C
for _, v := range rawMetric.FileSystemStats {
metric := cExtractor.NewCadvisorMetric(containerType, f.logger)

if v.Name != "" {
metric.AddTag(ci.DiskDev, v.Name)
}

metric.AddField(ci.MetricName(containerType, ci.FSUsage), v.UsedBytes)
metric.AddField(ci.MetricName(containerType, ci.FSCapacity), v.CapacityBytes)
metric.AddField(ci.MetricName(containerType, ci.FSAvailable), v.AvailableBytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func TestFSStats(t *testing.T) {
"container_filesystem_utilization": float64(0.3955174875484043),
}
expectedTags = map[string]string{
"Type": "ContainerFS",
"device": "rootfs",
"Type": "ContainerFS",
}
cExtractor.AssertContainsTaggedField(t, cMetrics[0], expectedFields, expectedTags)

Expand All @@ -77,7 +78,8 @@ func TestFSStats(t *testing.T) {
"container_filesystem_utilization": float64(0.0010704219949207732),
}
expectedTags = map[string]string{
"Type": "ContainerFS",
"device": "logfs",
"Type": "ContainerFS",
}
cExtractor.AssertContainsTaggedField(t, cMetrics[1], expectedFields, expectedTags)
}

0 comments on commit f9a1ae3

Please sign in to comment.