From c205d347d1e5ee1c68694f3fcb026a10e18da43f Mon Sep 17 00:00:00 2001 From: Paulo Sousa Date: Thu, 23 May 2024 13:11:50 -0300 Subject: [PATCH] fix lint --- cmd/healthchecker/health_checker.go | 12 +++++- cmd/healthchecker/options/options.go | 2 +- .../node_problem_detector_unix.go | 4 +- cmd/options/options.go | 4 +- pkg/custompluginmonitor/plugin/plugin_test.go | 5 ++- pkg/custompluginmonitor/types/config_test.go | 5 ++- pkg/exporters/k8sexporter/k8s_exporter.go | 2 +- .../prometheusexporter/prometheus_exporter.go | 1 - .../stackdriver/stackdriver_exporter.go | 1 - pkg/problemmetrics/problem_metrics_test.go | 10 ++++- pkg/systemlogmonitor/log_buffer.go | 4 +- pkg/systemstatsmonitor/cpu_collector.go | 20 +++++----- pkg/systemstatsmonitor/cpu_collector_unix.go | 40 ++++++++++--------- pkg/systemstatsmonitor/disk_collector.go | 28 ++++++------- pkg/systemstatsmonitor/host_collector.go | 2 +- .../memory_collector_unix.go | 26 ++++++------ pkg/systemstatsmonitor/net_collector.go | 6 ++- pkg/systemstatsmonitor/osfeature_collector.go | 14 +++---- pkg/util/http.go | 4 +- pkg/util/metrics/fakes_test.go | 5 ++- pkg/util/metrics/metric_float64.go | 5 ++- pkg/util/metrics/metric_int64.go | 5 ++- pkg/util/nethealth/nethealth.go | 4 +- 23 files changed, 122 insertions(+), 87 deletions(-) diff --git a/cmd/healthchecker/health_checker.go b/cmd/healthchecker/health_checker.go index cd7b64bdd..afb6895c3 100644 --- a/cmd/healthchecker/health_checker.go +++ b/cmd/healthchecker/health_checker.go @@ -39,8 +39,16 @@ func main() { } }) pflag.CommandLine.AddGoFlagSet(flag.CommandLine) - pflag.CommandLine.MarkHidden("vmodule") - pflag.CommandLine.MarkHidden("logtostderr") + err := pflag.CommandLine.MarkHidden("vmodule") + if err != nil { + fmt.Println(err) + os.Exit(int(types.Unknown)) + } + err = pflag.CommandLine.MarkHidden("logtostderr") + if err != nil { + fmt.Println(err) + os.Exit(int(types.Unknown)) + } hco := options.NewHealthCheckerOptions() hco.AddFlags(pflag.CommandLine) diff --git a/cmd/healthchecker/options/options.go b/cmd/healthchecker/options/options.go index 5711fc324..106ba69cd 100644 --- a/cmd/healthchecker/options/options.go +++ b/cmd/healthchecker/options/options.go @@ -52,7 +52,7 @@ func (hco *HealthCheckerOptions) AddFlags(fs *pflag.FlagSet) { "The component to check health for. Supports kubelet, docker, kube-proxy, and cri") // Deprecated: For backward compatibility on linux environment. Going forward "service" will be used instead of systemd-service if runtime.GOOS == "linux" { - fs.MarkDeprecated("systemd-service", "please use --service flag instead") + _ = fs.MarkDeprecated("systemd-service", "please use --service flag instead") fs.StringVar(&hco.Service, "systemd-service", "", "The underlying service responsible for the component. Set to the corresponding component for docker and kubelet, containerd for cri.") } diff --git a/cmd/nodeproblemdetector/node_problem_detector_unix.go b/cmd/nodeproblemdetector/node_problem_detector_unix.go index 0ea4bc07b..3e7d24e0a 100644 --- a/cmd/nodeproblemdetector/node_problem_detector_unix.go +++ b/cmd/nodeproblemdetector/node_problem_detector_unix.go @@ -37,8 +37,8 @@ func main() { } }) pflag.CommandLine.AddGoFlagSet(flag.CommandLine) - pflag.CommandLine.MarkHidden("vmodule") - pflag.CommandLine.MarkHidden("logtostderr") + _ = pflag.CommandLine.MarkHidden("vmodule") + _ = pflag.CommandLine.MarkHidden("logtostderr") npdo := options.NewNodeProblemDetectorOptions() npdo.AddFlags(pflag.CommandLine) diff --git a/cmd/options/options.go b/cmd/options/options.go index f0de27c79..4762e9269 100644 --- a/cmd/options/options.go +++ b/cmd/options/options.go @@ -102,10 +102,10 @@ func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions { func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) { fs.StringSliceVar(&npdo.SystemLogMonitorConfigPaths, "system-log-monitors", []string{}, "List of paths to system log monitor config files, comma separated.") - fs.MarkDeprecated("system-log-monitors", "replaced by --config.system-log-monitor. NPD will panic if both --system-log-monitors and --config.system-log-monitor are set.") + _ = fs.MarkDeprecated("system-log-monitors", "replaced by --config.system-log-monitor. NPD will panic if both --system-log-monitors and --config.system-log-monitor are set.") fs.StringSliceVar(&npdo.CustomPluginMonitorConfigPaths, "custom-plugin-monitors", []string{}, "List of paths to custom plugin monitor config files, comma separated.") - fs.MarkDeprecated("custom-plugin-monitors", "replaced by --config.custom-plugin-monitor. NPD will panic if both --custom-plugin-monitors and --config.custom-plugin-monitor are set.") + _ = fs.MarkDeprecated("custom-plugin-monitors", "replaced by --config.custom-plugin-monitor. NPD will panic if both --custom-plugin-monitors and --config.custom-plugin-monitor are set.") fs.BoolVar(&npdo.EnableK8sExporter, "enable-k8s-exporter", true, "Enables reporting to Kubernetes API server.") fs.StringVar(&npdo.EventNamespace, "event-namespace", "", "Namespace for recorded Kubernetes events.") fs.StringVar(&npdo.ApiServerOverride, "apiserver-override", diff --git a/pkg/custompluginmonitor/plugin/plugin_test.go b/pkg/custompluginmonitor/plugin/plugin_test.go index e6b558304..574d9dacd 100644 --- a/pkg/custompluginmonitor/plugin/plugin_test.go +++ b/pkg/custompluginmonitor/plugin/plugin_test.go @@ -103,7 +103,10 @@ func TestNewPluginRun(t *testing.T) { utMeta := v t.Run(desp, func(t *testing.T) { conf := cpmtypes.CustomPluginConfig{} - (&conf).ApplyConfiguration() + err := (&conf).ApplyConfiguration() + if err != nil { + t.Errorf("Error in applying configuration: %v", err) + } p := Plugin{config: conf} gotExitStatus, gotOutput := p.run(utMeta.Rule) // cut at position max_output_length if expected output is longer than max_output_length bytes diff --git a/pkg/custompluginmonitor/types/config_test.go b/pkg/custompluginmonitor/types/config_test.go index 2649910f6..98ba01207 100644 --- a/pkg/custompluginmonitor/types/config_test.go +++ b/pkg/custompluginmonitor/types/config_test.go @@ -218,7 +218,10 @@ func TestCustomPluginConfigApplyConfiguration(t *testing.T) { } for desp, utMeta := range utMetas { - (&utMeta.Orig).ApplyConfiguration() + err := (&utMeta.Orig).ApplyConfiguration() + if err != nil { + t.Errorf("Error in apply configuration for %q. Error: %v", desp, err) + } if !reflect.DeepEqual(utMeta.Orig, utMeta.Wanted) { t.Errorf("Error in apply configuration for %q", desp) t.Errorf("Wanted: %+v. \nGot: %+v", utMeta.Wanted, utMeta.Orig) diff --git a/pkg/exporters/k8sexporter/k8s_exporter.go b/pkg/exporters/k8sexporter/k8s_exporter.go index 31ec9c793..d7220b6fe 100644 --- a/pkg/exporters/k8sexporter/k8s_exporter.go +++ b/pkg/exporters/k8sexporter/k8s_exporter.go @@ -87,7 +87,7 @@ func (ke *k8sExporter) startHTTPReporting(npdo *options.NodeProblemDetectorOptio // logic in the future. mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - w.Write([]byte("ok")) + _, _ = w.Write([]byte("ok")) }) // Add the handler to serve condition http request. diff --git a/pkg/exporters/prometheusexporter/prometheus_exporter.go b/pkg/exporters/prometheusexporter/prometheus_exporter.go index eba07425f..2d95e0325 100644 --- a/pkg/exporters/prometheusexporter/prometheus_exporter.go +++ b/pkg/exporters/prometheusexporter/prometheus_exporter.go @@ -56,5 +56,4 @@ func NewExporterOrDie(npdo *options.NodeProblemDetectorOptions) types.Exporter { // ExportProblems does nothing. // Prometheus exporter only exports metrics. func (pe *prometheusExporter) ExportProblems(status *types.Status) { - return } diff --git a/pkg/exporters/stackdriver/stackdriver_exporter.go b/pkg/exporters/stackdriver/stackdriver_exporter.go index e333eb656..49087b634 100644 --- a/pkg/exporters/stackdriver/stackdriver_exporter.go +++ b/pkg/exporters/stackdriver/stackdriver_exporter.go @@ -186,7 +186,6 @@ func (se *stackdriverExporter) populateMetadataOrDie() { // ExportProblems does nothing. // Stackdriver exporter only exports metrics. func (se *stackdriverExporter) ExportProblems(status *types.Status) { - return } type commandLineOptions struct { diff --git a/pkg/problemmetrics/problem_metrics_test.go b/pkg/problemmetrics/problem_metrics_test.go index ff9ff8a8c..92927623d 100644 --- a/pkg/problemmetrics/problem_metrics_test.go +++ b/pkg/problemmetrics/problem_metrics_test.go @@ -118,7 +118,10 @@ func TestNewProblem(t *testing.T) { pmm, fakeProblemCounter, fakeProblemGauge := NewProblemMetricsManagerStub() for idx, reason := range test.reasons { - pmm.IncrementProblemCounter(reason, test.counts[idx]) + err := pmm.IncrementProblemCounter(reason, test.counts[idx]) + if err != nil { + t.Fatalf("failed to increment problem counter: %v", err) + } } gotMetrics := append(fakeProblemCounter.ListMetrics(), fakeProblemGauge.ListMetrics()...) @@ -266,7 +269,10 @@ func TestSetProblemGauge(t *testing.T) { pmm, fakeProblemCounter, fakeProblemGauge := NewProblemMetricsManagerStub() for _, argument := range test.arguments { - pmm.SetProblemGauge(argument.problemType, argument.reason, argument.value) + err := pmm.SetProblemGauge(argument.problemType, argument.reason, argument.value) + if err != nil { + t.Fatalf("failed to set problem gauge: %v", err) + } } gotMetrics := append(fakeProblemCounter.ListMetrics(), fakeProblemGauge.ListMetrics()...) diff --git a/pkg/systemlogmonitor/log_buffer.go b/pkg/systemlogmonitor/log_buffer.go index 1dc4bc402..ed847de6e 100644 --- a/pkg/systemlogmonitor/log_buffer.go +++ b/pkg/systemlogmonitor/log_buffer.go @@ -47,8 +47,8 @@ type logBuffer struct { // lines of patterns we support. func NewLogBuffer(maxLines int) *logBuffer { return &logBuffer{ - buffer: make([]*types.Log, maxLines, maxLines), - msg: make([]string, maxLines, maxLines), + buffer: make([]*types.Log, maxLines), + msg: make([]string, maxLines), max: maxLines, } } diff --git a/pkg/systemstatsmonitor/cpu_collector.go b/pkg/systemstatsmonitor/cpu_collector.go index 96e6ad416..8a3919cb8 100644 --- a/pkg/systemstatsmonitor/cpu_collector.go +++ b/pkg/systemstatsmonitor/cpu_collector.go @@ -187,34 +187,34 @@ func (cc *cpuCollector) recordUsage() { } timersStat := timersStats[0] - cc.mUsageTime.Record(map[string]string{stateLabel: "user"}, clockTick*timersStat.User-cc.lastUsageTime["user"]) + _ = cc.mUsageTime.Record(map[string]string{stateLabel: "user"}, clockTick*timersStat.User-cc.lastUsageTime["user"]) cc.lastUsageTime["user"] = clockTick * timersStat.User - cc.mUsageTime.Record(map[string]string{stateLabel: "system"}, clockTick*timersStat.System-cc.lastUsageTime["system"]) + _ = cc.mUsageTime.Record(map[string]string{stateLabel: "system"}, clockTick*timersStat.System-cc.lastUsageTime["system"]) cc.lastUsageTime["system"] = clockTick * timersStat.System - cc.mUsageTime.Record(map[string]string{stateLabel: "idle"}, clockTick*timersStat.Idle-cc.lastUsageTime["idle"]) + _ = cc.mUsageTime.Record(map[string]string{stateLabel: "idle"}, clockTick*timersStat.Idle-cc.lastUsageTime["idle"]) cc.lastUsageTime["idle"] = clockTick * timersStat.Idle - cc.mUsageTime.Record(map[string]string{stateLabel: "nice"}, clockTick*timersStat.Nice-cc.lastUsageTime["nice"]) + _ = cc.mUsageTime.Record(map[string]string{stateLabel: "nice"}, clockTick*timersStat.Nice-cc.lastUsageTime["nice"]) cc.lastUsageTime["nice"] = clockTick * timersStat.Nice - cc.mUsageTime.Record(map[string]string{stateLabel: "iowait"}, clockTick*timersStat.Iowait-cc.lastUsageTime["iowait"]) + _ = cc.mUsageTime.Record(map[string]string{stateLabel: "iowait"}, clockTick*timersStat.Iowait-cc.lastUsageTime["iowait"]) cc.lastUsageTime["iowait"] = clockTick * timersStat.Iowait - cc.mUsageTime.Record(map[string]string{stateLabel: "irq"}, clockTick*timersStat.Irq-cc.lastUsageTime["irq"]) + _ = cc.mUsageTime.Record(map[string]string{stateLabel: "irq"}, clockTick*timersStat.Irq-cc.lastUsageTime["irq"]) cc.lastUsageTime["irq"] = clockTick * timersStat.Irq - cc.mUsageTime.Record(map[string]string{stateLabel: "softirq"}, clockTick*timersStat.Softirq-cc.lastUsageTime["softirq"]) + _ = cc.mUsageTime.Record(map[string]string{stateLabel: "softirq"}, clockTick*timersStat.Softirq-cc.lastUsageTime["softirq"]) cc.lastUsageTime["softirq"] = clockTick * timersStat.Softirq - cc.mUsageTime.Record(map[string]string{stateLabel: "steal"}, clockTick*timersStat.Steal-cc.lastUsageTime["steal"]) + _ = cc.mUsageTime.Record(map[string]string{stateLabel: "steal"}, clockTick*timersStat.Steal-cc.lastUsageTime["steal"]) cc.lastUsageTime["steal"] = clockTick * timersStat.Steal - cc.mUsageTime.Record(map[string]string{stateLabel: "guest"}, clockTick*timersStat.Guest-cc.lastUsageTime["guest"]) + _ = cc.mUsageTime.Record(map[string]string{stateLabel: "guest"}, clockTick*timersStat.Guest-cc.lastUsageTime["guest"]) cc.lastUsageTime["guest"] = clockTick * timersStat.Guest - cc.mUsageTime.Record(map[string]string{stateLabel: "guest_nice"}, clockTick*timersStat.GuestNice-cc.lastUsageTime["guest_nice"]) + _ = cc.mUsageTime.Record(map[string]string{stateLabel: "guest_nice"}, clockTick*timersStat.GuestNice-cc.lastUsageTime["guest_nice"]) cc.lastUsageTime["guest_nice"] = clockTick * timersStat.GuestNice } diff --git a/pkg/systemstatsmonitor/cpu_collector_unix.go b/pkg/systemstatsmonitor/cpu_collector_unix.go index 3181fe0d4..46190ecf1 100644 --- a/pkg/systemstatsmonitor/cpu_collector_unix.go +++ b/pkg/systemstatsmonitor/cpu_collector_unix.go @@ -40,16 +40,16 @@ func (cc *cpuCollector) recordLoad() { } if cc.mRunnableTaskCount != nil { - cc.mRunnableTaskCount.Record(map[string]string{}, loadAvg.Load1) + _ = cc.mRunnableTaskCount.Record(map[string]string{}, loadAvg.Load1) } if cc.mCpuLoad1m != nil { - cc.mCpuLoad1m.Record(map[string]string{}, loadAvg.Load1) + _ = cc.mCpuLoad1m.Record(map[string]string{}, loadAvg.Load1) } if cc.mCpuLoad5m != nil { - cc.mCpuLoad5m.Record(map[string]string{}, loadAvg.Load5) + _ = cc.mCpuLoad5m.Record(map[string]string{}, loadAvg.Load5) } if cc.mCpuLoad15m != nil { - cc.mCpuLoad15m.Record(map[string]string{}, loadAvg.Load15) + _ = cc.mCpuLoad15m.Record(map[string]string{}, loadAvg.Load15) } } @@ -62,6 +62,10 @@ func (cc *cpuCollector) recordSystemStats() { } fs, err := procfs.NewFS(cc.procPath) + if err != nil { + klog.Errorf("Failed to open procfs: %v", err) + return + } stats, err := fs.Stat() if err != nil { klog.Errorf("Failed to retrieve cpu/process stats: %v", err) @@ -69,19 +73,19 @@ func (cc *cpuCollector) recordSystemStats() { } if cc.mSystemProcessesTotal != nil { - cc.mSystemProcessesTotal.Record(map[string]string{}, int64(stats.ProcessCreated)) + _ = cc.mSystemProcessesTotal.Record(map[string]string{}, int64(stats.ProcessCreated)) } if cc.mSystemProcsRunning != nil { - cc.mSystemProcsRunning.Record(map[string]string{}, int64(stats.ProcessesRunning)) + _ = cc.mSystemProcsRunning.Record(map[string]string{}, int64(stats.ProcessesRunning)) } if cc.mSystemProcsBlocked != nil { - cc.mSystemProcsBlocked.Record(map[string]string{}, int64(stats.ProcessesBlocked)) + _ = cc.mSystemProcsBlocked.Record(map[string]string{}, int64(stats.ProcessesBlocked)) } if cc.mSystemInterruptsTotal != nil { - cc.mSystemInterruptsTotal.Record(map[string]string{}, int64(stats.IRQTotal)) + _ = cc.mSystemInterruptsTotal.Record(map[string]string{}, int64(stats.IRQTotal)) } if cc.mSystemCPUStat != nil { @@ -90,25 +94,25 @@ func (cc *cpuCollector) recordSystemStats() { tags[cpuLabel] = fmt.Sprintf("cpu%d", i) tags[stageLabel] = "user" - cc.mSystemCPUStat.Record(tags, c.User) + _ = cc.mSystemCPUStat.Record(tags, c.User) tags[stageLabel] = "nice" - cc.mSystemCPUStat.Record(tags, c.Nice) + _ = cc.mSystemCPUStat.Record(tags, c.Nice) tags[stageLabel] = "system" - cc.mSystemCPUStat.Record(tags, c.System) + _ = cc.mSystemCPUStat.Record(tags, c.System) tags[stageLabel] = "idle" - cc.mSystemCPUStat.Record(tags, c.Idle) + _ = cc.mSystemCPUStat.Record(tags, c.Idle) tags[stageLabel] = "iowait" - cc.mSystemCPUStat.Record(tags, c.Iowait) + _ = cc.mSystemCPUStat.Record(tags, c.Iowait) tags[stageLabel] = "iRQ" - cc.mSystemCPUStat.Record(tags, c.IRQ) + _ = cc.mSystemCPUStat.Record(tags, c.IRQ) tags[stageLabel] = "softIRQ" - cc.mSystemCPUStat.Record(tags, c.SoftIRQ) + _ = cc.mSystemCPUStat.Record(tags, c.SoftIRQ) tags[stageLabel] = "steal" - cc.mSystemCPUStat.Record(tags, c.Steal) + _ = cc.mSystemCPUStat.Record(tags, c.Steal) tags[stageLabel] = "guest" - cc.mSystemCPUStat.Record(tags, c.Guest) + _ = cc.mSystemCPUStat.Record(tags, c.Guest) tags[stageLabel] = "guestNice" - cc.mSystemCPUStat.Record(tags, c.GuestNice) + _ = cc.mSystemCPUStat.Record(tags, c.GuestNice) } } } diff --git a/pkg/systemstatsmonitor/disk_collector.go b/pkg/systemstatsmonitor/disk_collector.go index b3b55f8a5..b0d7cf37b 100644 --- a/pkg/systemstatsmonitor/disk_collector.go +++ b/pkg/systemstatsmonitor/disk_collector.go @@ -188,10 +188,10 @@ func (dc *diskCollector) recordIOCounters(ioCountersStats map[string]disk.IOCoun dc.lastWeightedIO[deviceName] = ioCountersStat.WeightedIO if dc.mIOTime != nil { - dc.mIOTime.Record(tags, int64(ioCountersStat.IoTime-lastIOTime)) + _ = dc.mIOTime.Record(tags, int64(ioCountersStat.IoTime-lastIOTime)) } if dc.mWeightedIO != nil { - dc.mWeightedIO.Record(tags, int64(ioCountersStat.WeightedIO-lastWeightedIO)) + _ = dc.mWeightedIO.Record(tags, int64(ioCountersStat.WeightedIO-lastWeightedIO)) } if historyExist { avgQueueLen := float64(0.0) @@ -200,7 +200,7 @@ func (dc *diskCollector) recordIOCounters(ioCountersStats map[string]disk.IOCoun avgQueueLen = float64(ioCountersStat.WeightedIO-lastWeightedIO) / diffSampleTimeMs } if dc.mAvgQueueLen != nil { - dc.mAvgQueueLen.Record(tags, avgQueueLen) + _ = dc.mAvgQueueLen.Record(tags, avgQueueLen) } } @@ -208,19 +208,19 @@ func (dc *diskCollector) recordIOCounters(ioCountersStats map[string]disk.IOCoun tags = map[string]string{deviceNameLabel: deviceName, directionLabel: "read"} if dc.mOpsCount != nil { - dc.mOpsCount.Record(tags, int64(ioCountersStat.ReadCount-dc.lastReadCount[deviceName])) + _ = dc.mOpsCount.Record(tags, int64(ioCountersStat.ReadCount-dc.lastReadCount[deviceName])) dc.lastReadCount[deviceName] = ioCountersStat.ReadCount } if dc.mMergedOpsCount != nil { - dc.mMergedOpsCount.Record(tags, int64(ioCountersStat.MergedReadCount-dc.lastMergedReadCount[deviceName])) + _ = dc.mMergedOpsCount.Record(tags, int64(ioCountersStat.MergedReadCount-dc.lastMergedReadCount[deviceName])) dc.lastMergedReadCount[deviceName] = ioCountersStat.MergedReadCount } if dc.mOpsBytes != nil { - dc.mOpsBytes.Record(tags, int64(ioCountersStat.ReadBytes-dc.lastReadBytes[deviceName])) + _ = dc.mOpsBytes.Record(tags, int64(ioCountersStat.ReadBytes-dc.lastReadBytes[deviceName])) dc.lastReadBytes[deviceName] = ioCountersStat.ReadBytes } if dc.mOpsTime != nil { - dc.mOpsTime.Record(tags, int64(ioCountersStat.ReadTime-dc.lastReadTime[deviceName])) + _ = dc.mOpsTime.Record(tags, int64(ioCountersStat.ReadTime-dc.lastReadTime[deviceName])) dc.lastReadTime[deviceName] = ioCountersStat.ReadTime } @@ -228,19 +228,19 @@ func (dc *diskCollector) recordIOCounters(ioCountersStats map[string]disk.IOCoun tags = map[string]string{deviceNameLabel: deviceName, directionLabel: "write"} if dc.mOpsCount != nil { - dc.mOpsCount.Record(tags, int64(ioCountersStat.WriteCount-dc.lastWriteCount[deviceName])) + _ = dc.mOpsCount.Record(tags, int64(ioCountersStat.WriteCount-dc.lastWriteCount[deviceName])) dc.lastWriteCount[deviceName] = ioCountersStat.WriteCount } if dc.mMergedOpsCount != nil { - dc.mMergedOpsCount.Record(tags, int64(ioCountersStat.MergedWriteCount-dc.lastMergedWriteCount[deviceName])) + _ = dc.mMergedOpsCount.Record(tags, int64(ioCountersStat.MergedWriteCount-dc.lastMergedWriteCount[deviceName])) dc.lastMergedWriteCount[deviceName] = ioCountersStat.MergedWriteCount } if dc.mOpsBytes != nil { - dc.mOpsBytes.Record(tags, int64(ioCountersStat.WriteBytes-dc.lastWriteBytes[deviceName])) + _ = dc.mOpsBytes.Record(tags, int64(ioCountersStat.WriteBytes-dc.lastWriteBytes[deviceName])) dc.lastWriteBytes[deviceName] = ioCountersStat.WriteBytes } if dc.mOpsTime != nil { - dc.mOpsTime.Record(tags, int64(ioCountersStat.WriteTime-dc.lastWriteTime[deviceName])) + _ = dc.mOpsTime.Record(tags, int64(ioCountersStat.WriteTime-dc.lastWriteTime[deviceName])) dc.lastWriteTime[deviceName] = ioCountersStat.WriteTime } } @@ -301,10 +301,10 @@ func (dc *diskCollector) collect() { deviceName := strings.TrimPrefix(partition.Device, "/dev/") fstype := partition.Fstype opttypes := strings.Join(partition.Opts, ",") - dc.mBytesUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "free"}, int64(usageStat.Free)) - dc.mBytesUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "used"}, int64(usageStat.Used)) + _ = dc.mBytesUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "free"}, int64(usageStat.Free)) + _ = dc.mBytesUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "used"}, int64(usageStat.Used)) if dc.mPercentUsed != nil { - dc.mPercentUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "used"}, float64(usageStat.UsedPercent)) + _ = dc.mPercentUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "used"}, float64(usageStat.UsedPercent)) } } diff --git a/pkg/systemstatsmonitor/host_collector.go b/pkg/systemstatsmonitor/host_collector.go index c6c5ed38c..58446e522 100644 --- a/pkg/systemstatsmonitor/host_collector.go +++ b/pkg/systemstatsmonitor/host_collector.go @@ -74,6 +74,6 @@ func (hc *hostCollector) collect() { } if hc.uptime != nil { - hc.uptime.Record(hc.tags, int64(uptime)) + _ = hc.uptime.Record(hc.tags, int64(uptime)) } } diff --git a/pkg/systemstatsmonitor/memory_collector_unix.go b/pkg/systemstatsmonitor/memory_collector_unix.go index 23f8b09ac..75baa57c6 100644 --- a/pkg/systemstatsmonitor/memory_collector_unix.go +++ b/pkg/systemstatsmonitor/memory_collector_unix.go @@ -41,57 +41,57 @@ func (mc *memoryCollector) collect() { if mc.mBytesUsed != nil { if meminfo.MemFree != nil { - mc.mBytesUsed.Record(map[string]string{stateLabel: "free"}, int64(*meminfo.MemFree)*1024) + _ = mc.mBytesUsed.Record(map[string]string{stateLabel: "free"}, int64(*meminfo.MemFree)*1024) } if meminfo.Buffers != nil { - mc.mBytesUsed.Record(map[string]string{stateLabel: "buffered"}, int64(*meminfo.Buffers)*1024) + _ = mc.mBytesUsed.Record(map[string]string{stateLabel: "buffered"}, int64(*meminfo.Buffers)*1024) } if meminfo.Cached != nil { - mc.mBytesUsed.Record(map[string]string{stateLabel: "cached"}, int64(*meminfo.Cached)*1024) + _ = mc.mBytesUsed.Record(map[string]string{stateLabel: "cached"}, int64(*meminfo.Cached)*1024) } if meminfo.Slab != nil { - mc.mBytesUsed.Record(map[string]string{stateLabel: "slab"}, int64(*meminfo.Slab)*1024) + _ = mc.mBytesUsed.Record(map[string]string{stateLabel: "slab"}, int64(*meminfo.Slab)*1024) } if meminfo.MemTotal != nil && meminfo.MemFree != nil && meminfo.Buffers != nil && meminfo.Cached != nil && meminfo.Slab != nil { memUsed := *meminfo.MemTotal - *meminfo.MemFree - *meminfo.Buffers - *meminfo.Cached - *meminfo.Slab - mc.mBytesUsed.Record(map[string]string{stateLabel: "used"}, int64(memUsed)*1024) + _ = mc.mBytesUsed.Record(map[string]string{stateLabel: "used"}, int64(memUsed)*1024) } } if mc.mPercentUsed != nil && meminfo.MemTotal != nil && *meminfo.MemTotal > 0 && meminfo.MemFree != nil && meminfo.Buffers != nil && meminfo.Cached != nil && meminfo.Slab != nil { ratio := float64(*meminfo.MemTotal-*meminfo.MemFree-*meminfo.Buffers-*meminfo.Cached-*meminfo.Slab) / float64(*meminfo.MemTotal) - mc.mPercentUsed.Record(map[string]string{stateLabel: "used"}, float64(ratio*100.0)) + _ = mc.mPercentUsed.Record(map[string]string{stateLabel: "used"}, float64(ratio*100.0)) } if mc.mDirtyUsed != nil { if meminfo.Dirty != nil { - mc.mDirtyUsed.Record(map[string]string{stateLabel: "dirty"}, int64(*meminfo.Dirty)*1024) + _ = mc.mDirtyUsed.Record(map[string]string{stateLabel: "dirty"}, int64(*meminfo.Dirty)*1024) } if meminfo.Writeback != nil { - mc.mDirtyUsed.Record(map[string]string{stateLabel: "writeback"}, int64(*meminfo.Writeback)*1024) + _ = mc.mDirtyUsed.Record(map[string]string{stateLabel: "writeback"}, int64(*meminfo.Writeback)*1024) } } if mc.mAnonymousUsed != nil { if meminfo.ActiveAnon != nil { - mc.mAnonymousUsed.Record(map[string]string{stateLabel: "active"}, int64(*meminfo.ActiveAnon)*1024) + _ = mc.mAnonymousUsed.Record(map[string]string{stateLabel: "active"}, int64(*meminfo.ActiveAnon)*1024) } if meminfo.InactiveAnon != nil { - mc.mAnonymousUsed.Record(map[string]string{stateLabel: "inactive"}, int64(*meminfo.InactiveAnon)*1024) + _ = mc.mAnonymousUsed.Record(map[string]string{stateLabel: "inactive"}, int64(*meminfo.InactiveAnon)*1024) } } if mc.mPageCacheUsed != nil { if meminfo.ActiveFile != nil { - mc.mPageCacheUsed.Record(map[string]string{stateLabel: "active"}, int64(*meminfo.ActiveFile)*1024) + _ = mc.mPageCacheUsed.Record(map[string]string{stateLabel: "active"}, int64(*meminfo.ActiveFile)*1024) } if meminfo.InactiveFile != nil { - mc.mPageCacheUsed.Record(map[string]string{stateLabel: "inactive"}, int64(*meminfo.InactiveFile)*1024) + _ = mc.mPageCacheUsed.Record(map[string]string{stateLabel: "inactive"}, int64(*meminfo.InactiveFile)*1024) } } if mc.mUnevictableUsed != nil && meminfo.Unevictable != nil { - mc.mUnevictableUsed.Record(map[string]string{}, int64(*meminfo.Unevictable)*1024) + _ = mc.mUnevictableUsed.Record(map[string]string{}, int64(*meminfo.Unevictable)*1024) } } diff --git a/pkg/systemstatsmonitor/net_collector.go b/pkg/systemstatsmonitor/net_collector.go index fb0fdac6b..ac564655f 100644 --- a/pkg/systemstatsmonitor/net_collector.go +++ b/pkg/systemstatsmonitor/net_collector.go @@ -219,6 +219,10 @@ func (nc *netCollector) mustRegisterMetric(metricID metrics.MetricID, descriptio func (nc *netCollector) recordNetDev() { fs, err := procfs.NewFS(nc.procPath) + if err != nil { + klog.Errorf("Failed to open procfs: %v", err) + return + } stats, err := fs.NetDev() if err != nil { klog.Errorf("Failed to retrieve net dev stat: %v", err) @@ -281,7 +285,7 @@ func (r ifaceStatRecorder) RecordWithSameTags(stat procfs.NetDevLine, tags map[s // Range all registered collector and record its measurement with same tags for metricID, collector := range r.collectors { measurement := collector.exporter(stat) - collector.metric.Record(tags, measurement) + _ = collector.metric.Record(tags, measurement) klog.V(6).Infof("Metric %q record measurement %d with tags %v", metricID, measurement, tags) } } diff --git a/pkg/systemstatsmonitor/osfeature_collector.go b/pkg/systemstatsmonitor/osfeature_collector.go index 0de0c05e0..0c4c76240 100644 --- a/pkg/systemstatsmonitor/osfeature_collector.go +++ b/pkg/systemstatsmonitor/osfeature_collector.go @@ -85,12 +85,12 @@ func (ofc *osFeatureCollector) recordFeaturesFromCmdline(cmdlineArgs []system.Cm } } // Record the feature values. - ofc.osFeature.Record(map[string]string{featureLabel: "KTD"}, featuresMap["KTD"]) - ofc.osFeature.Record(map[string]string{featureLabel: "UnifiedCgroupHierarchy"}, featuresMap["UnifiedCgroupHierarchy"]) + _ = ofc.osFeature.Record(map[string]string{featureLabel: "KTD"}, featuresMap["KTD"]) + _ = ofc.osFeature.Record(map[string]string{featureLabel: "UnifiedCgroupHierarchy"}, featuresMap["UnifiedCgroupHierarchy"]) if featuresMap["ModuleSigned"] == 1 && featuresMap["LoadPinEnabled"] == 1 { - ofc.osFeature.Record(map[string]string{featureLabel: "KernelModuleIntegrity"}, 1) + _ = ofc.osFeature.Record(map[string]string{featureLabel: "KernelModuleIntegrity"}, 1) } else { - ofc.osFeature.Record(map[string]string{featureLabel: "KernelModuleIntegrity"}, 0) + _ = ofc.osFeature.Record(map[string]string{featureLabel: "KernelModuleIntegrity"}, 0) } } @@ -136,13 +136,13 @@ func (ofc *osFeatureCollector) recordFeaturesFromModules(modules []system.Module } // record the UnknownModules and GPUSupport if len(unknownModules) > 0 { - ofc.osFeature.Record(map[string]string{featureLabel: "UnknownModules", + _ = ofc.osFeature.Record(map[string]string{featureLabel: "UnknownModules", valueLabel: strings.Join(unknownModules, ",")}, 1) } else { - ofc.osFeature.Record(map[string]string{featureLabel: "UnknownModules"}, + _ = ofc.osFeature.Record(map[string]string{featureLabel: "UnknownModules"}, 0) } - ofc.osFeature.Record(map[string]string{featureLabel: "GPUSupport"}, + _ = ofc.osFeature.Record(map[string]string{featureLabel: "GPUSupport"}, int64(hasGPUSupport)) } diff --git a/pkg/util/http.go b/pkg/util/http.go index 1300be7e0..ec884e66c 100644 --- a/pkg/util/http.go +++ b/pkg/util/http.go @@ -30,11 +30,11 @@ func ReturnHTTPJson(w http.ResponseWriter, object interface{}) { } w.Header().Set("Content-type", "application/json") w.WriteHeader(http.StatusOK) - w.Write(data) + _, _ = w.Write(data) } // ReturnHTTPError generates error http response. func ReturnHTTPError(w http.ResponseWriter, err error) { w.WriteHeader(http.StatusInternalServerError) - w.Write([]byte(err.Error())) + _, _ = w.Write([]byte(err.Error())) } diff --git a/pkg/util/metrics/fakes_test.go b/pkg/util/metrics/fakes_test.go index bb7419681..46ffee247 100644 --- a/pkg/util/metrics/fakes_test.go +++ b/pkg/util/metrics/fakes_test.go @@ -232,7 +232,10 @@ func TestFakeInt64Metric(t *testing.T) { metric := NewFakeInt64Metric(test.metricName, test.aggregation, test.tagNames) for _, record := range test.records { - metric.Record(record.tags, record.measurement) + err := metric.Record(record.tags, record.measurement) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } } gotMetrics := metric.ListMetrics() diff --git a/pkg/util/metrics/metric_float64.go b/pkg/util/metrics/metric_float64.go index d6a903bb3..cfd04eeca 100644 --- a/pkg/util/metrics/metric_float64.go +++ b/pkg/util/metrics/metric_float64.go @@ -72,7 +72,10 @@ func NewFloat64Metric(metricID MetricID, viewName string, description string, un Aggregation: aggregationMethod, TagKeys: tagKeys, } - view.Register(newView) + err = view.Register(newView) + if err != nil { + return nil, fmt.Errorf("failed to register view %q: %v", viewName, err) + } metric := Float64Metric{viewName, measure} return &metric, nil diff --git a/pkg/util/metrics/metric_int64.go b/pkg/util/metrics/metric_int64.go index 794167616..834e862dc 100644 --- a/pkg/util/metrics/metric_int64.go +++ b/pkg/util/metrics/metric_int64.go @@ -72,7 +72,10 @@ func NewInt64Metric(metricID MetricID, viewName string, description string, unit Aggregation: aggregationMethod, TagKeys: tagKeys, } - view.Register(newView) + err = view.Register(newView) + if err != nil { + return nil, fmt.Errorf("failed to register view %q: %v", viewName, err) + } metric := Int64Metric{viewName, measure} return &metric, nil diff --git a/pkg/util/nethealth/nethealth.go b/pkg/util/nethealth/nethealth.go index 78a8a5895..431fcceee 100644 --- a/pkg/util/nethealth/nethealth.go +++ b/pkg/util/nethealth/nethealth.go @@ -54,7 +54,7 @@ func init() { } func monitorTimeElapsed(downloadStartTime time.Time, timeoutSeconds time.Duration) { - for true { + for { time.Sleep(1 * time.Second) // Check the status of the Get request if possible and check if timeout elapsed if time.Since(downloadStartTime) > timeoutSeconds*time.Second { @@ -74,7 +74,7 @@ func main() { log.Fatalf("Length reported (%d) is not equal to expected length (%d)", res.ContentLength, objectLength) } log.Printf("HTTP HEAD reports content length: %d - running GET\n", res.ContentLength) - res, err = http.Head(objectHashUrl) + _, err = http.Head(objectHashUrl) if err != nil { log.Fatalf("Failed to find hash URL %s (%s)", objectHashUrl, err) }