Skip to content

Commit

Permalink
Remove usage of custom init/stop in scraper and use start/shutdown fr…
Browse files Browse the repository at this point in the history
…om component

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Nov 20, 2020
1 parent cc0a999 commit 8874631
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (f *Factory) CreateMetricsScraper(
ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
scraperhelper.WithStart(s.Initialize),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (f *Factory) CreateMetricsScraper(
ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
scraperhelper.WithStart(s.Initialize),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ func (f *Factory) CreateMetricsScraper(
ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
scraperhelper.WithClose(s.Close),
scraperhelper.WithStart(s.Initialize),
scraperhelper.WithShutdown(s.Close),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (f *Factory) CreateMetricsScraper(
ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
scraperhelper.WithStart(s.Initialize),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (f *Factory) CreateMetricsScraper(
ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
scraperhelper.WithStart(s.Initialize),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (f *Factory) CreateResourceMetricsScraper(
ms := scraperhelper.NewResourceMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
scraperhelper.WithStart(s.Initialize),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (f *Factory) CreateMetricsScraper(
ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
scraperhelper.WithStart(s.Initialize),
)

return ms, nil
Expand Down
74 changes: 30 additions & 44 deletions receiver/scraperhelper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenthelper"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/obsreport"
)
Expand All @@ -28,16 +29,8 @@ type ScrapeMetrics func(context.Context) (pdata.MetricSlice, error)
// Scrape resource metrics.
type ScrapeResourceMetrics func(context.Context) (pdata.ResourceMetricsSlice, error)

// Initialize performs any timely initialization tasks such as
// setting up performance counters for initial collection.
type Initialize func(ctx context.Context) error

// Close should clean up any unmanaged resources such as
// performance counter handles.
type Close func(ctx context.Context) error

// ScraperOption apply changes to internal options.
type ScraperOption func(*baseScraper)
type ScraperOption func(*componenthelper.ComponentSettings)

type BaseScraper interface {
component.Component
Expand All @@ -61,40 +54,25 @@ type ResourceMetricsScraper interface {
var _ BaseScraper = (*baseScraper)(nil)

type baseScraper struct {
name string
initialize Initialize
close Close
component.Component
name string
}

func (b baseScraper) Name() string {
return b.name
}

func (b baseScraper) Start(ctx context.Context, _ component.Host) error {
if b.initialize == nil {
return nil
}
return b.initialize(ctx)
}

func (b baseScraper) Shutdown(ctx context.Context) error {
if b.close == nil {
return nil
// WithStart sets the function that will be called on startup.
func WithStart(start componenthelper.Start) ScraperOption {
return func(s *componenthelper.ComponentSettings) {
s.Start = start
}
return b.close(ctx)
}

// WithInitialize sets the function that will be called on startup.
func WithInitialize(initialize Initialize) ScraperOption {
return func(o *baseScraper) {
o.initialize = initialize
}
}

// WithClose sets the function that will be called on shutdown.
func WithClose(close Close) ScraperOption {
return func(o *baseScraper) {
o.close = close
// WithShutdown sets the function that will be called on shutdown.
func WithShutdown(shutdown componenthelper.Shutdown) ScraperOption {
return func(s *componenthelper.ComponentSettings) {
s.Shutdown = shutdown
}
}

Expand All @@ -113,13 +91,17 @@ func NewMetricsScraper(
scrape ScrapeMetrics,
options ...ScraperOption,
) MetricsScraper {
ms := &metricsScraper{
baseScraper: baseScraper{name: name},
ScrapeMetrics: scrape,
set := componenthelper.DefaultComponentSettings()
for _, op := range options {
op(set)
}

for _, op := range options {
op(&ms.baseScraper)
ms := &metricsScraper{
baseScraper: baseScraper{
Component: componenthelper.NewComponent(set),
name: name,
},
ScrapeMetrics: scrape,
}

return ms
Expand Down Expand Up @@ -148,13 +130,17 @@ func NewResourceMetricsScraper(
scrape ScrapeResourceMetrics,
options ...ScraperOption,
) ResourceMetricsScraper {
rms := &resourceMetricsScraper{
baseScraper: baseScraper{name: name},
ScrapeResourceMetrics: scrape,
set := componenthelper.DefaultComponentSettings()
for _, op := range options {
op(set)
}

for _, op := range options {
op(&rms.baseScraper)
rms := &resourceMetricsScraper{
baseScraper: baseScraper{
Component: componenthelper.NewComponent(set),
name: name,
},
ScrapeResourceMetrics: scrape,
}

return rms
Expand Down
8 changes: 4 additions & 4 deletions receiver/scraperhelper/scrapercontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,12 @@ func configureMetricOptions(test metricsTestCase, initializeChs []chan bool, scr
if test.initialize {
initializeChs[i] = make(chan bool, 1)
ti := &testInitialize{ch: initializeChs[i], err: test.initializeErr}
scraperOptions = append(scraperOptions, WithInitialize(ti.initialize))
scraperOptions = append(scraperOptions, WithStart(ti.initialize))
}
if test.close {
closeChs[i] = make(chan bool, 1)
tc := &testClose{ch: closeChs[i], err: test.closeErr}
scraperOptions = append(scraperOptions, WithClose(tc.close))
scraperOptions = append(scraperOptions, WithShutdown(tc.close))
}

scrapeMetricsChs[i] = make(chan int)
Expand All @@ -297,12 +297,12 @@ func configureMetricOptions(test metricsTestCase, initializeChs []chan bool, scr
if test.initialize {
initializeChs[test.scrapers+i] = make(chan bool, 1)
ti := &testInitialize{ch: initializeChs[test.scrapers+i], err: test.initializeErr}
scraperOptions = append(scraperOptions, WithInitialize(ti.initialize))
scraperOptions = append(scraperOptions, WithStart(ti.initialize))
}
if test.close {
closeChs[test.scrapers+i] = make(chan bool, 1)
tc := &testClose{ch: closeChs[test.scrapers+i], err: test.closeErr}
scraperOptions = append(scraperOptions, WithClose(tc.close))
scraperOptions = append(scraperOptions, WithShutdown(tc.close))
}

testScrapeResourceMetricsChs[i] = make(chan int)
Expand Down

0 comments on commit 8874631

Please sign in to comment.