From 29b576e7d340d36553c12fe5a201b0938b5df304 Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Sat, 15 May 2021 00:04:18 +0200 Subject: [PATCH] Fix some errcheck errors (#2881) * Check testbed errors * Check some more errors * Empty commit to retrigger CI * Address comments on hostmetricsreceiver * Rewrite some errchecking code * Inline ifs * Inline ifs and change log.Fatalf + Error() calls by log.Fatal --- component/componenttest/shutdown_verifier.go | 2 +- exporter/otlphttpexporter/otlp.go | 2 +- processor/processorhelper/hasher.go | 2 +- .../processesscraper/processes_scraper_unix.go | 4 +++- receiver/kafkareceiver/kafka_receiver.go | 2 +- receiver/otlpreceiver/otlphttp.go | 2 +- receiver/zipkinreceiver/trace_receiver.go | 2 +- service/zpages.go | 6 +++--- testbed/testbed/child_process.go | 4 +++- testbed/testbed/mock_backend.go | 5 +++-- testbed/testbed/results.go | 12 ++++++++---- testbed/testbed/test_case.go | 4 +++- 12 files changed, 29 insertions(+), 18 deletions(-) diff --git a/component/componenttest/shutdown_verifier.go b/component/componenttest/shutdown_verifier.go index e582d8f9cf9..71831a25421 100644 --- a/component/componenttest/shutdown_verifier.go +++ b/component/componenttest/shutdown_verifier.go @@ -50,7 +50,7 @@ func verifyTracesProcessorDoesntProduceAfterShutdown(t *testing.T, factory compo // Send some traces to the processor. const generatedCount = 10 for i := 0; i < generatedCount; i++ { - processor.ConsumeTraces(context.Background(), testdata.GenerateTraceDataOneSpan()) + require.NoError(t, processor.ConsumeTraces(context.Background(), testdata.GenerateTraceDataOneSpan())) } // Now shutdown the processor. diff --git a/exporter/otlphttpexporter/otlp.go b/exporter/otlphttpexporter/otlp.go index 4760b18333d..dddee78d751 100644 --- a/exporter/otlphttpexporter/otlp.go +++ b/exporter/otlphttpexporter/otlp.go @@ -126,7 +126,7 @@ func (e *exporter) export(ctx context.Context, url string, request []byte) error defer func() { // Discard any remaining response body when we are done reading. - io.CopyN(ioutil.Discard, resp.Body, maxHTTPResponseReadBytes) + io.CopyN(ioutil.Discard, resp.Body, maxHTTPResponseReadBytes) // nolint:errcheck resp.Body.Close() }() diff --git a/processor/processorhelper/hasher.go b/processor/processorhelper/hasher.go index 6f0999a9568..aad2cfe557c 100644 --- a/processor/processorhelper/hasher.go +++ b/processor/processorhelper/hasher.go @@ -61,7 +61,7 @@ func sha1Hasher(attr pdata.AttributeValue) { if len(val) > 0 { // #nosec h := sha1.New() - h.Write(val) + h.Write(val) // nolint: errcheck val = h.Sum(nil) hashedBytes := make([]byte, hex.EncodedLen(len(val))) hex.Encode(hashedBytes, val) diff --git a/receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_scraper_unix.go b/receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_scraper_unix.go index 17b1e7c3a4d..a4135ecc154 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_scraper_unix.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processesscraper/processes_scraper_unix.go @@ -40,7 +40,9 @@ func appendSystemSpecificProcessesMetrics(metrics pdata.MetricSlice, startIndex metrics.Resize(startIndex + unixMetricsLen) initializeProcessesCountMetric(metrics.At(startIndex+0), now, misc) - appendUnixSystemSpecificProcessesMetrics(metrics, startIndex+1, now, misc) + if err = appendUnixSystemSpecificProcessesMetrics(metrics, startIndex+1, now, misc); err != nil { + return err + } return nil } diff --git a/receiver/kafkareceiver/kafka_receiver.go b/receiver/kafkareceiver/kafka_receiver.go index 1d953aec61c..c21343cff28 100644 --- a/receiver/kafkareceiver/kafka_receiver.go +++ b/receiver/kafkareceiver/kafka_receiver.go @@ -109,7 +109,7 @@ func (c *kafkaTracesConsumer) Start(context.Context, component.Host) error { nextConsumer: c.nextConsumer, ready: make(chan bool), } - go c.consumeLoop(ctx, consumerGroup) + go c.consumeLoop(ctx, consumerGroup) // nolint:errcheck <-consumerGroup.ready return nil } diff --git a/receiver/otlpreceiver/otlphttp.go b/receiver/otlpreceiver/otlphttp.go index c94b21655f0..9da35519f64 100644 --- a/receiver/otlpreceiver/otlphttp.go +++ b/receiver/otlpreceiver/otlphttp.go @@ -72,5 +72,5 @@ func errorHandler(w http.ResponseWriter, r *http.Request, errMsg string, statusC w.Header().Set("Content-Type", contentType) w.WriteHeader(statusCode) - w.Write(msg) + w.Write(msg) // nolint:errcheck } diff --git a/receiver/zipkinreceiver/trace_receiver.go b/receiver/zipkinreceiver/trace_receiver.go index 558ac5f46e6..df80fc4a1d1 100644 --- a/receiver/zipkinreceiver/trace_receiver.go +++ b/receiver/zipkinreceiver/trace_receiver.go @@ -250,7 +250,7 @@ func (zr *ZipkinReceiver) ServeHTTP(w http.ResponseWriter, r *http.Request) { if consumerErr != nil { // Transient error, due to some internal condition. w.WriteHeader(http.StatusInternalServerError) - w.Write(errNextConsumerRespBody) + w.Write(errNextConsumerRespBody) // nolint:errcheck return } diff --git a/service/zpages.go b/service/zpages.go index 716dcee6885..c89a7d11479 100644 --- a/service/zpages.go +++ b/service/zpages.go @@ -40,7 +40,7 @@ func (srv *service) RegisterZPages(mux *http.ServeMux, pathPrefix string) { } func (srv *service) handleServicezRequest(w http.ResponseWriter, r *http.Request) { - r.ParseForm() + r.ParseForm() // nolint:errcheck w.Header().Set("Content-Type", "text/html; charset=utf-8") zpages.WriteHTMLHeader(w, zpages.HeaderData{Title: "service"}) zpages.WriteHTMLComponentHeader(w, zpages.ComponentHeaderData{ @@ -58,7 +58,7 @@ func (srv *service) handleServicezRequest(w http.ResponseWriter, r *http.Request } func (srv *service) handlePipelinezRequest(w http.ResponseWriter, r *http.Request) { - r.ParseForm() + r.ParseForm() // nolint:errcheck w.Header().Set("Content-Type", "text/html; charset=utf-8") pipelineName := r.Form.Get(zPipelineName) componentName := r.Form.Get(zComponentName) @@ -116,7 +116,7 @@ func (srv *service) getPipelinesSummaryTableData() zpages.SummaryPipelinesTableD } func handleExtensionzRequest(host component.Host, w http.ResponseWriter, r *http.Request) { - r.ParseForm() + r.ParseForm() // nolint:errcheck w.Header().Set("Content-Type", "text/html; charset=utf-8") extensionName := r.Form.Get(zExtensionName) zpages.WriteHTMLHeader(w, zpages.HeaderData{Title: "Extensions"}) diff --git a/testbed/testbed/child_process.go b/testbed/testbed/child_process.go index dbdad37d56e..c4d70da239f 100644 --- a/testbed/testbed/child_process.go +++ b/testbed/testbed/child_process.go @@ -375,7 +375,9 @@ func (cp *ChildProcess) WatchResourceConsumption() error { cp.fetchCPUUsage() if err := cp.checkAllowedResourceUsage(); err != nil { - cp.Stop() + if _, errStop := cp.Stop(); errStop != nil { + log.Printf("Failed to stop child process: %v", err) + } return err } diff --git a/testbed/testbed/mock_backend.go b/testbed/testbed/mock_backend.go index eac349e1ae0..2b95ca80902 100644 --- a/testbed/testbed/mock_backend.go +++ b/testbed/testbed/mock_backend.go @@ -100,8 +100,9 @@ func (mb *MockBackend) Stop() { log.Printf("Stopping mock backend...") mb.logFile.Close() - mb.receiver.Stop() - + if err := mb.receiver.Stop(); err != nil { + log.Printf("Failed to stop receiver: %v", err) + } // Print stats. log.Printf("Stopped backend. %s", mb.GetStats()) }) diff --git a/testbed/testbed/results.go b/testbed/testbed/results.go index 6fdec6c586d..161e39b5408 100644 --- a/testbed/testbed/results.go +++ b/testbed/testbed/results.go @@ -61,11 +61,13 @@ func (r *PerformanceResults) Init(resultsDir string) { r.perTestResults = []*PerformanceTestResult{} // Create resultsSummary file - os.MkdirAll(resultsDir, os.FileMode(0755)) + if err := os.MkdirAll(resultsDir, os.FileMode(0755)); err != nil { + log.Fatal(err) + } var err error r.resultsFile, err = os.Create(path.Join(r.resultsDir, "TESTRESULTS.md")) if err != nil { - log.Fatalf(err.Error()) + log.Fatal(err) } // Write the header @@ -145,11 +147,13 @@ func (r *CorrectnessResults) Init(resultsDir string) { r.perTestResults = []*CorrectnessTestResult{} // Create resultsSummary file - os.MkdirAll(resultsDir, os.FileMode(0755)) + if err := os.MkdirAll(resultsDir, os.FileMode(0755)); err != nil { + log.Fatal(err) + } var err error r.resultsFile, err = os.Create(path.Join(r.resultsDir, "CORRECTNESSRESULTS.md")) if err != nil { - log.Fatalf(err.Error()) + log.Fatal(err) } // Write the header diff --git a/testbed/testbed/test_case.go b/testbed/testbed/test_case.go index 698f4bd120c..010bb23afb2 100644 --- a/testbed/testbed/test_case.go +++ b/testbed/testbed/test_case.go @@ -207,7 +207,9 @@ func (tc *TestCase) StartAgent(args ...string) { // StopAgent stops agent process. func (tc *TestCase) StopAgent() { - tc.agentProc.Stop() + if _, err := tc.agentProc.Stop(); err != nil { + tc.indicateError(err) + } } // StartLoad starts the load generator and redirects its standard output and standard error