mirror of
https://github.com/kubernetes/node-problem-detector.git
synced 2026-02-14 09:59:56 +00:00
enabled and fixed the errcheck linter rule
This commit is contained in:
@@ -9,7 +9,7 @@ linters:
|
||||
- ineffassign
|
||||
- unused
|
||||
- govet
|
||||
#- errcheck
|
||||
- errcheck
|
||||
- staticcheck
|
||||
exclusions:
|
||||
generated: lax
|
||||
|
||||
@@ -39,11 +39,18 @@ func main() {
|
||||
}
|
||||
})
|
||||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
|
||||
pflag.CommandLine.MarkHidden("vmodule")
|
||||
pflag.CommandLine.MarkHidden("logtostderr")
|
||||
if err := pflag.CommandLine.MarkHidden("vmodule"); err != nil {
|
||||
klog.Fatalf("Failed to mark hidden flag 'vmodule': %v", err)
|
||||
}
|
||||
if err := pflag.CommandLine.MarkHidden("logtostderr"); err != nil {
|
||||
klog.Fatalf("Failed to mark hidden flag 'logtostderr': %v", err)
|
||||
}
|
||||
|
||||
hco := options.NewHealthCheckerOptions()
|
||||
hco.AddFlags(pflag.CommandLine)
|
||||
if err := hco.AddFlags(pflag.CommandLine); err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(int(types.Unknown))
|
||||
}
|
||||
pflag.Parse()
|
||||
hco.SetDefaults()
|
||||
if err := hco.IsValid(); err != nil {
|
||||
|
||||
@@ -47,12 +47,14 @@ type HealthCheckerOptions struct {
|
||||
}
|
||||
|
||||
// AddFlags adds health checker command line options to pflag.
|
||||
func (hco *HealthCheckerOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
func (hco *HealthCheckerOptions) AddFlags(fs *pflag.FlagSet) error {
|
||||
fs.StringVar(&hco.Component, "component", types.KubeletComponent,
|
||||
"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")
|
||||
if err := fs.MarkDeprecated("systemd-service", "please use --service flag instead"); err != nil {
|
||||
return fmt.Errorf("failed to mark deprecated flag 'systemd-service': %w", err)
|
||||
}
|
||||
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.")
|
||||
}
|
||||
@@ -73,6 +75,7 @@ func (hco *HealthCheckerOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
"The time to wait before marking the component as unhealthy.")
|
||||
fs.Var(&hco.LogPatterns, "log-pattern",
|
||||
"The log pattern to look for in service journald logs. The format for flag value <failureThresholdCount>:<logPattern>")
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsValid validates health checker command line options.
|
||||
|
||||
@@ -96,7 +96,9 @@ func writeTempFile(t *testing.T, ext string, contents string) (string, error) {
|
||||
fileName := f.Name()
|
||||
|
||||
if err := os.WriteFile(fileName, []byte(contents), 0o644); err != nil {
|
||||
os.Remove(fileName)
|
||||
if err := os.Remove(fileName); err != nil {
|
||||
t.Logf("Failed to remove temporary file %s: %v", fileName, err)
|
||||
}
|
||||
return "", fmt.Errorf("cannot write config to temp file %s, %v", fileName, err)
|
||||
}
|
||||
|
||||
@@ -106,7 +108,9 @@ func writeTempFile(t *testing.T, ext string, contents string) (string, error) {
|
||||
func setupNPD(t *testing.T) (*options.NodeProblemDetectorOptions, func()) {
|
||||
fakeLogFileName, err := writeTempFile(t, "log", "")
|
||||
if err != nil {
|
||||
os.Remove(fakeLogFileName)
|
||||
if err := os.Remove(fakeLogFileName); err != nil {
|
||||
t.Logf("Failed to remove temporary file %s: %v", fakeLogFileName, err)
|
||||
}
|
||||
t.Fatalf("cannot create temp config file, %v", err)
|
||||
}
|
||||
|
||||
@@ -114,8 +118,12 @@ func setupNPD(t *testing.T) (*options.NodeProblemDetectorOptions, func()) {
|
||||
|
||||
fakeConfigFileName, err := writeTempFile(t, "json", fakeConfigFileContents)
|
||||
if err != nil {
|
||||
os.Remove(fakeLogFileName)
|
||||
os.Remove(fakeConfigFileName)
|
||||
if err := os.Remove(fakeLogFileName); err != nil {
|
||||
t.Logf("Failed to remove temporary file %s: %v", fakeLogFileName, err)
|
||||
}
|
||||
if err := os.Remove(fakeConfigFileName); err != nil {
|
||||
t.Logf("Failed to remove temporary file %s: %v", fakeConfigFileName, err)
|
||||
}
|
||||
t.Fatalf("cannot create temp config file, %v", err)
|
||||
}
|
||||
|
||||
@@ -126,7 +134,11 @@ func setupNPD(t *testing.T) (*options.NodeProblemDetectorOptions, func()) {
|
||||
},
|
||||
},
|
||||
}, func() {
|
||||
os.Remove(fakeLogFileName)
|
||||
os.Remove(fakeConfigFileName)
|
||||
if err := os.Remove(fakeLogFileName); err != nil {
|
||||
t.Logf("Failed to remove temporary file %s: %v", fakeLogFileName, err)
|
||||
}
|
||||
if err := os.Remove(fakeConfigFileName); err != nil {
|
||||
t.Logf("Failed to remove temporary file %s: %v", fakeConfigFileName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,11 +38,17 @@ func main() {
|
||||
}
|
||||
})
|
||||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
|
||||
pflag.CommandLine.MarkHidden("vmodule")
|
||||
pflag.CommandLine.MarkHidden("logtostderr")
|
||||
if err := pflag.CommandLine.MarkHidden("vmodule"); err != nil {
|
||||
klog.Fatalf("Failed to mark hidden flag 'vmodule': %v", err)
|
||||
}
|
||||
if err := pflag.CommandLine.MarkHidden("logtostderr"); err != nil {
|
||||
klog.Fatalf("Failed to mark hidden flag 'logtostderr': %v", err)
|
||||
}
|
||||
|
||||
npdo := options.NewNodeProblemDetectorOptions()
|
||||
npdo.AddFlags(pflag.CommandLine)
|
||||
if err := npdo.AddFlags(pflag.CommandLine); err != nil {
|
||||
klog.Fatalf("Failed to add flags: %v", err)
|
||||
}
|
||||
|
||||
pflag.Parse()
|
||||
if err := npdMain(context.Background(), npdo); err != nil {
|
||||
|
||||
@@ -53,11 +53,17 @@ func main() {
|
||||
}
|
||||
})
|
||||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
|
||||
pflag.CommandLine.MarkHidden("vmodule")
|
||||
pflag.CommandLine.MarkHidden("logtostderr")
|
||||
if err := pflag.CommandLine.MarkHidden("vmodule"); err != nil {
|
||||
klog.Fatalf("Failed to mark hidden flag 'vmodule': %v", err)
|
||||
}
|
||||
if err := pflag.CommandLine.MarkHidden("logtostderr"); err != nil {
|
||||
klog.Fatalf("Failed to mark hidden flag 'logtostderr': %v", err)
|
||||
}
|
||||
|
||||
npdo := options.NewNodeProblemDetectorOptions()
|
||||
npdo.AddFlags(pflag.CommandLine)
|
||||
if err := npdo.AddFlags(pflag.CommandLine); err != nil {
|
||||
klog.Fatalf("Failed to add flags: %v", err)
|
||||
}
|
||||
|
||||
pflag.Parse()
|
||||
|
||||
|
||||
@@ -106,13 +106,17 @@ func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions {
|
||||
}
|
||||
|
||||
// AddFlags adds node problem detector command line options to pflag.
|
||||
func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) error {
|
||||
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.")
|
||||
if err := 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."); err != nil {
|
||||
return fmt.Errorf("failed to mark 'system-log-monitors' as deprecated: %w", err)
|
||||
}
|
||||
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.")
|
||||
if err := 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."); err != nil {
|
||||
return fmt.Errorf("failed to mark 'custom-plugin-monitors' as deprecated: %w", err)
|
||||
}
|
||||
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",
|
||||
@@ -149,6 +153,7 @@ func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
|
||||
problemDaemonName,
|
||||
problemdaemon.GetProblemDaemonHandlerOrDie(problemDaemonName).CmdOptionDescription))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidOrDie validates node problem detector command line options.
|
||||
|
||||
@@ -103,7 +103,9 @@ func TestNewPluginRun(t *testing.T) {
|
||||
utMeta := v
|
||||
t.Run(desp, func(t *testing.T) {
|
||||
conf := cpmtypes.CustomPluginConfig{}
|
||||
(&conf).ApplyConfiguration()
|
||||
if err := (&conf).ApplyConfiguration(); err != nil {
|
||||
t.Errorf("Failed to apply 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
|
||||
|
||||
@@ -219,7 +219,9 @@ func TestCustomPluginConfigApplyConfiguration(t *testing.T) {
|
||||
}
|
||||
|
||||
for desp, utMeta := range utMetas {
|
||||
(&utMeta.Orig).ApplyConfiguration()
|
||||
if err := (&utMeta.Orig).ApplyConfiguration(); err != nil {
|
||||
t.Errorf("Error in apply configuration for %q: %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)
|
||||
|
||||
@@ -94,7 +94,9 @@ 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"))
|
||||
if _, err := w.Write([]byte("ok")); err != nil {
|
||||
klog.Errorf("Failed to write response: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
// Add the handler to serve condition http request.
|
||||
|
||||
@@ -65,13 +65,19 @@ func getRepairFunc(hco *options.HealthCheckerOptions) func() {
|
||||
// Use "docker ps" for docker health check. Not using crictl for docker to remove
|
||||
// dependency on the kubelet.
|
||||
return func() {
|
||||
execCommand(types.CmdTimeout, "pkill", "-SIGUSR1", "dockerd")
|
||||
execCommand(types.CmdTimeout, "systemctl", "kill", "--kill-who=main", hco.Service)
|
||||
if _, err := execCommand(types.CmdTimeout, "pkill", "-SIGUSR1", "dockerd"); err != nil {
|
||||
klog.Errorf("Failed to send SIGUSR1 to dockerd: %v", err)
|
||||
}
|
||||
if _, err := execCommand(types.CmdTimeout, "systemctl", "kill", "--kill-who=main", hco.Service); err != nil {
|
||||
klog.Errorf("Failed to kill service %s: %v", hco.Service, err)
|
||||
}
|
||||
}
|
||||
default:
|
||||
// Just kill the service for all other components
|
||||
return func() {
|
||||
execCommand(types.CmdTimeout, "systemctl", "kill", "--kill-who=main", hco.Service)
|
||||
if _, err := execCommand(types.CmdTimeout, "systemctl", "kill", "--kill-who=main", hco.Service); err != nil {
|
||||
klog.Errorf("Failed to kill service %s: %v", hco.Service, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,7 +118,9 @@ func TestNewProblem(t *testing.T) {
|
||||
pmm, fakeProblemCounter, fakeProblemGauge := NewProblemMetricsManagerStub()
|
||||
|
||||
for idx, reason := range test.reasons {
|
||||
pmm.IncrementProblemCounter(reason, test.counts[idx])
|
||||
if err := pmm.IncrementProblemCounter(reason, test.counts[idx]); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
gotMetrics := append(fakeProblemCounter.ListMetrics(), fakeProblemGauge.ListMetrics()...)
|
||||
@@ -266,7 +268,9 @@ func TestSetProblemGauge(t *testing.T) {
|
||||
pmm, fakeProblemCounter, fakeProblemGauge := NewProblemMetricsManagerStub()
|
||||
|
||||
for _, argument := range test.arguments {
|
||||
pmm.SetProblemGauge(argument.problemType, argument.reason, argument.value)
|
||||
if err := pmm.SetProblemGauge(argument.problemType, argument.reason, argument.value); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
gotMetrics := append(fakeProblemCounter.ListMetrics(), fakeProblemGauge.ListMetrics()...)
|
||||
|
||||
@@ -91,7 +91,9 @@ const watchPollInterval = 500 * time.Millisecond
|
||||
// watchLoop is the main watch loop of filelog watcher.
|
||||
func (s *filelogWatcher) watchLoop() {
|
||||
defer func() {
|
||||
s.closer.Close()
|
||||
if err := s.closer.Close(); err != nil {
|
||||
klog.Errorf("Failed to close log file: %v", err)
|
||||
}
|
||||
close(s.logCh)
|
||||
s.tomb.Done()
|
||||
}()
|
||||
|
||||
@@ -141,8 +141,12 @@ Jan 2 03:04:05 kernel: [2.000000] 3
|
||||
f, err := os.CreateTemp("", "log_watcher_test")
|
||||
assert.NoError(t, err)
|
||||
defer func() {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
if err := f.Close(); err != nil {
|
||||
t.Logf("failed to close temporary file %s: %v", f.Name(), err)
|
||||
}
|
||||
if err := os.Remove(f.Name()); err != nil {
|
||||
t.Logf("failed to remove temporary file %s: %v", f.Name(), err)
|
||||
}
|
||||
}()
|
||||
_, err = f.Write([]byte(test.log))
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -77,7 +77,9 @@ func (k *kernelLogWatcher) Watch() (<-chan *logtypes.Log, error) {
|
||||
|
||||
// Stop closes the kmsgparser
|
||||
func (k *kernelLogWatcher) Stop() {
|
||||
k.kmsgParser.Close()
|
||||
if err := k.kmsgParser.Close(); err != nil {
|
||||
klog.Errorf("Failed to close kmsg parser: %v", err)
|
||||
}
|
||||
k.tomb.Stop()
|
||||
}
|
||||
|
||||
|
||||
@@ -187,34 +187,54 @@ func (cc *cpuCollector) recordUsage() {
|
||||
}
|
||||
timersStat := timersStats[0]
|
||||
|
||||
cc.mUsageTime.Record(map[string]string{stateLabel: "user"}, clockTick*timersStat.User-cc.lastUsageTime["user"])
|
||||
if err := cc.mUsageTime.Record(map[string]string{stateLabel: "user"}, clockTick*timersStat.User-cc.lastUsageTime["user"]); err != nil {
|
||||
klog.Errorf("Failed to record cpu usage time for user: %v", err)
|
||||
}
|
||||
cc.lastUsageTime["user"] = clockTick * timersStat.User
|
||||
|
||||
cc.mUsageTime.Record(map[string]string{stateLabel: "system"}, clockTick*timersStat.System-cc.lastUsageTime["system"])
|
||||
if err := cc.mUsageTime.Record(map[string]string{stateLabel: "system"}, clockTick*timersStat.System-cc.lastUsageTime["system"]); err != nil {
|
||||
klog.Errorf("Failed to record cpu usage time for system: %v", err)
|
||||
}
|
||||
cc.lastUsageTime["system"] = clockTick * timersStat.System
|
||||
|
||||
cc.mUsageTime.Record(map[string]string{stateLabel: "idle"}, clockTick*timersStat.Idle-cc.lastUsageTime["idle"])
|
||||
if err := cc.mUsageTime.Record(map[string]string{stateLabel: "idle"}, clockTick*timersStat.Idle-cc.lastUsageTime["idle"]); err != nil {
|
||||
klog.Errorf("Failed to record cpu usage time for idle: %v", err)
|
||||
}
|
||||
cc.lastUsageTime["idle"] = clockTick * timersStat.Idle
|
||||
|
||||
cc.mUsageTime.Record(map[string]string{stateLabel: "nice"}, clockTick*timersStat.Nice-cc.lastUsageTime["nice"])
|
||||
if err := cc.mUsageTime.Record(map[string]string{stateLabel: "nice"}, clockTick*timersStat.Nice-cc.lastUsageTime["nice"]); err != nil {
|
||||
klog.Errorf("Failed to record cpu usage time for nice: %v", err)
|
||||
}
|
||||
cc.lastUsageTime["nice"] = clockTick * timersStat.Nice
|
||||
|
||||
cc.mUsageTime.Record(map[string]string{stateLabel: "iowait"}, clockTick*timersStat.Iowait-cc.lastUsageTime["iowait"])
|
||||
if err := cc.mUsageTime.Record(map[string]string{stateLabel: "iowait"}, clockTick*timersStat.Iowait-cc.lastUsageTime["iowait"]); err != nil {
|
||||
klog.Errorf("Failed to record cpu usage time for iowait: %v", err)
|
||||
}
|
||||
cc.lastUsageTime["iowait"] = clockTick * timersStat.Iowait
|
||||
|
||||
cc.mUsageTime.Record(map[string]string{stateLabel: "irq"}, clockTick*timersStat.Irq-cc.lastUsageTime["irq"])
|
||||
if err := cc.mUsageTime.Record(map[string]string{stateLabel: "irq"}, clockTick*timersStat.Irq-cc.lastUsageTime["irq"]); err != nil {
|
||||
klog.Errorf("Failed to record cpu usage time for irq: %v", err)
|
||||
}
|
||||
cc.lastUsageTime["irq"] = clockTick * timersStat.Irq
|
||||
|
||||
cc.mUsageTime.Record(map[string]string{stateLabel: "softirq"}, clockTick*timersStat.Softirq-cc.lastUsageTime["softirq"])
|
||||
if err := cc.mUsageTime.Record(map[string]string{stateLabel: "softirq"}, clockTick*timersStat.Softirq-cc.lastUsageTime["softirq"]); err != nil {
|
||||
klog.Errorf("Failed to record cpu usage time for softirq: %v", err)
|
||||
}
|
||||
cc.lastUsageTime["softirq"] = clockTick * timersStat.Softirq
|
||||
|
||||
cc.mUsageTime.Record(map[string]string{stateLabel: "steal"}, clockTick*timersStat.Steal-cc.lastUsageTime["steal"])
|
||||
if err := cc.mUsageTime.Record(map[string]string{stateLabel: "steal"}, clockTick*timersStat.Steal-cc.lastUsageTime["steal"]); err != nil {
|
||||
klog.Errorf("Failed to record cpu usage time for steal: %v", err)
|
||||
}
|
||||
cc.lastUsageTime["steal"] = clockTick * timersStat.Steal
|
||||
|
||||
cc.mUsageTime.Record(map[string]string{stateLabel: "guest"}, clockTick*timersStat.Guest-cc.lastUsageTime["guest"])
|
||||
if err := cc.mUsageTime.Record(map[string]string{stateLabel: "guest"}, clockTick*timersStat.Guest-cc.lastUsageTime["guest"]); err != nil {
|
||||
klog.Errorf("Failed to record cpu usage time for guest: %v", err)
|
||||
}
|
||||
cc.lastUsageTime["guest"] = clockTick * timersStat.Guest
|
||||
|
||||
cc.mUsageTime.Record(map[string]string{stateLabel: "guest_nice"}, clockTick*timersStat.GuestNice-cc.lastUsageTime["guest_nice"])
|
||||
if err := cc.mUsageTime.Record(map[string]string{stateLabel: "guest_nice"}, clockTick*timersStat.GuestNice-cc.lastUsageTime["guest_nice"]); err != nil {
|
||||
klog.Errorf("Failed to record cpu usage time for guest_nice: %v", err)
|
||||
}
|
||||
cc.lastUsageTime["guest_nice"] = clockTick * timersStat.GuestNice
|
||||
}
|
||||
|
||||
|
||||
@@ -40,16 +40,24 @@ func (cc *cpuCollector) recordLoad() {
|
||||
}
|
||||
|
||||
if cc.mRunnableTaskCount != nil {
|
||||
cc.mRunnableTaskCount.Record(map[string]string{}, loadAvg.Load1)
|
||||
if err := cc.mRunnableTaskCount.Record(map[string]string{}, loadAvg.Load1); err != nil {
|
||||
klog.Errorf("Failed to record runnable task count: %v", err)
|
||||
}
|
||||
}
|
||||
if cc.mCpuLoad1m != nil {
|
||||
cc.mCpuLoad1m.Record(map[string]string{}, loadAvg.Load1)
|
||||
if err := cc.mCpuLoad1m.Record(map[string]string{}, loadAvg.Load1); err != nil {
|
||||
klog.Errorf("Failed to record cpu load 1m: %v", err)
|
||||
}
|
||||
}
|
||||
if cc.mCpuLoad5m != nil {
|
||||
cc.mCpuLoad5m.Record(map[string]string{}, loadAvg.Load5)
|
||||
if err := cc.mCpuLoad5m.Record(map[string]string{}, loadAvg.Load5); err != nil {
|
||||
klog.Errorf("Failed to record cpu load 5m: %v", err)
|
||||
}
|
||||
}
|
||||
if cc.mCpuLoad15m != nil {
|
||||
cc.mCpuLoad15m.Record(map[string]string{}, loadAvg.Load15)
|
||||
if err := cc.mCpuLoad15m.Record(map[string]string{}, loadAvg.Load15); err != nil {
|
||||
klog.Errorf("Failed to record cpu load 15m: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,19 +81,27 @@ func (cc *cpuCollector) recordSystemStats() {
|
||||
}
|
||||
|
||||
if cc.mSystemProcessesTotal != nil {
|
||||
cc.mSystemProcessesTotal.Record(map[string]string{}, int64(stats.ProcessCreated))
|
||||
if err := cc.mSystemProcessesTotal.Record(map[string]string{}, int64(stats.ProcessCreated)); err != nil {
|
||||
klog.Errorf("Failed to record system processes total: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if cc.mSystemProcsRunning != nil {
|
||||
cc.mSystemProcsRunning.Record(map[string]string{}, int64(stats.ProcessesRunning))
|
||||
if err := cc.mSystemProcsRunning.Record(map[string]string{}, int64(stats.ProcessesRunning)); err != nil {
|
||||
klog.Errorf("Failed to record system procs running: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if cc.mSystemProcsBlocked != nil {
|
||||
cc.mSystemProcsBlocked.Record(map[string]string{}, int64(stats.ProcessesBlocked))
|
||||
if err := cc.mSystemProcsBlocked.Record(map[string]string{}, int64(stats.ProcessesBlocked)); err != nil {
|
||||
klog.Errorf("Failed to record system procs blocked: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if cc.mSystemInterruptsTotal != nil {
|
||||
cc.mSystemInterruptsTotal.Record(map[string]string{}, int64(stats.IRQTotal))
|
||||
if err := cc.mSystemInterruptsTotal.Record(map[string]string{}, int64(stats.IRQTotal)); err != nil {
|
||||
klog.Errorf("Failed to record system interrupts total: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if cc.mSystemCPUStat != nil {
|
||||
@@ -94,25 +110,45 @@ func (cc *cpuCollector) recordSystemStats() {
|
||||
tags[cpuLabel] = fmt.Sprintf("cpu%d", i)
|
||||
|
||||
tags[stageLabel] = "user"
|
||||
cc.mSystemCPUStat.Record(tags, c.User)
|
||||
if err := cc.mSystemCPUStat.Record(tags, c.User); err != nil {
|
||||
klog.Errorf("Failed to record system cpu stat for user: %v", err)
|
||||
}
|
||||
tags[stageLabel] = "nice"
|
||||
cc.mSystemCPUStat.Record(tags, c.Nice)
|
||||
if err := cc.mSystemCPUStat.Record(tags, c.Nice); err != nil {
|
||||
klog.Errorf("Failed to record system cpu stat for nice: %v", err)
|
||||
}
|
||||
tags[stageLabel] = "system"
|
||||
cc.mSystemCPUStat.Record(tags, c.System)
|
||||
if err := cc.mSystemCPUStat.Record(tags, c.System); err != nil {
|
||||
klog.Errorf("Failed to record system cpu stat for system: %v", err)
|
||||
}
|
||||
tags[stageLabel] = "idle"
|
||||
cc.mSystemCPUStat.Record(tags, c.Idle)
|
||||
if err := cc.mSystemCPUStat.Record(tags, c.Idle); err != nil {
|
||||
klog.Errorf("Failed to record system cpu stat for idle: %v", err)
|
||||
}
|
||||
tags[stageLabel] = "iowait"
|
||||
cc.mSystemCPUStat.Record(tags, c.Iowait)
|
||||
if err := cc.mSystemCPUStat.Record(tags, c.Iowait); err != nil {
|
||||
klog.Errorf("Failed to record system cpu stat for iowait: %v", err)
|
||||
}
|
||||
tags[stageLabel] = "iRQ"
|
||||
cc.mSystemCPUStat.Record(tags, c.IRQ)
|
||||
if err := cc.mSystemCPUStat.Record(tags, c.IRQ); err != nil {
|
||||
klog.Errorf("Failed to record system cpu stat for iRQ: %v", err)
|
||||
}
|
||||
tags[stageLabel] = "softIRQ"
|
||||
cc.mSystemCPUStat.Record(tags, c.SoftIRQ)
|
||||
if err := cc.mSystemCPUStat.Record(tags, c.SoftIRQ); err != nil {
|
||||
klog.Errorf("Failed to record system cpu stat for softIRQ: %v", err)
|
||||
}
|
||||
tags[stageLabel] = "steal"
|
||||
cc.mSystemCPUStat.Record(tags, c.Steal)
|
||||
if err := cc.mSystemCPUStat.Record(tags, c.Steal); err != nil {
|
||||
klog.Errorf("Failed to record system cpu stat for steal: %v", err)
|
||||
}
|
||||
tags[stageLabel] = "guest"
|
||||
cc.mSystemCPUStat.Record(tags, c.Guest)
|
||||
if err := cc.mSystemCPUStat.Record(tags, c.Guest); err != nil {
|
||||
klog.Errorf("Failed to record system cpu stat for guest: %v", err)
|
||||
}
|
||||
tags[stageLabel] = "guestNice"
|
||||
cc.mSystemCPUStat.Record(tags, c.GuestNice)
|
||||
if err := cc.mSystemCPUStat.Record(tags, c.GuestNice); err != nil {
|
||||
klog.Errorf("Failed to record system cpu stat for guestNice: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,10 +188,14 @@ 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))
|
||||
if err := dc.mIOTime.Record(tags, int64(ioCountersStat.IoTime-lastIOTime)); err != nil {
|
||||
klog.Errorf("Failed to record IO time for %s: %v", deviceName, err)
|
||||
}
|
||||
}
|
||||
if dc.mWeightedIO != nil {
|
||||
dc.mWeightedIO.Record(tags, int64(ioCountersStat.WeightedIO-lastWeightedIO))
|
||||
if err := dc.mWeightedIO.Record(tags, int64(ioCountersStat.WeightedIO-lastWeightedIO)); err != nil {
|
||||
klog.Errorf("Failed to record weighted IO for %s: %v", deviceName, err)
|
||||
}
|
||||
}
|
||||
if historyExist {
|
||||
avgQueueLen := float64(0.0)
|
||||
@@ -200,7 +204,9 @@ func (dc *diskCollector) recordIOCounters(ioCountersStats map[string]disk.IOCoun
|
||||
avgQueueLen = float64(ioCountersStat.WeightedIO-lastWeightedIO) / diffSampleTimeMs
|
||||
}
|
||||
if dc.mAvgQueueLen != nil {
|
||||
dc.mAvgQueueLen.Record(tags, avgQueueLen)
|
||||
if err := dc.mAvgQueueLen.Record(tags, avgQueueLen); err != nil {
|
||||
klog.Errorf("Failed to record average queue length for %s: %v", deviceName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,19 +214,27 @@ 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]))
|
||||
if err := dc.mOpsCount.Record(tags, int64(ioCountersStat.ReadCount-dc.lastReadCount[deviceName])); err != nil {
|
||||
klog.Errorf("Failed to record read ops count for %s: %v", deviceName, err)
|
||||
}
|
||||
dc.lastReadCount[deviceName] = ioCountersStat.ReadCount
|
||||
}
|
||||
if dc.mMergedOpsCount != nil {
|
||||
dc.mMergedOpsCount.Record(tags, int64(ioCountersStat.MergedReadCount-dc.lastMergedReadCount[deviceName]))
|
||||
if err := dc.mMergedOpsCount.Record(tags, int64(ioCountersStat.MergedReadCount-dc.lastMergedReadCount[deviceName])); err != nil {
|
||||
klog.Errorf("Failed to record merged read ops count for %s: %v", deviceName, err)
|
||||
}
|
||||
dc.lastMergedReadCount[deviceName] = ioCountersStat.MergedReadCount
|
||||
}
|
||||
if dc.mOpsBytes != nil {
|
||||
dc.mOpsBytes.Record(tags, int64(ioCountersStat.ReadBytes-dc.lastReadBytes[deviceName]))
|
||||
if err := dc.mOpsBytes.Record(tags, int64(ioCountersStat.ReadBytes-dc.lastReadBytes[deviceName])); err != nil {
|
||||
klog.Errorf("Failed to record read ops bytes for %s: %v", deviceName, err)
|
||||
}
|
||||
dc.lastReadBytes[deviceName] = ioCountersStat.ReadBytes
|
||||
}
|
||||
if dc.mOpsTime != nil {
|
||||
dc.mOpsTime.Record(tags, int64(ioCountersStat.ReadTime-dc.lastReadTime[deviceName]))
|
||||
if err := dc.mOpsTime.Record(tags, int64(ioCountersStat.ReadTime-dc.lastReadTime[deviceName])); err != nil {
|
||||
klog.Errorf("Failed to record read ops time for %s: %v", deviceName, err)
|
||||
}
|
||||
dc.lastReadTime[deviceName] = ioCountersStat.ReadTime
|
||||
}
|
||||
|
||||
@@ -228,19 +242,27 @@ 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]))
|
||||
if err := dc.mOpsCount.Record(tags, int64(ioCountersStat.WriteCount-dc.lastWriteCount[deviceName])); err != nil {
|
||||
klog.Errorf("Failed to record write ops count for %s: %v", deviceName, err)
|
||||
}
|
||||
dc.lastWriteCount[deviceName] = ioCountersStat.WriteCount
|
||||
}
|
||||
if dc.mMergedOpsCount != nil {
|
||||
dc.mMergedOpsCount.Record(tags, int64(ioCountersStat.MergedWriteCount-dc.lastMergedWriteCount[deviceName]))
|
||||
if err := dc.mMergedOpsCount.Record(tags, int64(ioCountersStat.MergedWriteCount-dc.lastMergedWriteCount[deviceName])); err != nil {
|
||||
klog.Errorf("Failed to record merged write ops count for %s: %v", deviceName, err)
|
||||
}
|
||||
dc.lastMergedWriteCount[deviceName] = ioCountersStat.MergedWriteCount
|
||||
}
|
||||
if dc.mOpsBytes != nil {
|
||||
dc.mOpsBytes.Record(tags, int64(ioCountersStat.WriteBytes-dc.lastWriteBytes[deviceName]))
|
||||
if err := dc.mOpsBytes.Record(tags, int64(ioCountersStat.WriteBytes-dc.lastWriteBytes[deviceName])); err != nil {
|
||||
klog.Errorf("Failed to record write ops bytes for %s: %v", deviceName, err)
|
||||
}
|
||||
dc.lastWriteBytes[deviceName] = ioCountersStat.WriteBytes
|
||||
}
|
||||
if dc.mOpsTime != nil {
|
||||
dc.mOpsTime.Record(tags, int64(ioCountersStat.WriteTime-dc.lastWriteTime[deviceName]))
|
||||
if err := dc.mOpsTime.Record(tags, int64(ioCountersStat.WriteTime-dc.lastWriteTime[deviceName])); err != nil {
|
||||
klog.Errorf("Failed to record write ops time for %s: %v", deviceName, err)
|
||||
}
|
||||
dc.lastWriteTime[deviceName] = ioCountersStat.WriteTime
|
||||
}
|
||||
}
|
||||
@@ -301,10 +323,16 @@ 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))
|
||||
if err := dc.mBytesUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "free"}, int64(usageStat.Free)); err != nil {
|
||||
klog.Errorf("Failed to record free bytes for %s: %v", deviceName, err)
|
||||
}
|
||||
if err := dc.mBytesUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "used"}, int64(usageStat.Used)); err != nil {
|
||||
klog.Errorf("Failed to record used bytes for %s: %v", deviceName, err)
|
||||
}
|
||||
if dc.mPercentUsed != nil {
|
||||
dc.mPercentUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "used"}, float64(usageStat.UsedPercent))
|
||||
if err := dc.mPercentUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "used"}, float64(usageStat.UsedPercent)); err != nil {
|
||||
klog.Errorf("Failed to record used percent for %s: %v", deviceName, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,6 +74,8 @@ func (hc *hostCollector) collect() {
|
||||
}
|
||||
|
||||
if hc.uptime != nil {
|
||||
hc.uptime.Record(hc.tags, int64(uptime))
|
||||
if err := hc.uptime.Record(hc.tags, int64(uptime)); err != nil {
|
||||
klog.Errorf("Failed to record uptime: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,57 +41,83 @@ func (mc *memoryCollector) collect() {
|
||||
|
||||
if mc.mBytesUsed != nil {
|
||||
if meminfo.MemFree != nil {
|
||||
mc.mBytesUsed.Record(map[string]string{stateLabel: "free"}, int64(*meminfo.MemFree)*1024)
|
||||
if err := mc.mBytesUsed.Record(map[string]string{stateLabel: "free"}, int64(*meminfo.MemFree)*1024); err != nil {
|
||||
klog.Errorf("Failed to record memory bytes used for free state: %v", err)
|
||||
}
|
||||
}
|
||||
if meminfo.Buffers != nil {
|
||||
mc.mBytesUsed.Record(map[string]string{stateLabel: "buffered"}, int64(*meminfo.Buffers)*1024)
|
||||
if err := mc.mBytesUsed.Record(map[string]string{stateLabel: "buffered"}, int64(*meminfo.Buffers)*1024); err != nil {
|
||||
klog.Errorf("Failed to record memory bytes used for buffered state: %v", err)
|
||||
}
|
||||
}
|
||||
if meminfo.Cached != nil {
|
||||
mc.mBytesUsed.Record(map[string]string{stateLabel: "cached"}, int64(*meminfo.Cached)*1024)
|
||||
if err := mc.mBytesUsed.Record(map[string]string{stateLabel: "cached"}, int64(*meminfo.Cached)*1024); err != nil {
|
||||
klog.Errorf("Failed to record memory bytes used for cached state: %v", err)
|
||||
}
|
||||
}
|
||||
if meminfo.Slab != nil {
|
||||
mc.mBytesUsed.Record(map[string]string{stateLabel: "slab"}, int64(*meminfo.Slab)*1024)
|
||||
if err := mc.mBytesUsed.Record(map[string]string{stateLabel: "slab"}, int64(*meminfo.Slab)*1024); err != nil {
|
||||
klog.Errorf("Failed to record memory bytes used for slab state: %v", err)
|
||||
}
|
||||
}
|
||||
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)
|
||||
if err := mc.mBytesUsed.Record(map[string]string{stateLabel: "used"}, int64(memUsed)*1024); err != nil {
|
||||
klog.Errorf("Failed to record memory bytes used for used state: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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))
|
||||
if err := mc.mPercentUsed.Record(map[string]string{stateLabel: "used"}, float64(ratio*100.0)); err != nil {
|
||||
klog.Errorf("Failed to record memory percent used: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if mc.mDirtyUsed != nil {
|
||||
if meminfo.Dirty != nil {
|
||||
mc.mDirtyUsed.Record(map[string]string{stateLabel: "dirty"}, int64(*meminfo.Dirty)*1024)
|
||||
if err := mc.mDirtyUsed.Record(map[string]string{stateLabel: "dirty"}, int64(*meminfo.Dirty)*1024); err != nil {
|
||||
klog.Errorf("Failed to record dirty used memory: %v", err)
|
||||
}
|
||||
}
|
||||
if meminfo.Writeback != nil {
|
||||
mc.mDirtyUsed.Record(map[string]string{stateLabel: "writeback"}, int64(*meminfo.Writeback)*1024)
|
||||
if err := mc.mDirtyUsed.Record(map[string]string{stateLabel: "writeback"}, int64(*meminfo.Writeback)*1024); err != nil {
|
||||
klog.Errorf("Failed to record writeback used memory: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if mc.mAnonymousUsed != nil {
|
||||
if meminfo.ActiveAnon != nil {
|
||||
mc.mAnonymousUsed.Record(map[string]string{stateLabel: "active"}, int64(*meminfo.ActiveAnon)*1024)
|
||||
if err := mc.mAnonymousUsed.Record(map[string]string{stateLabel: "active"}, int64(*meminfo.ActiveAnon)*1024); err != nil {
|
||||
klog.Errorf("Failed to record active anonymous used memory: %v", err)
|
||||
}
|
||||
}
|
||||
if meminfo.InactiveAnon != nil {
|
||||
mc.mAnonymousUsed.Record(map[string]string{stateLabel: "inactive"}, int64(*meminfo.InactiveAnon)*1024)
|
||||
if err := mc.mAnonymousUsed.Record(map[string]string{stateLabel: "inactive"}, int64(*meminfo.InactiveAnon)*1024); err != nil {
|
||||
klog.Errorf("Failed to record inactive anonymous used memory: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if mc.mPageCacheUsed != nil {
|
||||
if meminfo.ActiveFile != nil {
|
||||
mc.mPageCacheUsed.Record(map[string]string{stateLabel: "active"}, int64(*meminfo.ActiveFile)*1024)
|
||||
if err := mc.mPageCacheUsed.Record(map[string]string{stateLabel: "active"}, int64(*meminfo.ActiveFile)*1024); err != nil {
|
||||
klog.Errorf("Failed to record active page cache used memory: %v", err)
|
||||
}
|
||||
}
|
||||
if meminfo.InactiveFile != nil {
|
||||
mc.mPageCacheUsed.Record(map[string]string{stateLabel: "inactive"}, int64(*meminfo.InactiveFile)*1024)
|
||||
if err := mc.mPageCacheUsed.Record(map[string]string{stateLabel: "inactive"}, int64(*meminfo.InactiveFile)*1024); err != nil {
|
||||
klog.Errorf("Failed to record inactive page cache used memory: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if mc.mUnevictableUsed != nil && meminfo.Unevictable != nil {
|
||||
mc.mUnevictableUsed.Record(map[string]string{}, int64(*meminfo.Unevictable)*1024)
|
||||
if err := mc.mUnevictableUsed.Record(map[string]string{}, int64(*meminfo.Unevictable)*1024); err != nil {
|
||||
klog.Errorf("Failed to record unevictable used memory: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -287,7 +287,9 @@ 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)
|
||||
if err := collector.metric.Record(tags, measurement); err != nil {
|
||||
klog.Errorf("Failed to record metric %q: %v", metricID, err)
|
||||
}
|
||||
klog.V(6).Infof("Metric %q record measurement %d with tags %v", metricID, measurement, tags)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,12 +86,20 @@ 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"])
|
||||
if err := ofc.osFeature.Record(map[string]string{featureLabel: "KTD"}, featuresMap["KTD"]); err != nil {
|
||||
klog.Errorf("Failed to record KTD feature: %v", err)
|
||||
}
|
||||
if err := ofc.osFeature.Record(map[string]string{featureLabel: "UnifiedCgroupHierarchy"}, featuresMap["UnifiedCgroupHierarchy"]); err != nil {
|
||||
klog.Errorf("Failed to record UnifiedCgroupHierarchy feature: %v", err)
|
||||
}
|
||||
if featuresMap["ModuleSigned"] == 1 && featuresMap["LoadPinEnabled"] == 1 {
|
||||
ofc.osFeature.Record(map[string]string{featureLabel: "KernelModuleIntegrity"}, 1)
|
||||
if err := ofc.osFeature.Record(map[string]string{featureLabel: "KernelModuleIntegrity"}, 1); err != nil {
|
||||
klog.Errorf("Failed to record KernelModuleIntegrity feature: %v", err)
|
||||
}
|
||||
} else {
|
||||
ofc.osFeature.Record(map[string]string{featureLabel: "KernelModuleIntegrity"}, 0)
|
||||
if err := ofc.osFeature.Record(map[string]string{featureLabel: "KernelModuleIntegrity"}, 0); err != nil {
|
||||
klog.Errorf("Failed to record KernelModuleIntegrity feature: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,16 +145,22 @@ func (ofc *osFeatureCollector) recordFeaturesFromModules(modules []system.Module
|
||||
}
|
||||
// record the UnknownModules and GPUSupport
|
||||
if len(unknownModules) > 0 {
|
||||
ofc.osFeature.Record(map[string]string{
|
||||
if err := ofc.osFeature.Record(map[string]string{
|
||||
featureLabel: "UnknownModules",
|
||||
valueLabel: strings.Join(unknownModules, ","),
|
||||
}, 1)
|
||||
}, 1); err != nil {
|
||||
klog.Errorf("Failed to record UnknownModules feature: %v", err)
|
||||
}
|
||||
} else {
|
||||
ofc.osFeature.Record(map[string]string{featureLabel: "UnknownModules"},
|
||||
0)
|
||||
if err := ofc.osFeature.Record(map[string]string{featureLabel: "UnknownModules"},
|
||||
0); err != nil {
|
||||
klog.Errorf("Failed to record UnknownModules feature: %v", err)
|
||||
}
|
||||
}
|
||||
if err := ofc.osFeature.Record(map[string]string{featureLabel: "GPUSupport"},
|
||||
int64(hasGPUSupport)); err != nil {
|
||||
klog.Errorf("Failed to record GPUSupport feature: %v", err)
|
||||
}
|
||||
ofc.osFeature.Record(map[string]string{featureLabel: "GPUSupport"},
|
||||
int64(hasGPUSupport))
|
||||
}
|
||||
|
||||
func (ofc *osFeatureCollector) collect() {
|
||||
|
||||
@@ -19,6 +19,8 @@ package util
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
// ReturnHTTPJson generates json http response.
|
||||
@@ -30,11 +32,15 @@ func ReturnHTTPJson(w http.ResponseWriter, object interface{}) {
|
||||
}
|
||||
w.Header().Set("Content-type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(data)
|
||||
if _, err := w.Write(data); err != nil {
|
||||
klog.Errorf("Failed to write http response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// ReturnHTTPError generates error http response.
|
||||
func ReturnHTTPError(w http.ResponseWriter, err error) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
if _, err := w.Write([]byte(err.Error())); err != nil {
|
||||
klog.Errorf("Failed to write http error response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,7 +232,9 @@ func TestFakeInt64Metric(t *testing.T) {
|
||||
metric := NewFakeInt64Metric(test.metricName, test.aggregation, test.tagNames)
|
||||
|
||||
for _, record := range test.records {
|
||||
metric.Record(record.tags, record.measurement)
|
||||
if err := metric.Record(record.tags, record.measurement); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
gotMetrics := metric.ListMetrics()
|
||||
|
||||
@@ -72,7 +72,9 @@ func NewFloat64Metric(metricID MetricID, viewName string, description string, un
|
||||
Aggregation: aggregationMethod,
|
||||
TagKeys: tagKeys,
|
||||
}
|
||||
view.Register(newView)
|
||||
if err := view.Register(newView); err != nil {
|
||||
return nil, fmt.Errorf("failed to register view for metric %q: %v", viewName, err)
|
||||
}
|
||||
|
||||
metric := Float64Metric{viewName, measure}
|
||||
return &metric, nil
|
||||
|
||||
@@ -72,7 +72,9 @@ func NewInt64Metric(metricID MetricID, viewName string, description string, unit
|
||||
Aggregation: aggregationMethod,
|
||||
TagKeys: tagKeys,
|
||||
}
|
||||
view.Register(newView)
|
||||
if err := view.Register(newView); err != nil {
|
||||
return nil, fmt.Errorf("failed to register view for metric %q: %v", viewName, err)
|
||||
}
|
||||
|
||||
metric := Int64Metric{viewName, measure}
|
||||
return &metric, nil
|
||||
|
||||
@@ -16,6 +16,8 @@ package system
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
// ReadFileIntoLines reads contents from a file and returns lines.
|
||||
@@ -24,7 +26,11 @@ func ReadFileIntoLines(filename string) ([]string, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
defer func() {
|
||||
if err := file.Close(); err != nil {
|
||||
klog.Errorf("Failed to close file %s: %v", filename, err)
|
||||
}
|
||||
}()
|
||||
|
||||
var result []string
|
||||
s := bufio.NewScanner(file)
|
||||
|
||||
Reference in New Issue
Block a user