diff --git a/service/collector.go b/service/collector.go index 2bb9f1f9b26..b284f0bb91a 100644 --- a/service/collector.go +++ b/service/collector.go @@ -149,10 +149,16 @@ 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 { - return multierr.Append(err, fmt.Errorf("failed to shutdown service after error: %w", shutdownErr)) + errs = multierr.Append(err, fmt.Errorf("failed to shutdown service after error: %w", shutdownErr)) } - return err + + // 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 @@ -222,6 +228,11 @@ func (col *Collector) shutdown(ctx context.Context) error { errs = multierr.Append(errs, fmt.Errorf("failed to shutdown service: %w", err)) } + // TODO: Move this as part of the service shutdown. + 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 diff --git a/service/service.go b/service/service.go index d9b9180fce8..77e56db5be8 100644 --- a/service/service.go +++ b/service/service.go @@ -132,9 +132,6 @@ func (srv *service) Shutdown(ctx context.Context) error { errs = multierr.Append(errs, fmt.Errorf("failed to shutdown telemetry: %w", err)) } - if err := srv.telemetryInitializer.shutdown(); err != nil { - errs = multierr.Append(errs, fmt.Errorf("failed to shutdown telemetry initializer: %w", err)) - } // TODO: Shutdown MeterProvider. return errs } diff --git a/service/service_test.go b/service/service_test.go index e1638f5ef17..06a962f7d06 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -16,6 +16,8 @@ package service import ( "context" + "fmt" + "net/http" "path/filepath" "testing" @@ -152,6 +154,7 @@ func TestServiceTelemetryReusable(t *testing.T) { // Create a service telemetry := newColTelemetry(featuregate.NewRegistry()) + srvOne, err := newService(&settings{ BuildInfo: component.NewDefaultBuildInfo(), Factories: factories, @@ -160,9 +163,20 @@ func TestServiceTelemetryReusable(t *testing.T) { }) require.NoError(t, err) + // Setup tu curl the telemetry URL to ensure it works + telemetryURL := fmt.Sprintf("http://%s/metrics", telemetry.server.Addr) + // Start the service require.NoError(t, srvOne.Start(context.Background())) + // check telemetry server to ensure we get a response + var resp *http.Response + + // #nosec G107 + resp, err = http.Get(telemetryURL) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) + // Shutdown the service require.NoError(t, srvOne.Shutdown(context.Background())) @@ -185,6 +199,12 @@ func TestServiceTelemetryReusable(t *testing.T) { // Start the new service require.NoError(t, srvTwo.Start(context.Background())) + // check telemetry server to ensure we get a response + // #nosec G107 + resp, err = http.Get(telemetryURL) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) + // Shutdown the new service require.NoError(t, srvTwo.Shutdown(context.Background())) }