diff --git a/cmd/telegraf/telegraf.go b/cmd/telegraf/telegraf.go index 42fd297a1661b..5646332b9e25c 100644 --- a/cmd/telegraf/telegraf.go +++ b/cmd/telegraf/telegraf.go @@ -351,6 +351,7 @@ func (t *Telegraf) runAgent(ctx context.Context, c *config.Config, reloadConfig // SdNotify() only tries to notify if the NOTIFY_SOCKET environment is set, so it's safe to call when systemd isn't present. // Ignore the return values here because they're not valid for platforms that don't use systemd. // For platforms that use systemd, telegraf doesn't log if the notification failed. + //nolint:errcheck // see above _, _ = daemon.SdNotify(false, daemon.SdNotifyReady) if t.once { diff --git a/config/config.go b/config/config.go index 86376577ea119..0c9a14f70d5c6 100644 --- a/config/config.go +++ b/config/config.go @@ -946,8 +946,8 @@ func (c *Config) probeParser(parentcategory string, parentname string, table *as } // Try to parse the options to detect if any of them is misspelled - // We don't actually use the parser, so no need to check the error. parser := creator("") + //nolint:errcheck // We don't actually use the parser, so no need to check the error. _ = c.toml.UnmarshalTable(table, parser) return true diff --git a/config/config_test.go b/config/config_test.go index 5ed0b5b375ef6..0c458fbf8a152 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -519,7 +519,8 @@ func TestConfig_AzureMonitorNamespacePrefix(t *testing.T) { func TestGetDefaultConfigPathFromEnvURL(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) - _, _ = w.Write([]byte("[agent]\ndebug = true")) + _, err := w.Write([]byte("[agent]\ndebug = true")) + require.NoError(t, err) })) defer ts.Close() @@ -1193,7 +1194,8 @@ func TestPersisterInputStoreLoad(t *testing.T) { p.state.Version++ p.state.Offset += uint64(i + 1) p.state.Bits = append(p.state.Bits, len(p.state.Bits)) - p.state.Modified, _ = time.Parse(time.RFC3339, "2022-11-03T16:49:00+02:00") + p.state.Modified, err = time.Parse(time.RFC3339, "2022-11-03T16:49:00+02:00") + require.NoError(t, err) // Store the state for later comparison expected[plugin.ID()] = p.GetState() @@ -1542,7 +1544,10 @@ type MockupStatePlugin struct { } func (m *MockupStatePlugin) Init() error { - t0, _ := time.Parse(time.RFC3339, "2021-04-24T23:42:00+02:00") + t0, err := time.Parse(time.RFC3339, "2021-04-24T23:42:00+02:00") + if err != nil { + return err + } m.state = MockupState{ Name: "mockup", Bits: []int{}, diff --git a/filter/filter_test.go b/filter/filter_test.go index 4268ccbbcae61..05a78bd4aa46e 100644 --- a/filter/filter_test.go +++ b/filter/filter_test.go @@ -70,7 +70,8 @@ func TestIncludeExclude(t *testing.T) { var benchbool bool func BenchmarkFilterSingleNoGlobFalse(b *testing.B) { - f, _ := Compile([]string{"cpu"}) + f, err := Compile([]string{"cpu"}) + require.NoError(b, err) var tmp bool for n := 0; n < b.N; n++ { tmp = f.Match("network") @@ -79,7 +80,8 @@ func BenchmarkFilterSingleNoGlobFalse(b *testing.B) { } func BenchmarkFilterSingleNoGlobTrue(b *testing.B) { - f, _ := Compile([]string{"cpu"}) + f, err := Compile([]string{"cpu"}) + require.NoError(b, err) var tmp bool for n := 0; n < b.N; n++ { tmp = f.Match("cpu") @@ -88,7 +90,8 @@ func BenchmarkFilterSingleNoGlobTrue(b *testing.B) { } func BenchmarkFilter(b *testing.B) { - f, _ := Compile([]string{"cpu", "mem", "net*"}) + f, err := Compile([]string{"cpu", "mem", "net*"}) + require.NoError(b, err) var tmp bool for n := 0; n < b.N; n++ { tmp = f.Match("network") @@ -97,7 +100,8 @@ func BenchmarkFilter(b *testing.B) { } func BenchmarkFilterNoGlob(b *testing.B) { - f, _ := Compile([]string{"cpu", "mem", "net"}) + f, err := Compile([]string{"cpu", "mem", "net"}) + require.NoError(b, err) var tmp bool for n := 0; n < b.N; n++ { tmp = f.Match("net") @@ -106,8 +110,9 @@ func BenchmarkFilterNoGlob(b *testing.B) { } func BenchmarkFilter2(b *testing.B) { - f, _ := Compile([]string{"aa", "bb", "c", "ad", "ar", "at", "aq", + f, err := Compile([]string{"aa", "bb", "c", "ad", "ar", "at", "aq", "aw", "az", "axxx", "ab", "cpu", "mem", "net*"}) + require.NoError(b, err) var tmp bool for n := 0; n < b.N; n++ { tmp = f.Match("network") @@ -116,8 +121,9 @@ func BenchmarkFilter2(b *testing.B) { } func BenchmarkFilter2NoGlob(b *testing.B) { - f, _ := Compile([]string{"aa", "bb", "c", "ad", "ar", "at", "aq", + f, err := Compile([]string{"aa", "bb", "c", "ad", "ar", "at", "aq", "aw", "az", "axxx", "ab", "cpu", "mem", "net"}) + require.NoError(b, err) var tmp bool for n := 0; n < b.N; n++ { tmp = f.Match("net") diff --git a/internal/globpath/globpath_test.go b/internal/globpath/globpath_test.go index a245489b00675..0f5408095e844 100644 --- a/internal/globpath/globpath_test.go +++ b/internal/globpath/globpath_test.go @@ -69,7 +69,8 @@ func TestRootGlob(t *testing.T) { } for _, test := range tests { - actual, _ := Compile(test.input) + actual, err := Compile(test.input) + require.NoError(t, err) require.Equal(t, actual.rootGlob, test.output) } } diff --git a/internal/internal_test.go b/internal/internal_test.go index fd2b9d0f4ba91..5d0fde9d76146 100644 --- a/internal/internal_test.go +++ b/internal/internal_test.go @@ -44,21 +44,17 @@ func TestSnakeCase(t *testing.T) { } } -var ( - sleepbin, _ = exec.LookPath("sleep") - echobin, _ = exec.LookPath("echo") - shell, _ = exec.LookPath("sh") -) - func TestRunTimeout(t *testing.T) { t.Skip("Skipping test due to random failures & a data race when running test-all.") - if sleepbin == "" { + sleepbin, err := exec.LookPath("sleep") + if err != nil || sleepbin == "" { t.Skip("'sleep' binary not available on OS, skipping.") } + cmd := exec.Command(sleepbin, "10") start := time.Now() - err := RunTimeout(cmd, time.Millisecond*20) + err = RunTimeout(cmd, time.Millisecond*20) elapsed := time.Since(start) require.Equal(t, ErrTimeout, err) @@ -71,12 +67,13 @@ func TestRunTimeoutFastExit(t *testing.T) { if testing.Short() { t.Skip("Skipping test due to random failures.") } - if echobin == "" { + echobin, err := exec.LookPath("echo") + if err != nil || echobin == "" { t.Skip("'echo' binary not available on OS, skipping.") } cmd := exec.Command(echobin) start := time.Now() - err := RunTimeout(cmd, time.Millisecond*20) + err = RunTimeout(cmd, time.Millisecond*20) buf := &bytes.Buffer{} log.SetOutput(buf) elapsed := time.Since(start) @@ -93,12 +90,13 @@ func TestRunTimeoutFastExit(t *testing.T) { func TestCombinedOutputTimeout(t *testing.T) { // TODO: Fix this test t.Skip("Test failing too often, skip for now and revisit later.") - if sleepbin == "" { + sleepbin, err := exec.LookPath("sleep") + if err != nil || sleepbin == "" { t.Skip("'sleep' binary not available on OS, skipping.") } cmd := exec.Command(sleepbin, "10") start := time.Now() - _, err := CombinedOutputTimeout(cmd, time.Millisecond*20) + _, err = CombinedOutputTimeout(cmd, time.Millisecond*20) elapsed := time.Since(start) require.Equal(t, ErrTimeout, err) @@ -107,7 +105,8 @@ func TestCombinedOutputTimeout(t *testing.T) { } func TestCombinedOutput(t *testing.T) { - if echobin == "" { + echobin, err := exec.LookPath("echo") + if err != nil || echobin == "" { t.Skip("'echo' binary not available on OS, skipping.") } cmd := exec.Command(echobin, "foo") @@ -120,7 +119,8 @@ func TestCombinedOutput(t *testing.T) { // test that CombinedOutputTimeout and exec.Cmd.CombinedOutput return // the same output from a failed command. func TestCombinedOutputError(t *testing.T) { - if shell == "" { + shell, err := exec.LookPath("sh") + if err != nil || shell == "" { t.Skip("'sh' binary not available on OS, skipping.") } cmd := exec.Command(shell, "-c", "false") @@ -135,11 +135,12 @@ func TestCombinedOutputError(t *testing.T) { } func TestRunError(t *testing.T) { - if shell == "" { + shell, err := exec.LookPath("sh") + if err != nil || shell == "" { t.Skip("'sh' binary not available on OS, skipping.") } cmd := exec.Command(shell, "-c", "false") - err := RunTimeout(cmd, time.Second) + err = RunTimeout(cmd, time.Second) require.Error(t, err) } @@ -306,8 +307,9 @@ func TestAlignDuration(t *testing.T) { func TestAlignTime(t *testing.T) { rfc3339 := func(value string) time.Time { - t, _ := time.Parse(time.RFC3339, value) - return t + tt, err := time.Parse(time.RFC3339, value) + require.NoError(t, err) + return tt } tests := []struct { diff --git a/internal/rotate/file_writer_test.go b/internal/rotate/file_writer_test.go index 5b11f996b8706..4ebc74c12a490 100644 --- a/internal/rotate/file_writer_test.go +++ b/internal/rotate/file_writer_test.go @@ -19,13 +19,15 @@ func TestFileWriter_NoRotation(t *testing.T) { require.NoError(t, err) _, err = writer.Write([]byte("Hello World 2")) require.NoError(t, err) - files, _ := os.ReadDir(tempDir) + files, err := os.ReadDir(tempDir) + require.NoError(t, err) require.Len(t, files, 1) } func TestFileWriter_TimeRotation(t *testing.T) { tempDir := t.TempDir() - interval, _ := time.ParseDuration("10ms") + interval, err := time.ParseDuration("10ms") + require.NoError(t, err) writer, err := NewFileWriter(filepath.Join(tempDir, "test"), interval, 0, -1) require.NoError(t, err) t.Cleanup(func() { require.NoError(t, writer.Close()) }) @@ -35,22 +37,25 @@ func TestFileWriter_TimeRotation(t *testing.T) { time.Sleep(interval) _, err = writer.Write([]byte("Hello World 2")) require.NoError(t, err) - files, _ := os.ReadDir(tempDir) + files, err := os.ReadDir(tempDir) + require.NoError(t, err) require.Len(t, files, 2) } func TestFileWriter_ReopenTimeRotation(t *testing.T) { tempDir := t.TempDir() - interval, _ := time.ParseDuration("10ms") + interval, err := time.ParseDuration("10ms") + require.NoError(t, err) filePath := filepath.Join(tempDir, "test.log") - err := os.WriteFile(filePath, []byte("Hello World"), 0640) + err = os.WriteFile(filePath, []byte("Hello World"), 0640) time.Sleep(interval) require.NoError(t, err) writer, err := NewFileWriter(filepath.Join(tempDir, "test.log"), interval, 0, -1) require.NoError(t, err) t.Cleanup(func() { require.NoError(t, writer.Close()) }) - files, _ := os.ReadDir(tempDir) + files, err := os.ReadDir(tempDir) + require.NoError(t, err) require.Len(t, files, 2) } @@ -65,7 +70,8 @@ func TestFileWriter_SizeRotation(t *testing.T) { require.NoError(t, err) _, err = writer.Write([]byte("World 2")) require.NoError(t, err) - files, _ := os.ReadDir(tempDir) + files, err := os.ReadDir(tempDir) + require.NoError(t, err) require.Len(t, files, 2) } @@ -81,7 +87,8 @@ func TestFileWriter_ReopenSizeRotation(t *testing.T) { _, err = writer.Write([]byte("Hello World Again")) require.NoError(t, err) - files, _ := os.ReadDir(tempDir) + files, err := os.ReadDir(tempDir) + require.NoError(t, err) require.Len(t, files, 2) } @@ -108,7 +115,8 @@ func TestFileWriter_DeleteArchives(t *testing.T) { _, err = writer.Write([]byte("Third file")) require.NoError(t, err) - files, _ := os.ReadDir(tempDir) + files, err := os.ReadDir(tempDir) + require.NoError(t, err) require.Len(t, files, 3) for _, tempFile := range files { @@ -135,7 +143,8 @@ func TestFileWriter_CloseDoesNotRotate(t *testing.T) { require.NoError(t, err) require.NoError(t, writer.Close()) - files, _ := os.ReadDir(tempDir) + files, err := os.ReadDir(tempDir) + require.NoError(t, err) require.Len(t, files, 1) require.Regexp(t, "^test.log$", files[0].Name()) } diff --git a/internal/snmp/field.go b/internal/snmp/field.go index 603897ca43d36..ff101d8023878 100644 --- a/internal/snmp/field.go +++ b/internal/snmp/field.go @@ -129,10 +129,16 @@ func (f *Field) Convert(ent gosnmp.SnmpPDU) (v interface{}, err error) { case uint64: v = float64(vt) / math.Pow10(d) case []byte: - vf, _ := strconv.ParseFloat(string(vt), 64) + vf, err := strconv.ParseFloat(string(vt), 64) + if err != nil { + return nil, fmt.Errorf("failed to convert field to float with value %s: %w", vt, err) + } v = vf / math.Pow10(d) case string: - vf, _ := strconv.ParseFloat(vt, 64) + vf, err := strconv.ParseFloat(vt, 64) + if err != nil { + return nil, fmt.Errorf("failed to convert field to float with value %s: %w", vt, err) + } v = vf / math.Pow10(d) } return v, nil diff --git a/internal/snmp/translator_gosmi.go b/internal/snmp/translator_gosmi.go index 10509c62ea714..dc9e10195b5b1 100644 --- a/internal/snmp/translator_gosmi.go +++ b/internal/snmp/translator_gosmi.go @@ -136,7 +136,10 @@ func snmpTranslateCall(oid string) (mibName string, oidNum string, oidText strin } } oidNum = strings.Join(s, ".") - out, _ = gosmi.GetNodeByOID(types.OidMustFromString(oidNum)) + out, err = gosmi.GetNodeByOID(types.OidMustFromString(oidNum)) + if err != nil { + return oid, oid, oid, "", out, err + } } else { out, err = gosmi.GetNodeByOID(types.OidMustFromString(oid)) oidNum = oid diff --git a/internal/templating/engine_test.go b/internal/templating/engine_test.go index 1246db24de997..b778ca1a7b551 100644 --- a/internal/templating/engine_test.go +++ b/internal/templating/engine_test.go @@ -7,7 +7,8 @@ import ( ) func TestEngineAlternateSeparator(t *testing.T) { - defaultTemplate, _ := NewDefaultTemplateWithPattern("topic*") + defaultTemplate, err := NewDefaultTemplateWithPattern("topic*") + require.NoError(t, err) engine, err := NewEngine("_", defaultTemplate, []string{ "/ /*/*/* /measurement/origin/measurement*", }) diff --git a/logger/default_logger_test.go b/logger/default_logger_test.go index e4641b7ed080b..a93e73d55566b 100644 --- a/logger/default_logger_test.go +++ b/logger/default_logger_test.go @@ -111,7 +111,8 @@ func TestWriteToFileInRotation(t *testing.T) { log.Printf("I! TEST 1") // Writes 31 bytes, will rotate log.Printf("I! TEST") // Writes 29 byes, no rotation expected - files, _ := os.ReadDir(tempDir) + files, err := os.ReadDir(tempDir) + require.NoError(t, err) require.Len(t, files, 2) } diff --git a/migrations/inputs_httpjson/migration_test.go b/migrations/inputs_httpjson/migration_test.go index 02cbce2ea200f..fda159bcc8146 100644 --- a/migrations/inputs_httpjson/migration_test.go +++ b/migrations/inputs_httpjson/migration_test.go @@ -112,7 +112,8 @@ func TestParsing(t *testing.T) { // Start the test-server server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/stats" { - _, _ = w.Write(input) + _, err = w.Write(input) + require.NoError(t, err) } else { w.WriteHeader(http.StatusNotFound) }