Skip to content

Commit

Permalink
Bundled service shutdown and telemetry into a helper function in the …
Browse files Browse the repository at this point in the history
…collector

Signed-off-by: Corbin Phelps <[email protected]>
  • Loading branch information
Corbin Phelps committed Oct 25, 2022
1 parent 2444365 commit db052c5
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit db052c5

Please sign in to comment.