From db052c5f24d515f4ed6feb43849163f458feb430 Mon Sep 17 00:00:00 2001 From: Corbin Phelps Date: Tue, 25 Oct 2022 15:48:27 -0400 Subject: [PATCH] Bundled service shutdown and telemetry into a helper function in the collector Signed-off-by: Corbin Phelps --- service/collector.go | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/service/collector.go b/service/collector.go index b284f0bb91a..42bffac8f3a 100644 --- a/service/collector.go +++ b/service/collector.go @@ -149,16 +149,9 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error { } if err = col.service.Start(ctx); err != nil { - errs := err - if shutdownErr := col.service.Shutdown(ctx); shutdownErr != nil { - errs = multierr.Append(err, fmt.Errorf("failed to shutdown service after error: %w", shutdownErr)) + if shutdownErr := col.shutdownServiceAndTelemetry(ctx); shutdownErr != nil { + return multierr.Append(err, shutdownErr) } - - // TODO: Move this as part of the service shutdown. - if shutdownErr := col.service.telemetryInitializer.shutdown(); shutdownErr != nil { - errs = multierr.Append(errs, fmt.Errorf("failed to shutdown collector telemetry: %w", shutdownErr)) - } - return errs } col.setCollectorState(Running) return nil @@ -224,17 +217,30 @@ func (col *Collector) shutdown(ctx context.Context) error { errs = multierr.Append(errs, fmt.Errorf("failed to shutdown config provider: %w", err)) } + if err := col.shutdownServiceAndTelemetry(ctx); err != nil { + errs = multierr.Append(errs, err) + } + + col.setCollectorState(Closed) + + return errs +} + +// shutdownServiceAndTelemetry bundles shutting down the service and telemetryInitializer. +// Returned error will be in multierr form and wrapped. +func (col *Collector) shutdownServiceAndTelemetry(ctx context.Context) error { + var errs error + + // shutdown service if err := col.service.Shutdown(ctx); err != nil { - errs = multierr.Append(errs, fmt.Errorf("failed to shutdown service: %w", err)) + errs = multierr.Append(err, fmt.Errorf("failed to shutdown service after error: %w", err)) } // TODO: Move this as part of the service shutdown. + // shutdown telemetryInitializer if err := col.service.telemetryInitializer.shutdown(); err != nil { errs = multierr.Append(errs, fmt.Errorf("failed to shutdown collector telemetry: %w", err)) } - - col.setCollectorState(Closed) - return errs }