Simplified code

This commit is contained in:
Holger Obermaier 2022-01-24 11:31:45 +01:00
parent 25b9268b24
commit daa7c6bf99

View File

@ -15,8 +15,6 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
var warnLog *log.Logger = log.New(os.Stderr, "Warning: ", log.LstdFlags)
// //
// readOneLine reads one line from a file. // readOneLine reads one line from a file.
// It returns ok when file was successfully read. // It returns ok when file was successfully read.
@ -138,7 +136,8 @@ func (m *CPUFreqCollector) Init(config []byte) error {
m.cpus, m.cpus,
CPUFreqCollectorCPU{ CPUFreqCollectorCPU{
tagSet: map[string]string{ tagSet: map[string]string{
"coreID": strings.TrimSpace(coreID), "type": "cpu",
"type-id": strings.TrimSpace(coreID),
"packageID": strings.TrimSpace(packageID), "packageID": strings.TrimSpace(packageID),
}, },
scalingCurFreqFile: scalingCurFreqFile, scalingCurFreqFile: scalingCurFreqFile,
@ -150,8 +149,9 @@ func (m *CPUFreqCollector) Init(config []byte) error {
numPackages := strconv.Itoa(maxPackageID + 1) numPackages := strconv.Itoa(maxPackageID + 1)
numCores := strconv.Itoa(maxCoreID + 1) numCores := strconv.Itoa(maxCoreID + 1)
for i := range m.cpus { for i := range m.cpus {
m.cpus[i].tagSet["num_core"] = numCores c := &m.cpus[i]
m.cpus[i].tagSet["num_package"] = numPackages c.tagSet["num_core"] = numCores
c.tagSet["num_package"] = numPackages
} }
m.init = true m.init = true
@ -163,21 +163,23 @@ func (m *CPUFreqCollector) Read(interval time.Duration, out *[]lp.MutableMetric)
return return
} }
for _, cpu := range m.cpus { now := time.Now()
for i := range m.cpus {
cpu := &m.cpus[i]
// Read current frequency // Read current frequency
line, ok := readOneLine(cpu.scalingCurFreqFile) line, ok := readOneLine(cpu.scalingCurFreqFile)
if !ok { 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 continue
} }
cpuFreq, err := strconv.Atoi(line) cpuFreq, err := strconv.Atoi(line)
if err != nil { 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 continue
} }
value := map[string]interface{}{"value": cpuFreq} y, err := lp.New("cpufreq", cpu.tagSet, map[string]interface{}{"value": cpuFreq}, now)
y, err := lp.New("cpufreq", cpu.tagSet, value, time.Now())
if err == nil { if err == nil {
*out = append(*out, y) *out = append(*out, y)
} }