Switched to cclog.ComponentError() for error reporting in Read()

This commit is contained in:
Holger Obermaier 2022-02-04 14:42:42 +01:00
parent 66b9a25a88
commit 76b69c59b4
5 changed files with 79 additions and 42 deletions

View File

@ -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 for the metric, if type != node use proper type and type-id
tags := map[string]string{"type" : "node"} 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 ! // Each metric has exactly one field: value !
value := map[string]interface{}{"value": int(x)} value := map[string]interface{}{"value": int(x)}
y, err := lp.New("sample_metric", tags, m.meta, value, time.Now()) y, err := lp.New("sample_metric", tags, m.meta, value, time.Now())

View File

@ -5,13 +5,13 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"time" "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 return nil
} }
func (m *CPUFreqCpuInfoCollector) Read(interval time.Duration, output chan lp.CCMetric) { func (m *CPUFreqCpuInfoCollector) Read(interval time.Duration, output chan lp.CCMetric) {
if !m.init { if !m.init {
return return
@ -159,7 +158,9 @@ func (m *CPUFreqCpuInfoCollector) Read(interval time.Duration, output chan lp.CC
const cpuInfoFile = "/proc/cpuinfo" const cpuInfoFile = "/proc/cpuinfo"
file, err := os.Open(cpuInfoFile) file, err := os.Open(cpuInfoFile)
if err != nil { 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 return
} }
defer file.Close() defer file.Close()
@ -178,7 +179,9 @@ func (m *CPUFreqCpuInfoCollector) Read(interval time.Duration, output chan lp.CC
if !t.isHT { if !t.isHT {
value, err := strconv.ParseFloat(strings.TrimSpace(lineSplit[1]), 64) value, err := strconv.ParseFloat(strings.TrimSpace(lineSplit[1]), 64)
if err != nil { 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 return
} }
y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": value}, now) y, err := lp.New("cpufreq", t.tagSet, m.meta, map[string]interface{}{"value": value}, now)

View File

@ -4,13 +4,13 @@ import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
@ -200,12 +200,16 @@ func (m *CPUFreqCollector) Read(interval time.Duration, output chan lp.CCMetric)
// Read current frequency // Read current frequency
line, ok := readOneLine(t.scalingCurFreqFile) line, ok := readOneLine(t.scalingCurFreqFile)
if !ok { 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 continue
} }
cpuFreq, err := strconv.Atoi(line) cpuFreq, err := strconv.Atoi(line)
if err != nil { 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 continue
} }

View File

@ -7,13 +7,13 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"os/exec" "os/exec"
"os/user" "os/user"
"strconv" "strconv"
"strings" "strings"
"time" "time"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" 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 cmd.Stderr = cmdStderr
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): Failed to execute command \"%s\": %s\n", cmd.String(), err.Error()) dataStdErr, _ := ioutil.ReadAll(cmdStderr)
fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): command exit code: \"%d\"\n", cmd.ProcessState.ExitCode()) dataStdOut, _ := ioutil.ReadAll(cmdStdout)
data, _ := ioutil.ReadAll(cmdStderr) cclog.ComponentError(
fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): command stderr: \"%s\"\n", string(data)) m.name,
data, _ = ioutil.ReadAll(cmdStdout) fmt.Sprintf("Read(): Failed to execute command \"%s\": %v\n", cmd.String(), err),
fmt.Fprintf(os.Stderr, "GpfsCollector.Read(): command stdout: \"%s\"\n", string(data)) 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 return
} }
@ -113,7 +116,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) {
filesystem, ok := key_value["_fs_"] filesystem, ok := key_value["_fs_"]
if !ok { 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 continue
} }
@ -122,26 +127,30 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) {
// return code // return code
rc, err := strconv.Atoi(key_value["_rc_"]) rc, err := strconv.Atoi(key_value["_rc_"])
if err != nil { 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 continue
} }
if rc != 0 { 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 continue
} }
sec, err := strconv.ParseInt(key_value["_t_"], 10, 64) sec, err := strconv.ParseInt(key_value["_t_"], 10, 64)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, cclog.ComponentError(
"GpfsCollector.Read(): Failed to convert seconds to int '%s': %v\n", m.name,
key_value["_t_"], err) fmt.Sprintf("Read(): Failed to convert seconds '%s' to int: %v", key_value["_t_"], err))
continue continue
} }
msec, err := strconv.ParseInt(key_value["_tu_"], 10, 64) msec, err := strconv.ParseInt(key_value["_tu_"], 10, 64)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, cclog.ComponentError(
"GpfsCollector.Read(): Failed to convert micro seconds to int '%s': %v\n", m.name,
key_value["_tu_"], err) fmt.Sprintf("Read(): Failed to convert micro seconds '%s' to int: %v", key_value["_tu_"], err))
continue continue
} }
timestamp := time.Unix(sec, msec*1000) timestamp := time.Unix(sec, msec*1000)
@ -149,9 +158,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) {
// bytes read // bytes read
bytesRead, err := strconv.ParseInt(key_value["_br_"], 10, 64) bytesRead, err := strconv.ParseInt(key_value["_br_"], 10, 64)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, cclog.ComponentError(
"GpfsCollector.Read(): Failed to convert bytes read '%s': %s\n", m.name,
key_value["_br_"], err.Error()) fmt.Sprintf("Read(): Failed to convert bytes read '%s' to int: %v", key_value["_br_"], err))
continue continue
} }
@ -163,9 +172,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) {
// bytes written // bytes written
bytesWritten, err := strconv.ParseInt(key_value["_bw_"], 10, 64) bytesWritten, err := strconv.ParseInt(key_value["_bw_"], 10, 64)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, cclog.ComponentError(
"GpfsCollector.Read(): Failed to convert bytes written '%s': %s\n", m.name,
key_value["_bw_"], err.Error()) fmt.Sprintf("Read(): Failed to convert bytes written '%s' to int: %v", key_value["_bw_"], err))
continue continue
} }
@ -177,9 +186,9 @@ func (m *GpfsCollector) Read(interval time.Duration, output chan lp.CCMetric) {
// number of opens // number of opens
numOpens, err := strconv.ParseInt(key_value["_oc_"], 10, 64) numOpens, err := strconv.ParseInt(key_value["_oc_"], 10, 64)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, cclog.ComponentError(
"GpfsCollector.Read(): Failed to convert number of opens '%s': %s\n", m.name,
key_value["_oc_"], err.Error()) fmt.Sprintf("Read(): Failed to convert number of opens '%s' to int: %v", key_value["_oc_"], err))
continue continue
} }
y, err = lp.New("gpfs_num_opens", m.tags, m.meta, map[string]interface{}{"value": numOpens}, timestamp) 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 // number of closes
numCloses, err := strconv.ParseInt(key_value["_cc_"], 10, 64) numCloses, err := strconv.ParseInt(key_value["_cc_"], 10, 64)
if err != nil { 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 continue
} }
y, err = lp.New("gpfs_num_closes", m.tags, m.meta, map[string]interface{}{"value": numCloses}, timestamp) 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 // number of reads
numReads, err := strconv.ParseInt(key_value["_rdc_"], 10, 64) numReads, err := strconv.ParseInt(key_value["_rdc_"], 10, 64)
if err != nil { 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 continue
} }
y, err = lp.New("gpfs_num_reads", m.tags, m.meta, map[string]interface{}{"value": numReads}, timestamp) 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 // number of writes
numWrites, err := strconv.ParseInt(key_value["_wc_"], 10, 64) numWrites, err := strconv.ParseInt(key_value["_wc_"], 10, 64)
if err != nil { 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 continue
} }
y, err = lp.New("gpfs_num_writes", m.tags, m.meta, map[string]interface{}{"value": numWrites}, timestamp) 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 // number of read directories
numReaddirs, err := strconv.ParseInt(key_value["_dir_"], 10, 64) numReaddirs, err := strconv.ParseInt(key_value["_dir_"], 10, 64)
if err != nil { 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 continue
} }
y, err = lp.New("gpfs_num_readdirs", m.tags, m.meta, map[string]interface{}{"value": numReaddirs}, timestamp) 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 // Number of inode updates
numInodeUpdates, err := strconv.ParseInt(key_value["_iu_"], 10, 64) numInodeUpdates, err := strconv.ParseInt(key_value["_iu_"], 10, 64)
if err != nil { 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 continue
} }
y, err = lp.New("gpfs_num_inode_updates", m.tags, m.meta, map[string]interface{}{"value": numInodeUpdates}, timestamp) y, err = lp.New("gpfs_num_inode_updates", m.tags, m.meta, map[string]interface{}{"value": numInodeUpdates}, timestamp)

View File

@ -4,13 +4,13 @@ import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" 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]*") globPattern := filepath.Join(baseDir, "node[0-9]*")
dirs, err := filepath.Glob(globPattern) dirs, err := filepath.Glob(globPattern)
if err != nil { 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 { 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)) m.topology = make([]NUMAStatsCollectorTopolgy, 0, len(dirs))
for _, dir := range dirs { for _, dir := range dirs {
@ -97,6 +97,9 @@ func (m *NUMAStatsCollector) Read(interval time.Duration, output chan lp.CCMetri
now := time.Now() now := time.Now()
file, err := os.Open(t.file) file, err := os.Open(t.file)
if err != nil { if err != nil {
cclog.ComponentError(
m.name,
fmt.Sprintf("Read(): Failed to open file '%s': %v", t.file, err))
return return
} }
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
@ -108,7 +111,9 @@ func (m *NUMAStatsCollector) Read(interval time.Duration, output chan lp.CCMetri
key := split[0] key := split[0]
value, err := strconv.ParseInt(split[1], 10, 64) value, err := strconv.ParseInt(split[1], 10, 64)
if err != nil { 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 continue
} }
y, err := lp.New("numastats_"+key, t.tagSet, m.meta, map[string]interface{}{"value": value}, now) y, err := lp.New("numastats_"+key, t.tagSet, m.meta, map[string]interface{}{"value": value}, now)