Skip to content

Commit

Permalink
Call combine error without checking for errors size (#1443)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Jul 27, 2020
1 parent a2b2aed commit 600fd5f
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 82 deletions.
18 changes: 11 additions & 7 deletions component/componenterror/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "; "))
}
12 changes: 2 additions & 10 deletions receiver/hostmetricsreceiver/hostmetrics_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions service/builder/exporters_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 2 additions & 9 deletions service/builder/extensions_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions service/builder/pipelines_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions service/builder/receivers_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 2 additions & 9 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 600fd5f

Please sign in to comment.