From 7af0f8274a58e8e62c7d9c02185bb7e145d31818 Mon Sep 17 00:00:00 2001 From: Vendula Poncova Date: Mon, 31 Jul 2023 13:45:11 +0200 Subject: [PATCH] fix: Count the number of vCPUs instead of the number of CPU cores Based on the available documents, the client should collect and export "a single metric that counts the number of VCPUs on the system". Let's use a value that can be calculated as: `cat /proc/cpuinfo | grep processor | wc -l` There are several alternative options and methods to consider, but let's start with the easist one that doesn't require any additional dependencies. Use the metric name `system_cpu_logical_count` that follows semantic conventions for OTEL: https://github.com/open-telemetry/semantic-conventions/pull/99 --- hostinfo/cpu.go | 9 ++------- hostinfo/info.go | 20 ++++++++++---------- notify/prometheus.go | 4 ++-- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/hostinfo/cpu.go b/hostinfo/cpu.go index 7450aa6..bb0869e 100644 --- a/hostinfo/cpu.go +++ b/hostinfo/cpu.go @@ -19,11 +19,6 @@ func GetCPUCount() (uint, error) { return 0, fmt.Errorf("GetCPUCount: failed to load CPUInfo: %w", err) } - if len(info) == 0 { - return 0, fmt.Errorf("GetCPUCount: no value in CPUInfo") - } - - cpuCores := info[0].CPUCores - - return cpuCores, nil + cpuCount := uint(len(info)) + return cpuCount, nil } diff --git a/hostinfo/info.go b/hostinfo/info.go index 47749f6..46c5e4c 100644 --- a/hostinfo/info.go +++ b/hostinfo/info.go @@ -7,15 +7,15 @@ import ( ) type HostInfo struct { - CpuCoreCount uint - HostId string - CertPath string - CertKeyPath string + CpuCount uint + HostId string + CertPath string + CertKeyPath string } func LoadHostInfo(c *config.Config) (*HostInfo, error) { - cpuCoreCount, err := GetCPUCount() + cpuCount, err := GetCPUCount() if err != nil { return nil, err } @@ -26,16 +26,16 @@ func LoadHostInfo(c *config.Config) (*HostInfo, error) { } return &HostInfo{ - CpuCoreCount: cpuCoreCount, - HostId: hostId, - CertPath: GetCertPath(c), - CertKeyPath: GetCertKeyPath(c), + CpuCount: cpuCount, + HostId: hostId, + CertPath: GetCertPath(c), + CertKeyPath: GetCertKeyPath(c), }, nil } func (hi *HostInfo) Print() { fmt.Println("HostInfo:") - fmt.Println(" CpuCoreCount: ", hi.CpuCoreCount) + fmt.Println(" CpuCount: ", hi.CpuCount) fmt.Println(" HostId: ", hi.HostId) fmt.Println(" CertPath: ", hi.CertPath) fmt.Println(" CertKeyPath: ", hi.CertKeyPath) diff --git a/notify/prometheus.go b/notify/prometheus.go index 2b96da1..62ed5b5 100644 --- a/notify/prometheus.go +++ b/notify/prometheus.go @@ -91,7 +91,7 @@ func hostInfo2WriteRequest(hostinfo *hostinfo.HostInfo) *prompb.WriteRequest { labels := []prompb.Label{ { Name: "__name__", - Value: "cpu_core_count", + Value: "system_cpu_logical_count", }, { Name: "hostId", @@ -101,7 +101,7 @@ func hostInfo2WriteRequest(hostinfo *hostinfo.HostInfo) *prompb.WriteRequest { samples := []prompb.Sample{ { - Value: float64(hostinfo.CpuCoreCount), + Value: float64(hostinfo.CpuCount), Timestamp: timestamp, }, }