diff --git a/receiver/hostmetricsreceiver/hostmetrics_receiver_test.go b/receiver/hostmetricsreceiver/hostmetrics_receiver_test.go index e11047e16b7..c605154321a 100644 --- a/receiver/hostmetricsreceiver/hostmetrics_receiver_test.go +++ b/receiver/hostmetricsreceiver/hostmetrics_receiver_test.go @@ -26,6 +26,7 @@ import ( "github.com/stretchr/testify/require" "go.uber.org/zap" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/consumer/consumertest" "go.opentelemetry.io/collector/consumer/pdata" @@ -212,9 +213,9 @@ func (m *mockFactory) CreateMetricsScraper(context.Context, *zap.Logger, interna return args.Get(0).(receiverhelper.MetricsScraper), args.Error(1) } -func (m *mockScraper) Name() string { return "" } -func (m *mockScraper) Initialize(context.Context) error { return nil } -func (m *mockScraper) Close(context.Context) error { return nil } +func (m *mockScraper) Name() string { return "" } +func (m *mockScraper) Start(context.Context, component.Host) error { return nil } +func (m *mockScraper) Shutdown(context.Context) error { return nil } func (m *mockScraper) Scrape(context.Context, string) (pdata.MetricSlice, error) { return pdata.NewMetricSlice(), errors.New("err1") } @@ -228,9 +229,9 @@ func (m *mockResourceFactory) CreateResourceMetricsScraper(context.Context, *zap return args.Get(0).(receiverhelper.ResourceMetricsScraper), args.Error(1) } -func (m *mockResourceScraper) Name() string { return "" } -func (m *mockResourceScraper) Initialize(context.Context) error { return nil } -func (m *mockResourceScraper) Close(context.Context) error { return nil } +func (m *mockResourceScraper) Name() string { return "" } +func (m *mockResourceScraper) Start(context.Context, component.Host) error { return nil } +func (m *mockResourceScraper) Shutdown(context.Context) error { return nil } func (m *mockResourceScraper) Scrape(context.Context, string) (pdata.ResourceMetricsSlice, error) { return pdata.NewResourceMetricsSlice(), errors.New("err2") } diff --git a/receiver/receiverhelper/scraper.go b/receiver/receiverhelper/scraper.go index 7eb4dca002f..a21c9061090 100644 --- a/receiver/receiverhelper/scraper.go +++ b/receiver/receiverhelper/scraper.go @@ -17,6 +17,7 @@ package receiverhelper import ( "context" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer/pdata" "go.opentelemetry.io/collector/obsreport" ) @@ -39,16 +40,10 @@ type Close func(ctx context.Context) error type ScraperOption func(*baseScraper) type BaseScraper interface { + component.Component + // Name returns the scraper name Name() string - - // Initialize performs any timely initialization tasks such as - // setting up performance counters for initial collection. - Initialize(ctx context.Context) error - - // Close should clean up any unmanaged resources such as - // performance counter handles. - Close(ctx context.Context) error } // MetricsScraper is an interface for scrapers that scrape metrics. @@ -75,14 +70,14 @@ func (b baseScraper) Name() string { return b.name } -func (b baseScraper) Initialize(ctx context.Context) error { +func (b baseScraper) Start(ctx context.Context, _ component.Host) error { if b.initialize == nil { return nil } return b.initialize(ctx) } -func (b baseScraper) Close(ctx context.Context) error { +func (b baseScraper) Shutdown(ctx context.Context) error { if b.close == nil { return nil } diff --git a/receiver/receiverhelper/scrapercontroller.go b/receiver/receiverhelper/scrapercontroller.go index c1082143024..bee489e6dac 100644 --- a/receiver/receiverhelper/scrapercontroller.go +++ b/receiver/receiverhelper/scrapercontroller.go @@ -138,9 +138,11 @@ func NewScraperControllerReceiver( } // Start the receiver, invoked during service start. -func (sc *scraperController) Start(ctx context.Context, _ component.Host) error { - if err := sc.initializeScrapers(ctx); err != nil { - return err +func (sc *scraperController) Start(ctx context.Context, host component.Host) error { + for _, scraper := range sc.resourceMetricScrapers { + if err := scraper.Start(ctx, host); err != nil { + return err + } } sc.initialized = true @@ -157,24 +159,13 @@ func (sc *scraperController) Shutdown(ctx context.Context) error { <-sc.terminated } - var errors []error - - if err := sc.closeScrapers(ctx); err != nil { - errors = append(errors, err) - } - - return componenterror.CombineErrors(errors) -} - -// initializeScrapers initializes all the scrapers -func (sc *scraperController) initializeScrapers(ctx context.Context) error { + var errs []error for _, scraper := range sc.resourceMetricScrapers { - if err := scraper.Initialize(ctx); err != nil { - return err + if err := scraper.Shutdown(ctx); err != nil { + errs = append(errs, err) } } - - return nil + return componenterror.CombineErrors(errs) } // startScraping initiates a ticker that calls Scrape based on the configured @@ -232,19 +223,6 @@ func (sc *scraperController) stopScraping() { close(sc.done) } -// closeScrapers closes all the scrapers -func (sc *scraperController) closeScrapers(ctx context.Context) error { - var errs []error - - for _, scraper := range sc.resourceMetricScrapers { - if err := scraper.Close(ctx); err != nil { - errs = append(errs, err) - } - } - - return componenterror.CombineErrors(errs) -} - var _ ResourceMetricsScraper = (*multiMetricScraper)(nil) type multiMetricScraper struct { @@ -255,19 +233,19 @@ func (mms *multiMetricScraper) Name() string { return "" } -func (mms *multiMetricScraper) Initialize(ctx context.Context) error { +func (mms *multiMetricScraper) Start(ctx context.Context, host component.Host) error { for _, scraper := range mms.scrapers { - if err := scraper.Initialize(ctx); err != nil { + if err := scraper.Start(ctx, host); err != nil { return err } } return nil } -func (mms *multiMetricScraper) Close(ctx context.Context) error { +func (mms *multiMetricScraper) Shutdown(ctx context.Context) error { var errs []error for _, scraper := range mms.scrapers { - if err := scraper.Close(ctx); err != nil { + if err := scraper.Shutdown(ctx); err != nil { errs = append(errs, err) } }