Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] use ReportComponentStatus #29907

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions extension/observer/ecsobserver/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 18 additions & 7 deletions extension/observer/ecsobserver/extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
})
}
4 changes: 2 additions & 2 deletions extension/observer/ecsobserver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}