Skip to content

Commit

Permalink
Use Component for start/shutdown of the Scraper (#2187)
Browse files Browse the repository at this point in the history
In a followup PR, after #2186 is merged, use component helper.

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Nov 20, 2020
1 parent 9bf0bb4 commit 1f3f91f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 51 deletions.
13 changes: 7 additions & 6 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand Down
15 changes: 5 additions & 10 deletions receiver/receiverhelper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package receiverhelper
import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/obsreport"
)
Expand All @@ -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.
Expand All @@ -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
}
Expand Down
48 changes: 13 additions & 35 deletions receiver/receiverhelper/scrapercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 1f3f91f

Please sign in to comment.