Skip to content

Commit

Permalink
State set to closing when Collector Run encounters startup failure (#…
Browse files Browse the repository at this point in the history
…4974)

* When Run fails due to a startup issue it will set the state to closed

Signed-off-by: Corbin Phelps <corbin.phelps@bluemedora.com>

* Fixed changelog

Signed-off-by: Corbin Phelps <corbin.phelps@bluemedora.com>
  • Loading branch information
Corbin Phelps authored Mar 14, 2022
1 parent 5ee716f commit 26e67c3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

- Collector `Run` will now exit when a context cancels (#4954)
- Add missing droppedAttributesCount to pdata generated resource (#4979)
- Collector `Run` will now set state to `Closed` if startup fails (#4974)

## v0.46.0 Beta

Expand Down
2 changes: 2 additions & 0 deletions service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,12 @@ func (col *Collector) Run(ctx context.Context) error {
col.asyncErrorChannel = make(chan error)

if err := col.setupConfigurationComponents(ctx); err != nil {
col.setCollectorState(Closed)
return err
}

if err := collectorTelemetry.init(col); err != nil {
col.setCollectorState(Closed)
return err
}

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

// TestCollector_ClosedStateOnStartUpError tests the collector changes it's state to Closed when a startup error occurs
func TestCollector_ClosedStateOnStartUpError(t *testing.T) {
preservedAppTelemetry := collectorTelemetry
collectorTelemetry = &colTelemetry{}
defer func() { collectorTelemetry = preservedAppTelemetry }()

factories, err := testcomponents.NewDefaultFactories()
require.NoError(t, err)

// Load a bad config causing startup to fail
set := CollectorSettings{
BuildInfo: component.NewDefaultBuildInfo(),
Factories: factories,
ConfigProvider: MustNewDefaultConfigProvider([]string{filepath.Join("testdata", "otelcol-invalid.yaml")}, nil),
}
col, err := New(set)
require.NoError(t, err)

// Expect run to error
require.Error(t, col.Run(context.Background()))

// Expect state to be closed
assert.Equal(t, Closed, col.GetState())
}

type mockColTelemetry struct{}

func (tel *mockColTelemetry) init(*Collector) error {
Expand Down

0 comments on commit 26e67c3

Please sign in to comment.