diff --git a/extension/observer/ecsobserver/extension.go b/extension/observer/ecsobserver/extension.go index b3f832544f84..02db6fff56cc 100644 --- a/extension/observer/ecsobserver/extension.go +++ b/extension/observer/ecsobserver/extension.go @@ -15,31 +15,31 @@ var _ extension.Extension = (*ecsObserver)(nil) // ecsObserver implements component.ServiceExtension interface. type ecsObserver struct { - logger *zap.Logger - sd *serviceDiscovery + telemetrySettings component.TelemetrySettings + sd *serviceDiscovery // for Shutdown cancel func() } // Start runs the service discovery in background -func (e *ecsObserver) Start(_ context.Context, host component.Host) error { - e.logger.Info("Starting ECSDiscovery") +func (e *ecsObserver) Start(_ context.Context, _ component.Host) error { + e.telemetrySettings.Logger.Info("Starting ECSDiscovery") // Ignore the ctx parameter as it is not for long running operation ctx, cancel := context.WithCancel(context.Background()) e.cancel = cancel go func() { if err := e.sd.runAndWriteFile(ctx); err != nil { - e.logger.Error("ECSDiscovery stopped by error", zap.Error(err)) + e.telemetrySettings.Logger.Error("ECSDiscovery stopped by error", zap.Error(err)) // Stop the collector - host.ReportFatalError(err) + _ = e.telemetrySettings.ReportComponentStatus(component.NewFatalErrorEvent(err)) } }() return nil } func (e *ecsObserver) Shutdown(_ context.Context) error { - e.logger.Info("Stopping ECSDiscovery") + e.telemetrySettings.Logger.Info("Stopping ECSDiscovery") e.cancel() return nil } diff --git a/extension/observer/ecsobserver/extension_test.go b/extension/observer/ecsobserver/extension_test.go index 28b69ea3b628..72db6137f30b 100644 --- a/extension/observer/ecsobserver/extension_test.go +++ b/extension/observer/ecsobserver/extension_test.go @@ -97,12 +97,23 @@ func TestExtensionStartStop(t *testing.T) { t.Run("critical error", func(t *testing.T) { c := ecsmock.NewClusterWithName("different than default config") - ext := createTestExt(c, "testdata/ut_ext_critical_error.actual.yaml") - host := newInspectErrorHost() - require.NoError(t, ext.Start(context.TODO(), host)) - time.Sleep(waitDuration) - err := host.(*inspectErrorHost).getError() - require.Error(t, err) - require.Error(t, hasCriticalError(zap.NewExample(), err)) + f := newTestTaskFetcher(t, c) + cfg := createDefaultConfig() + sdCfg := cfg.(*Config) + sdCfg.RefreshInterval = 100 * time.Millisecond + sdCfg.ResultFile = "testdata/ut_ext_critical_error.actual.yaml" + cs := extensiontest.NewNopCreateSettings() + statusEventChan := make(chan *component.StatusEvent) + cs.TelemetrySettings.ReportComponentStatus = func(e *component.StatusEvent) error { + statusEventChan <- e + return nil + } + ext, err := createExtensionWithFetcher(cs, sdCfg, f) + require.NoError(t, err) + err = ext.Start(context.Background(), componenttest.NewNopHost()) + require.NoError(t, err) + e := <-statusEventChan + require.Error(t, e.Err()) + require.Error(t, hasCriticalError(zap.NewExample(), e.Err())) }) } diff --git a/extension/observer/ecsobserver/factory.go b/extension/observer/ecsobserver/factory.go index 7e444f7c3c82..615136d0f6f8 100644 --- a/extension/observer/ecsobserver/factory.go +++ b/extension/observer/ecsobserver/factory.go @@ -44,7 +44,7 @@ func createExtensionWithFetcher(params extension.CreateSettings, sdCfg *Config, return nil, err } return &ecsObserver{ - logger: params.Logger, - sd: sd, + telemetrySettings: params.TelemetrySettings, + sd: sd, }, nil }