From 76b69c59b4b70125938fbf08ab153e7292c6d060 Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Fri, 4 Feb 2022 14:42:42 +0100 Subject: [PATCH] Switched to cclog.ComponentError() for error reporting in Read() --- collectors/README.md | 6 +++ collectors/cpufreqCpuinfoMetric.go | 13 +++-- collectors/cpufreqMetric.go | 10 ++-- collectors/gpfsMetric.go | 79 ++++++++++++++++++------------ collectors/numastatsMetric.go | 13 +++-- 5 files changed, 79 insertions(+), 42 deletions(-) diff --git a/collectors/README.md b/collectors/README.md index 5b650eb..8423c95 100644 --- a/collectors/README.md +++ b/collectors/README.md @@ -96,6 +96,12 @@ func (m *SampleCollector) Read(interval time.Duration, output chan lp.CCMetric) } // tags for the metric, if type != node use proper type and type-id tags := map[string]string{"type" : "node"} + + x, err := GetMetric() + if err != nil { + cclog.ComponentError(m.name, fmt.Sprintf("Read(): %v", err)) + } + // Each metric has exactly one field: value ! value := map[string]interface{}{"value": int(x)} y, err := lp.New("sample_metric", tags, m.meta, value, time.Now()) diff --git a/collectors/cpufreqCpuinfoMetric.go b/collectors/cpufreqCpuinfoMetric.go index 9c91a50..c77a981 100644 --- a/collectors/cpufreqCpuinfoMetric.go +++ b/collectors/cpufreqCpuinfoMetric.go @@ -5,13 +5,13 @@ import ( "encoding/json" "fmt" - "log" "os" "strconv" "strings" "time" - lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" + cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" + lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" ) // @@ -151,7 +151,6 @@ func (m *CPUFreqCpuInfoCollector) Init(config json.RawMessage) error { return nil } - func (m *CPUFreqCpuInfoCollector) Read(interval time.Duration, output chan lp.CCMetric) { if !m.init { return @@ -159,7 +158,9 @@ func (m *CPUFreqCpuInfoCollector) Read(interval time.Duration, output chan lp.CC const cpuInfoFile = "/proc/cpuinfo" file, err := os.Open(cpuInfoFile) if err != nil { - log.Printf("Failed to open '%s': %v", cpuInfoFile, err) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to open '%s': %v", cpuInfoFile, err)) return } defer file.Close() @@ -178,7 +179,9 @@ func (m *CPUFreqCpuInfoCollector) Read(interval time.Duration, output chan lp.CC if !t.isHT { value, err := strconv.ParseFloat(strings.TrimSpace(lineSplit[1]), 64) if err != nil { - log.Printf("Failed to convert cpu MHz to float: %v", err) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert cpu MHz to float: %v", err)) return } y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": value}, now) diff --git a/collectors/cpufreqMetric.go b/collectors/cpufreqMetric.go index f3309ff..b464160 100644 --- a/collectors/cpufreqMetric.go +++ b/collectors/cpufreqMetric.go @@ -4,13 +4,13 @@ import ( "bufio" "encoding/json" "fmt" - "log" "os" "path/filepath" "strconv" "strings" "time" + cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" "golang.org/x/sys/unix" ) @@ -200,12 +200,16 @@ func (m *CPUFreqCollector) Read(interval time.Duration, output chan lp.CCMetric) // Read current frequency line, ok := readOneLine(t.scalingCurFreqFile) if !ok { - log.Printf("CPUFreqCollector.Read(): Failed to read one line from file '%s'", t.scalingCurFreqFile) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to read one line from file '%s'", t.scalingCurFreqFile)) continue } cpuFreq, err := strconv.Atoi(line) if err != nil { - log.Printf("CPUFreqCollector.Read(): Failed to convert CPU frequency '%s': %v", line, err) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert CPU frequency '%s': %v", line, err)) continue } diff --git a/collectors/gpfsMetric.go b/collectors/gpfsMetric.go index 53db1c2..8055d4c 100644 --- a/collectors/gpfsMetric.go +++ b/collectors/gpfsMetric.go @@ -7,13 +7,13 @@ import ( "fmt" "io/ioutil" "log" - "os" "os/exec" "os/user" "strconv" "strings" "time" + cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" ) @@ -86,12 +86,15 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { cmd.Stderr = cmdStderr err := cmd.Run() if err != nil { - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): Failed to execute command \"%s\": %s\n", cmd.String(), err.Error()) - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): command exit code: \"%d\"\n", cmd.ProcessState.ExitCode()) - data, _ := ioutil.ReadAll(cmdStderr) - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): command stderr: \"%s\"\n", string(data)) - data, _ = ioutil.ReadAll(cmdStdout) - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): command stdout: \"%s\"\n", string(data)) + dataStdErr, _ := ioutil.ReadAll(cmdStderr) + dataStdOut, _ := ioutil.ReadAll(cmdStdout) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to execute command \"%s\": %v\n", cmd.String(), err), + fmt.Sprintf("Read(): command exit code: \"%d\"\n", cmd.ProcessState.ExitCode()), + fmt.Sprintf("Read(): command stderr: \"%s\"\n", string(dataStdErr)), + fmt.Sprintf("Read(): command stdout: \"%s\"\n", string(dataStdOut)), + ) return } @@ -113,7 +116,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { filesystem, ok := key_value["_fs_"] if !ok { - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): Failed to get filesystem name.\n") + cclog.ComponentError( + m.name, + "Read(): Failed to get filesystem name.") continue } @@ -122,26 +127,30 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { // return code rc, err := strconv.Atoi(key_value["_rc_"]) if err != nil { - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): Failed to convert return code: %s\n", err.Error()) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert return code '%s' to int: %v", key_value["_rc_"], err)) continue } if rc != 0 { - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): Filesystem %s not ok.", filesystem) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Filesystem %s not ok.", filesystem)) continue } sec, err := strconv.ParseInt(key_value["_t_"], 10, 64) if err != nil { - fmt.Fprintf(os.Stderr, - "GpfsCollector.Read(): Failed to convert seconds to int '%s': %v\n", - key_value["_t_"], err) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert seconds '%s' to int: %v", key_value["_t_"], err)) continue } msec, err := strconv.ParseInt(key_value["_tu_"], 10, 64) if err != nil { - fmt.Fprintf(os.Stderr, - "GpfsCollector.Read(): Failed to convert micro seconds to int '%s': %v\n", - key_value["_tu_"], err) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert micro seconds '%s' to int: %v", key_value["_tu_"], err)) continue } timestamp := time.Unix(sec, msec*1000) @@ -149,9 +158,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { // bytes read bytesRead, err := strconv.ParseInt(key_value["_br_"], 10, 64) if err != nil { - fmt.Fprintf(os.Stderr, - "GpfsCollector.Read(): Failed to convert bytes read '%s': %s\n", - key_value["_br_"], err.Error()) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert bytes read '%s' to int: %v", key_value["_br_"], err)) continue } @@ -163,9 +172,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { // bytes written bytesWritten, err := strconv.ParseInt(key_value["_bw_"], 10, 64) if err != nil { - fmt.Fprintf(os.Stderr, - "GpfsCollector.Read(): Failed to convert bytes written '%s': %s\n", - key_value["_bw_"], err.Error()) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert bytes written '%s' to int: %v", key_value["_bw_"], err)) continue } @@ -177,9 +186,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { // number of opens numOpens, err := strconv.ParseInt(key_value["_oc_"], 10, 64) if err != nil { - fmt.Fprintf(os.Stderr, - "GpfsCollector.Read(): Failed to convert number of opens '%s': %s\n", - key_value["_oc_"], err.Error()) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert number of opens '%s' to int: %v", key_value["_oc_"], err)) continue } y, err = lp.New("gpfs_num_opens", m.tags, m.meta, map[string]interface{}{"value": numOpens}, timestamp) @@ -190,7 +199,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { // number of closes numCloses, err := strconv.ParseInt(key_value["_cc_"], 10, 64) if err != nil { - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): Failed to convert number of closes: %s\n", err.Error()) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert number of closes: '%s' to int: %v", key_value["_cc_"], err)) continue } y, err = lp.New("gpfs_num_closes", m.tags, m.meta, map[string]interface{}{"value": numCloses}, timestamp) @@ -201,7 +212,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { // number of reads numReads, err := strconv.ParseInt(key_value["_rdc_"], 10, 64) if err != nil { - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): Failed to convert number of reads: %s\n", err.Error()) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert number of reads: '%s' to int: %v", key_value["_rdc_"], err)) continue } y, err = lp.New("gpfs_num_reads", m.tags, m.meta, map[string]interface{}{"value": numReads}, timestamp) @@ -212,7 +225,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { // number of writes numWrites, err := strconv.ParseInt(key_value["_wc_"], 10, 64) if err != nil { - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): Failed to convert number of writes: %s\n", err.Error()) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert number of writes: '%s' to int: %v", key_value["_wc_"], err)) continue } y, err = lp.New("gpfs_num_writes", m.tags, m.meta, map[string]interface{}{"value": numWrites}, timestamp) @@ -223,7 +238,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { // number of read directories numReaddirs, err := strconv.ParseInt(key_value["_dir_"], 10, 64) if err != nil { - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): Failed to convert number of read directories: %s\n", err.Error()) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert number of read directories: '%s' to int: %v", key_value["_dir_"], err)) continue } y, err = lp.New("gpfs_num_readdirs", m.tags, m.meta, map[string]interface{}{"value": numReaddirs}, timestamp) @@ -234,7 +251,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) { // Number of inode updates numInodeUpdates, err := strconv.ParseInt(key_value["_iu_"], 10, 64) if err != nil { - fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): Failed to convert Number of inode updates: %s\n", err.Error()) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert number of inode updates: '%s' to int: %v", key_value["_iu_"], err)) continue } y, err = lp.New("gpfs_num_inode_updates", m.tags, m.meta, map[string]interface{}{"value": numInodeUpdates}, timestamp) diff --git a/collectors/numastatsMetric.go b/collectors/numastatsMetric.go index e6f31a2..08bd51b 100644 --- a/collectors/numastatsMetric.go +++ b/collectors/numastatsMetric.go @@ -4,13 +4,13 @@ import ( "bufio" "encoding/json" "fmt" - "log" "os" "path/filepath" "strconv" "strings" "time" + cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" ) @@ -65,10 +65,10 @@ func (m *NUMAStatsCollector) Init(config json.RawMessage) error { globPattern := filepath.Join(baseDir, "node[0-9]*") dirs, err := filepath.Glob(globPattern) if err != nil { - return fmt.Errorf("unable to glob files with pattern %s", globPattern) + return fmt.Errorf("unable to glob files with pattern '%s'", globPattern) } if dirs == nil { - return fmt.Errorf("unable to find any files with pattern %s", globPattern) + return fmt.Errorf("unable to find any files with pattern '%s'", globPattern) } m.topology = make([]NUMAStatsCollectorTopolgy, 0, len(dirs)) for _, dir := range dirs { @@ -97,6 +97,9 @@ func (m *NUMAStatsCollector) Read(interval time.Duration, output chan lp.CCMetri now := time.Now() file, err := os.Open(t.file) if err != nil { + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to open file '%s': %v", t.file, err)) return } scanner := bufio.NewScanner(file) @@ -108,7 +111,9 @@ func (m *NUMAStatsCollector) Read(interval time.Duration, output chan lp.CCMetri key := split[0] value, err := strconv.ParseInt(split[1], 10, 64) if err != nil { - log.Printf("failed to convert %s='%s' to int64: %v", key, split[1], err) + cclog.ComponentError( + m.name, + fmt.Sprintf("Read(): Failed to convert %s='%s' to int64: %v", key, split[1], err)) continue } y, err := lp.New("numastats_"+key, t.tagSet, m.meta, map[string]interface{}{"value": value}, now)