From 9ea9a5f029c62d49a4d2e31d7f516ace67d73760 Mon Sep 17 00:00:00 2001
From: david <davidventura27@gmail.com>
Date: Thu, 16 Dec 2021 13:20:20 +0100
Subject: [PATCH] only publish metrics for isolated cpus

Signed-off-by: david <davidventura27@gmail.com>
---
 collector/cpu_linux.go            | 29 +++--------------------------
 collector/fixtures/e2e-output.txt |  5 +----
 2 files changed, 4 insertions(+), 30 deletions(-)

diff --git a/collector/cpu_linux.go b/collector/cpu_linux.go
index 63c3768f..017ffec6 100644
--- a/collector/cpu_linux.go
+++ b/collector/cpu_linux.go
@@ -299,34 +299,11 @@ func (c *cpuCollector) updateThermalThrottle(ch chan<- prometheus.Metric) error
 	return nil
 }
 
-func contains(s []uint16, e uint16) bool {
-	for _, a := range s {
-		if a == e {
-			return true
-		}
-	}
-	return false
-}
-
 // updateStat reads /proc/stat through procfs and exports CPU-related metrics.
 func (c *cpuCollector) updateIsolated(ch chan<- prometheus.Metric) error {
-	stats, err := c.fs.Stat()
-	if err != nil {
-		return err
-	}
-
-	c.updateCPUStats(stats.CPU)
-
-	// Acquire a lock to read the stats.
-	c.cpuStatsMutex.Lock()
-	defer c.cpuStatsMutex.Unlock()
-	for cpuID, _ := range c.cpuStats {
-		cpuNum := strconv.Itoa(cpuID)
-		isIsolated := 0.0
-		if contains(c.isolatedCpus, uint16(cpuID)) {
-			isIsolated = 1.0
-		}
-		ch <- prometheus.MustNewConstMetric(c.cpuIsolated, prometheus.GaugeValue, isIsolated, cpuNum)
+	for _, cpu := range c.isolatedCpus {
+		cpuNum := strconv.Itoa(int(cpu))
+		ch <- prometheus.MustNewConstMetric(c.cpuIsolated, prometheus.GaugeValue, 1.0, cpuNum)
 	}
 
 	return nil
diff --git a/collector/fixtures/e2e-output.txt b/collector/fixtures/e2e-output.txt
index d377115f..8a98a161 100644
--- a/collector/fixtures/e2e-output.txt
+++ b/collector/fixtures/e2e-output.txt
@@ -321,14 +321,11 @@ node_cpu_info{cachesize="8192 KB",core="3",cpu="3",family="6",microcode="0xb4",m
 node_cpu_info{cachesize="8192 KB",core="3",cpu="7",family="6",microcode="0xb4",model="142",model_name="Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz",package="0",stepping="10",vendor="GenuineIntel"} 1
 # HELP node_cpu_isolated Whether each core is isolated, information from /sys/devices/system/cpu/isolated.
 # TYPE node_cpu_isolated gauge
-node_cpu_isolated{cpu="0"} 0
 node_cpu_isolated{cpu="1"} 1
-node_cpu_isolated{cpu="2"} 0
 node_cpu_isolated{cpu="3"} 1
 node_cpu_isolated{cpu="4"} 1
 node_cpu_isolated{cpu="5"} 1
-node_cpu_isolated{cpu="6"} 0
-node_cpu_isolated{cpu="7"} 0
+node_cpu_isolated{cpu="9"} 1
 # HELP node_cpu_package_throttles_total Number of times this CPU package has been throttled.
 # TYPE node_cpu_package_throttles_total counter
 node_cpu_package_throttles_total{package="0"} 30