From f9a1ae386bd1b3c94fefe5551377d5fdb3c13a9f Mon Sep 17 00:00:00 2001 From: Kulwant Singh Date: Tue, 20 Feb 2024 19:29:44 +0000 Subject: [PATCH] Fix CPU utilization percentage for Windows nodes (#161) * 1. Fix CPU cores in Windows 2. Sum fs usage field * fix fs type * fix fs issue * fix fs issue --- .../internal/host/nodeCapacity.go | 17 +++++++++++++++++ .../internal/host/nodeCapacity_test.go | 4 ++-- .../internal/k8swindows/extractors/extractor.go | 1 + .../k8swindows/extractors/extractorhelpers.go | 8 ++++++-- .../k8swindows/extractors/fs_extractor.go | 4 ++++ .../k8swindows/extractors/fs_extractor_test.go | 6 ++++-- 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity.go b/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity.go index 522591e95c82..01fa4612ad24 100644 --- a/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity.go +++ b/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity.go @@ -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) @@ -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 } diff --git a/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity_test.go b/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity_test.go index fc903775f0f1..3f4abc99906b 100644 --- a/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity_test.go +++ b/receiver/awscontainerinsightreceiver/internal/host/nodeCapacity_test.go @@ -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 } } diff --git a/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/extractor.go b/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/extractor.go index a1b2fe3583de..3e904fd22606 100644 --- a/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/extractor.go +++ b/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/extractor.go @@ -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 diff --git a/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/extractorhelpers.go b/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/extractorhelpers.go index f4c50ef437a2..df888ca3e97c 100644 --- a/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/extractorhelpers.go +++ b/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/extractorhelpers.go @@ -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 diff --git a/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/fs_extractor.go b/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/fs_extractor.go index 2723af9f480e..0120736f7840 100644 --- a/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/fs_extractor.go +++ b/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/fs_extractor.go @@ -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) diff --git a/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/fs_extractor_test.go b/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/fs_extractor_test.go index 60088a7b63e6..e53e68d4543c 100644 --- a/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/fs_extractor_test.go +++ b/receiver/awscontainerinsightreceiver/internal/k8swindows/extractors/fs_extractor_test.go @@ -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) @@ -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) }