From bcce471b271cd70c371de1855184afebb6a7c585 Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Mon, 24 Jan 2022 11:31:45 +0100 Subject: [PATCH] Simplified code --- collectors/cpufreqMetric.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/collectors/cpufreqMetric.go b/collectors/cpufreqMetric.go index 94f8f4a..f5a10bc 100644 --- a/collectors/cpufreqMetric.go +++ b/collectors/cpufreqMetric.go @@ -15,8 +15,6 @@ import ( "golang.org/x/sys/unix" ) -var warnLog *log.Logger = log.New(os.Stderr, "Warning: ", log.LstdFlags) - // // readOneLine reads one line from a file. // It returns ok when file was successfully read. @@ -138,7 +136,8 @@ func (m *CPUFreqCollector) Init(config []byte) error { m.cpus, CPUFreqCollectorCPU{ tagSet: map[string]string{ - "coreID": strings.TrimSpace(coreID), + "type": "cpu", + "type-id": strings.TrimSpace(coreID), "packageID": strings.TrimSpace(packageID), }, scalingCurFreqFile: scalingCurFreqFile, @@ -150,8 +149,9 @@ func (m *CPUFreqCollector) Init(config []byte) error { numPackages := strconv.Itoa(maxPackageID + 1) numCores := strconv.Itoa(maxCoreID + 1) for i := range m.cpus { - m.cpus[i].tagSet["num_core"] = numCores - m.cpus[i].tagSet["num_package"] = numPackages + c := &m.cpus[i] + c.tagSet["num_core"] = numCores + c.tagSet["num_package"] = numPackages } m.init = true @@ -163,21 +163,23 @@ func (m *CPUFreqCollector) Read(interval time.Duration, out *[]lp.MutableMetric) return } - for _, cpu := range m.cpus { + now := time.Now() + for i := range m.cpus { + cpu := &m.cpus[i] + // Read current frequency line, ok := readOneLine(cpu.scalingCurFreqFile) if !ok { - warnLog.Printf("CPUFreqCollector.Read(): Failed to read one line from file '%s'", cpu.scalingCurFreqFile) + log.Printf("CPUFreqCollector.Read(): Failed to read one line from file '%s'", cpu.scalingCurFreqFile) continue } cpuFreq, err := strconv.Atoi(line) if err != nil { - warnLog.Printf("CPUFreqCollector.Read(): Failed to convert CPU frequency '%s': %v", line, err) + log.Printf("CPUFreqCollector.Read(): Failed to convert CPU frequency '%s': %v", line, err) continue } - value := map[string]interface{}{"value": cpuFreq} - y, err := lp.New("cpufreq", cpu.tagSet, value, time.Now()) + y, err := lp.New("cpufreq", cpu.tagSet, map[string]interface{}{"value": cpuFreq}, now) if err == nil { *out = append(*out, y) }