Skip to content

Commit

Permalink
Fix bug in setting the correct collector state after a config change …
Browse files Browse the repository at this point in the history
…event. (#5896)

* Fix bug in setting the correct collector state after a config change event.

Fixes #5830.

* Update collector_test.go

Co-authored-by: Bogdan Drutu <[email protected]>
Co-authored-by: Bogdan Drutu <[email protected]>
  • Loading branch information
3 people authored Aug 15, 2022
1 parent e89786d commit a56622d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

### 🧰 Bug fixes 🧰

- Fix bug in setting the correct collector state after a configuration change event. (#5830)

## v0.58.0 Beta

### 🛑 Breaking changes 🛑
Expand Down
3 changes: 1 addition & 2 deletions service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func (col *Collector) runAndWaitForShutdownEvent(ctx context.Context) error {
signal.Notify(col.signalsChannel, os.Interrupt, syscall.SIGTERM)
}

col.setCollectorState(Running)
LOOP:
for {
select {
Expand Down Expand Up @@ -198,7 +197,7 @@ func (col *Collector) setupConfigurationComponents(ctx context.Context) error {
if err = col.service.Start(ctx); err != nil {
return err
}

col.setCollectorState(Running)
return nil
}

Expand Down
48 changes: 48 additions & 0 deletions service/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,53 @@ func TestCollectorCancelContext(t *testing.T) {
assert.Equal(t, Closed, col.GetState())
}

type mockCfgProvider struct {
watcher chan error
}

func (p mockCfgProvider) Get(_ context.Context, _ component.Factories) (*Config, error) {
return generateConfig(), nil
}

func (p mockCfgProvider) Watch() <-chan error {
return p.watcher
}

func (p mockCfgProvider) Shutdown(_ context.Context) error {
return nil
}

func TestCollectorStateAfterConfigChange(t *testing.T) {
factories, err := componenttest.NopFactories()
require.NoError(t, err)

watcher := make(chan error, 1)
col, err := New(CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
ConfigProvider: &mockCfgProvider{watcher: watcher},
telemetry: newColTelemetry(featuregate.NewRegistry()),
})
require.NoError(t, err)

wg := startCollector(context.Background(), t, col)

assert.Eventually(t, func() bool {
return Running == col.GetState()
}, 2*time.Second, 200*time.Millisecond)

watcher <- nil

assert.Eventually(t, func() bool {
return Running == col.GetState()
}, 2*time.Second, 200*time.Millisecond)

col.Shutdown()

wg.Wait()
assert.Equal(t, Closed, col.GetState())
}

func TestCollectorReportError(t *testing.T) {
factories, err := componenttest.NopFactories()
require.NoError(t, err)
Expand Down Expand Up @@ -348,6 +395,7 @@ func testCollectorStartHelper(t *testing.T, telemetry *telemetryInitializer, tc
assertMetrics(t, metricsAddr, tc.expectedLabels)

assertZPages(t)

col.Shutdown()

wg.Wait()
Expand Down

0 comments on commit a56622d

Please sign in to comment.