From d837d72028714ad63d7c32a907505b56e1704b6c Mon Sep 17 00:00:00 2001 From: Matthew Wear Date: Wed, 23 Aug 2023 17:20:30 -0700 Subject: [PATCH] Add ReportFatalError behavior to ReportComponentStatus --- service/host.go | 3 +++ service/service_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/service/host.go b/service/host.go index c11ce0c3521..ced03e39e9c 100644 --- a/service/host.go +++ b/service/host.go @@ -42,6 +42,9 @@ func (host *serviceHost) ReportFatalError(err error) { func (host *serviceHost) ReportComponentStatus(source *component.InstanceID, event *component.StatusEvent) { // TODO: What should we do if there is an error notifying here? host.serviceExtensions.NotifyComponentStatusChange(source, event) //nolint:errcheck + if event.Status() == component.StatusFatalError { + host.asyncErrorChannel <- event.Err() + } } func (host *serviceHost) GetFactory(kind component.Kind, componentType component.Type) component.Factory { diff --git a/service/service_test.go b/service/service_test.go index 555a0d5a863..948d6ebac97 100644 --- a/service/service_test.go +++ b/service/service_test.go @@ -403,6 +403,29 @@ func TestNilCollectorEffectiveConfig(t *testing.T) { require.NoError(t, srv.Shutdown(context.Background())) } +func TestServiceFatalError(t *testing.T) { + set := newNopSettings() + set.AsyncErrorChannel = make(chan error) + + srv, err := New(context.Background(), set, newNopConfig()) + require.NoError(t, err) + + assert.NoError(t, srv.Start(context.Background())) + t.Cleanup(func() { + assert.NoError(t, srv.Shutdown(context.Background())) + }) + + go func() { + ev, _ := component.NewStatusEvent(component.StatusFatalError, component.WithError(assert.AnError)) + srv.host.ReportComponentStatus(&component.InstanceID{}, ev) + }() + + err = <-srv.host.asyncErrorChannel + + require.Error(t, err) + require.ErrorIs(t, err, assert.AnError) +} + func assertResourceLabels(t *testing.T, res pcommon.Resource, expectedLabels map[string]labelValue) { for key, labelValue := range expectedLabels { lookupKey, ok := prometheusToOtelConv[key]