From 600fd5f4c599d1bcc023354d76a31342a35ccc70 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Mon, 27 Jul 2020 13:17:14 -0700 Subject: [PATCH] Call combine error without checking for errors size (#1443) Signed-off-by: Bogdan Drutu --- component/componenterror/errors.go | 18 +++++++++++------- .../hostmetrics_receiver.go | 12 ++---------- .../diskscraper/disk_scraper_windows.go | 6 +----- .../filesystemscraper/filesystem_scraper.go | 6 +----- .../scraper/networkscraper/network_scraper.go | 6 +----- .../scraper/processscraper/process_scraper.go | 12 ++---------- .../scraper/swapscraper/swap_scraper_others.go | 6 +----- .../swapscraper/swap_scraper_windows.go | 6 +----- service/builder/exporters_builder.go | 5 +---- service/builder/extensions_builder.go | 11 ++--------- service/builder/pipelines_builder.go | 5 +---- service/builder/receivers_builder.go | 5 +---- service/service.go | 11 ++--------- 13 files changed, 27 insertions(+), 82 deletions(-) diff --git a/component/componenterror/errors.go b/component/componenterror/errors.go index 03e88c41df4..09f376a484e 100644 --- a/component/componenterror/errors.go +++ b/component/componenterror/errors.go @@ -36,14 +36,18 @@ var ( // CombineErrors converts a list of errors into one error. func CombineErrors(errs []error) error { numErrors := len(errs) + if numErrors == 0 { + // No errors + return nil + } + if numErrors == 1 { return errs[0] - } else if numErrors > 1 { - errMsgs := make([]string, 0, numErrors) - for _, err := range errs { - errMsgs = append(errMsgs, err.Error()) - } - return fmt.Errorf("[%s]", strings.Join(errMsgs, "; ")) } - return nil + + errMsgs := make([]string, 0, numErrors) + for _, err := range errs { + errMsgs = append(errMsgs, err.Error()) + } + return fmt.Errorf("[%s]", strings.Join(errMsgs, "; ")) } diff --git a/receiver/hostmetricsreceiver/hostmetrics_receiver.go b/receiver/hostmetricsreceiver/hostmetrics_receiver.go index 15bd6975019..cace8e10f4b 100644 --- a/receiver/hostmetricsreceiver/hostmetrics_receiver.go +++ b/receiver/hostmetricsreceiver/hostmetrics_receiver.go @@ -199,11 +199,7 @@ func (hmr *receiver) scrapeAndAppendHostMetrics(ctx context.Context, metricData scraperMetrics.MoveAndAppendTo(metrics) } - if len(errors) > 0 { - return componenterror.CombineErrors(errors) - } - - return nil + return componenterror.CombineErrors(errors) } func (hmr *receiver) scrapeAndAppendResourceMetrics(ctx context.Context, metricData data.MetricData) error { @@ -223,11 +219,7 @@ func (hmr *receiver) scrapeAndAppendResourceMetrics(ctx context.Context, metricD scraperResourceMetrics.MoveAndAppendTo(rm) } - if len(errors) > 0 { - return componenterror.CombineErrors(errors) - } - - return nil + return componenterror.CombineErrors(errors) } func (hmr *receiver) closeScrapers(ctx context.Context) error { diff --git a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper_windows.go b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper_windows.go index 6ecc9f1ea55..50b95daf015 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper_windows.go +++ b/receiver/hostmetricsreceiver/internal/scraper/diskscraper/disk_scraper_windows.go @@ -225,11 +225,7 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { errors = append(errors, err) } - if len(errors) > 0 { - return metrics, componenterror.CombineErrors(errors) - } - - return metrics, nil + return metrics, componenterror.CombineErrors(errors) } func (s *scraper) scrapeAndAppendDiskIOMetric(metrics pdata.MetricSlice, durationSinceLastScraped float64) error { diff --git a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go index 6adb56b3ca7..8bac6aec528 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/filesystemscraper/filesystem_scraper.go @@ -107,11 +107,7 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { appendSystemSpecificMetrics(metrics, 1, usages) } - if len(errors) > 0 { - return metrics, componenterror.CombineErrors(errors) - } - - return metrics, nil + return metrics, componenterror.CombineErrors(errors) } func initializeFileSystemUsageMetric(metric pdata.Metric, deviceUsages []*deviceUsage) { diff --git a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/network_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/network_scraper.go index 1592d1a9df1..affad170ab6 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/networkscraper/network_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/networkscraper/network_scraper.go @@ -73,11 +73,7 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { errors = append(errors, err) } - if len(errors) > 0 { - return metrics, componenterror.CombineErrors(errors) - } - - return metrics, nil + return metrics, componenterror.CombineErrors(errors) } func (s *scraper) scrapeAndAppendNetworkCounterMetrics(metrics pdata.MetricSlice, startTime pdata.TimestampUnixNano) error { diff --git a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go index 8a807eeabe1..64a5a7b67ec 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go +++ b/receiver/hostmetricsreceiver/internal/scraper/processscraper/process_scraper.go @@ -111,11 +111,7 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.ResourceMetricsSlice, } } - if len(errs) > 0 { - return rms, componenterror.CombineErrors(errs) - } - - return rms, nil + return rms, componenterror.CombineErrors(errs) } // getProcessMetadata returns a slice of processMetadata, including handles, @@ -167,11 +163,7 @@ func (s *scraper) getProcessMetadata() ([]*processMetadata, error) { metadata = append(metadata, md) } - if len(errs) > 0 { - return metadata, componenterror.CombineErrors(errs) - } - - return metadata, nil + return metadata, componenterror.CombineErrors(errs) } func scrapeAndAppendCPUTimeMetric(metrics pdata.MetricSlice, startTime pdata.TimestampUnixNano, handle processHandle) error { diff --git a/receiver/hostmetricsreceiver/internal/scraper/swapscraper/swap_scraper_others.go b/receiver/hostmetricsreceiver/internal/scraper/swapscraper/swap_scraper_others.go index 1033aad1820..026195e0ea1 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/swapscraper/swap_scraper_others.go +++ b/receiver/hostmetricsreceiver/internal/scraper/swapscraper/swap_scraper_others.go @@ -75,11 +75,7 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { errors = append(errors, err) } - if len(errors) > 0 { - return metrics, componenterror.CombineErrors(errors) - } - - return metrics, nil + return metrics, componenterror.CombineErrors(errors) } func (s *scraper) scrapeAndAppendSwapUsageMetric(metrics pdata.MetricSlice) error { diff --git a/receiver/hostmetricsreceiver/internal/scraper/swapscraper/swap_scraper_windows.go b/receiver/hostmetricsreceiver/internal/scraper/swapscraper/swap_scraper_windows.go index d854c8d8479..f755cf04257 100644 --- a/receiver/hostmetricsreceiver/internal/scraper/swapscraper/swap_scraper_windows.go +++ b/receiver/hostmetricsreceiver/internal/scraper/swapscraper/swap_scraper_windows.go @@ -105,11 +105,7 @@ func (s *scraper) ScrapeMetrics(_ context.Context) (pdata.MetricSlice, error) { errors = append(errors, err) } - if len(errors) > 0 { - return metrics, componenterror.CombineErrors(errors) - } - - return metrics, nil + return metrics, componenterror.CombineErrors(errors) } func (s *scraper) scrapeAndAppendSwapUsageMetric(metrics pdata.MetricSlice) error { diff --git a/service/builder/exporters_builder.go b/service/builder/exporters_builder.go index 0cff7e3ed6a..fec1f07b012 100644 --- a/service/builder/exporters_builder.go +++ b/service/builder/exporters_builder.go @@ -107,10 +107,7 @@ func (exps Exporters) ShutdownAll(ctx context.Context) error { } } - if len(errs) != 0 { - return componenterror.CombineErrors(errs) - } - return nil + return componenterror.CombineErrors(errs) } func (exps Exporters) ToMapByDataType() map[configmodels.DataType]map[configmodels.Exporter]component.Exporter { diff --git a/service/builder/extensions_builder.go b/service/builder/extensions_builder.go index 8b0eb373821..7c4dc8d206e 100644 --- a/service/builder/extensions_builder.go +++ b/service/builder/extensions_builder.go @@ -71,10 +71,7 @@ func (exts Extensions) ShutdownAll(ctx context.Context) error { } } - if len(errs) != 0 { - return componenterror.CombineErrors(errs) - } - return nil + return componenterror.CombineErrors(errs) } func (exts Extensions) NotifyPipelineReady() error { @@ -102,11 +99,7 @@ func (exts Extensions) NotifyPipelineNotReady() error { } } - if len(errs) != 0 { - return componenterror.CombineErrors(errs) - } - - return nil + return componenterror.CombineErrors(errs) } func (exts Extensions) ToMap() map[configmodels.Extension]component.ServiceExtension { diff --git a/service/builder/pipelines_builder.go b/service/builder/pipelines_builder.go index d8b29a1a4e2..e367f21432b 100644 --- a/service/builder/pipelines_builder.go +++ b/service/builder/pipelines_builder.go @@ -76,10 +76,7 @@ func (bps BuiltPipelines) ShutdownProcessors(ctx context.Context) error { bp.logger.Info("Pipeline is shutdown.") } - if len(errs) != 0 { - return componenterror.CombineErrors(errs) - } - return nil + return componenterror.CombineErrors(errs) } // PipelinesBuilder builds pipelines from config. diff --git a/service/builder/receivers_builder.go b/service/builder/receivers_builder.go index 8db73774642..066e0792aaa 100644 --- a/service/builder/receivers_builder.go +++ b/service/builder/receivers_builder.go @@ -62,10 +62,7 @@ func (rcvs Receivers) ShutdownAll(ctx context.Context) error { } } - if len(errs) != 0 { - return componenterror.CombineErrors(errs) - } - return nil + return componenterror.CombineErrors(errs) } // StartAll starts all receivers. diff --git a/service/service.go b/service/service.go index 33856634e77..2db988d06c0 100644 --- a/service/service.go +++ b/service/service.go @@ -396,11 +396,7 @@ func (app *Application) shutdownPipelines(ctx context.Context) error { errs = append(errs, errors.Wrap(err, "failed to shutdown exporters")) } - if len(errs) != 0 { - return componenterror.CombineErrors(errs) - } - - return nil + return componenterror.CombineErrors(errs) } func (app *Application) shutdownExtensions(ctx context.Context) error { @@ -475,10 +471,7 @@ func (app *Application) execute(ctx context.Context, factory ConfigFactory) erro app.stateChannel <- Closed close(app.stateChannel) - if len(errs) != 0 { - return componenterror.CombineErrors(errs) - } - return nil + return componenterror.CombineErrors(errs) } // Start starts the collector according to the command and configuration