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 (#2193)

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Nov 23, 2020
1 parent a460a4a commit 7874cd5
Show file tree
Hide file tree
Showing 32 changed files with 142 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ const totalInstanceName = "_Total"

// PerfCounterScraper scrapes performance counter data.
type PerfCounterScraper interface {
// Initialize initializes the PerfCounterScraper so that subsequent calls
// to Scrape will return performance counter data for the specified set.
// start initializes the PerfCounterScraper so that subsequent calls
// to scrape will return performance counter data for the specified set.
// of objects
Initialize(objects ...string) error
// Scrape returns performance data for the initialized objects.
// scrape returns performance data for the initialized objects.
Scrape() (PerfDataCollection, error)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

// MockPerfCounterScraperError is an implementation of PerfCounterScraper that returns
// the supplied errors when Scrape, GetObject, or GetValues are called.
// the supplied errors when scrape, GetObject, or GetValues are called.
type MockPerfCounterScraperError struct {
scrapeErr error
getObjectErr error
Expand All @@ -36,12 +36,12 @@ func NewMockPerfCounterScraperError(scrapeErr, getObjectErr, getValuesErr error)
return &MockPerfCounterScraperError{scrapeErr: scrapeErr, getObjectErr: getObjectErr, getValuesErr: getValuesErr}
}

// Initialize is a no-op
// start is a no-op
func (p *MockPerfCounterScraperError) Initialize(objects ...string) error {
return nil
}

// Scrape returns the specified scrapeErr or an object that will return a subsequent error
// scrape returns the specified scrapeErr or an object that will return a subsequent error
// if scrapeErr is nil
func (p *MockPerfCounterScraperError) Scrape() (PerfDataCollection, error) {
if p.scrapeErr != nil {
Expand Down Expand Up @@ -80,7 +80,7 @@ func (obj mockPerfDataObjectError) GetValues(counterNames ...string) ([]*Counter
}

// MockPerfCounterScraper is an implementation of PerfCounterScraper that returns the supplied
// object / counter values on each successive call to Scrape, in the specified order.
// object / counter values on each successive call to scrape, in the specified order.
//
// Example Usage:
//
Expand All @@ -91,7 +91,7 @@ func (obj mockPerfDataObjectError) GetValues(counterNames ...string) ([]*Counter
// },
// })
//
// s.Scrape().GetObject("Object1").GetValues("Counter1", "Counter2")
// s.scrape().GetObject("Object1").GetValues("Counter1", "Counter2")
//
// ... 1st call returns []*CounterValues{ { Values: { "Counter1": 1, "Counter2": 4 } } }
// ... 2nd call returns []*CounterValues{ { Values: { "Counter1": 2, "Counter2": 4 } } }
Expand All @@ -101,17 +101,17 @@ type MockPerfCounterScraper struct {
}

// NewMockPerfCounterScraper returns a MockPerfCounterScraper that will return the supplied
// object / counter values on each successive call to Scrape, in the specified order.
// object / counter values on each successive call to scrape, in the specified order.
func NewMockPerfCounterScraper(objectsAndValuesToReturn map[string]map[string][]int64) *MockPerfCounterScraper {
return &MockPerfCounterScraper{objectsAndValuesToReturn: objectsAndValuesToReturn}
}

// Initialize is a no-op
// start is a no-op
func (p *MockPerfCounterScraper) Initialize(objects ...string) error {
return nil
}

// Scrape returns a perf data collection with the supplied object / counter values,
// scrape returns a perf data collection with the supplied object / counter values,
// according to the supplied order.
func (p *MockPerfCounterScraper) Scrape() (PerfDataCollection, error) {
objectsAndValuesToReturn := make(map[string]map[string]int64, len(p.objectsAndValuesToReturn))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/host"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
Expand All @@ -44,8 +45,7 @@ func newCPUScraper(_ context.Context, cfg *Config) *scraper {
return &scraper{config: cfg, bootTime: host.BootTime, times: cpu.Times}
}

// Initialize
func (s *scraper) Initialize(_ context.Context) error {
func (s *scraper) start(context.Context, component.Host) error {
bootTime, err := s.bootTime()
if err != nil {
return err
Expand All @@ -55,8 +55,7 @@ func (s *scraper) Initialize(_ context.Context) error {
return nil
}

// Scrape
func (s *scraper) Scrape(_ context.Context) (pdata.MetricSlice, error) {
func (s *scraper) scrape(_ context.Context) (pdata.MetricSlice, error) {
metrics := pdata.NewMetricSlice()

now := internal.TimeToUnixNano(time.Now())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
Expand Down Expand Up @@ -71,14 +72,14 @@ func TestScrape(t *testing.T) {
scraper.times = test.timesFunc
}

err := scraper.Initialize(context.Background())
err := scraper.start(context.Background(), componenttest.NewNopHost())
if test.initializationErr != "" {
assert.EqualError(t, err, test.initializationErr)
return
}
require.NoError(t, err, "Failed to initialize cpu scraper: %v", err)

metrics, err := scraper.Scrape(context.Background())
metrics, err := scraper.scrape(context.Background())
if test.expectedErr != "" {
assert.EqualError(t, err, test.expectedErr)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (f *Factory) CreateMetricsScraper(

ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
s.scrape,
scraperhelper.WithStart(s.start),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/processor/filterset"
Expand Down Expand Up @@ -70,8 +71,7 @@ func newDiskScraper(_ context.Context, cfg *Config) (*scraper, error) {
return scraper, nil
}

// Initialize
func (s *scraper) Initialize(_ context.Context) error {
func (s *scraper) start(context.Context, component.Host) error {
bootTime, err := s.bootTime()
if err != nil {
return err
Expand All @@ -81,8 +81,7 @@ func (s *scraper) Initialize(_ context.Context) error {
return nil
}

// Scrape
func (s *scraper) Scrape(_ context.Context) (pdata.MetricSlice, error) {
func (s *scraper) scrape(_ context.Context) (pdata.MetricSlice, error) {
metrics := pdata.NewMetricSlice()

now := internal.TimeToUnixNano(time.Now())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumererror"
)

Expand Down Expand Up @@ -52,10 +53,10 @@ func TestScrape_Others(t *testing.T) {
scraper.ioCounters = test.ioCountersFunc
}

err = scraper.Initialize(context.Background())
err = scraper.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err, "Failed to initialize disk scraper: %v", err)

_, err = scraper.Scrape(context.Background())
_, err = scraper.scrape(context.Background())
assert.EqualError(t, err, test.expectedErr)

isPartial := consumererror.IsPartialScrapeError(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/processor/filterset"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
Expand Down Expand Up @@ -86,14 +87,14 @@ func TestScrape(t *testing.T) {
scraper.bootTime = test.bootTimeFunc
}

err = scraper.Initialize(context.Background())
err = scraper.start(context.Background(), componenttest.NewNopHost())
if test.initializationErr != "" {
assert.EqualError(t, err, test.initializationErr)
return
}
require.NoError(t, err, "Failed to initialize disk scraper: %v", err)

metrics, err := scraper.Scrape(context.Background())
metrics, err := scraper.scrape(context.Background())
require.NoError(t, err, "Failed to scrape metrics: %v", err)

if !test.expectMetrics {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/shirou/gopsutil/host"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/processor/filterset"
Expand Down Expand Up @@ -83,8 +84,7 @@ func newDiskScraper(_ context.Context, cfg *Config) (*scraper, error) {
return scraper, nil
}

// Initialize
func (s *scraper) Initialize(_ context.Context) error {
func (s *scraper) start(context.Context, component.Host) error {
bootTime, err := s.bootTime()
if err != nil {
return err
Expand All @@ -95,8 +95,7 @@ func (s *scraper) Initialize(_ context.Context) error {
return s.perfCounterScraper.Initialize(logicalDisk)
}

// Scrape
func (s *scraper) Scrape(ctx context.Context) (pdata.MetricSlice, error) {
func (s *scraper) scrape(ctx context.Context) (pdata.MetricSlice, error) {
metrics := pdata.NewMetricSlice()

now := internal.TimeToUnixNano(time.Now())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/perfcounters"
)
Expand Down Expand Up @@ -62,10 +63,10 @@ func TestScrape_Error(t *testing.T) {

scraper.perfCounterScraper = perfcounters.NewMockPerfCounterScraperError(test.scrapeErr, test.getObjectErr, test.getValuesErr)

err = scraper.Initialize(context.Background())
err = scraper.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err, "Failed to initialize disk scraper: %v", err)

_, err = scraper.Scrape(context.Background())
_, err = scraper.scrape(context.Background())
assert.EqualError(t, err, test.expectedErr)

isPartial := consumererror.IsPartialScrapeError(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (f *Factory) CreateMetricsScraper(

ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
s.scrape,
scraperhelper.WithStart(s.start),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func (f *Factory) CreateMetricsScraper(

ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
scraperhelper.WithClose(s.Close),
s.scrape,
scraperhelper.WithStart(s.start),
scraperhelper.WithShutdown(s.shutdown),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/shirou/gopsutil/load"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
Expand All @@ -42,18 +43,18 @@ func newLoadScraper(_ context.Context, logger *zap.Logger, cfg *Config) *scraper
return &scraper{logger: logger, config: cfg, load: getSampledLoadAverages}
}

// Initialize
func (s *scraper) Initialize(ctx context.Context) error {
// start
func (s *scraper) start(ctx context.Context, _ component.Host) error {
return startSampling(ctx, s.logger)
}

// Close
func (s *scraper) Close(ctx context.Context) error {
// shutdown
func (s *scraper) shutdown(ctx context.Context) error {
return stopSampling(ctx)
}

// Scrape
func (s *scraper) Scrape(_ context.Context) (pdata.MetricSlice, error) {
// scrape
func (s *scraper) scrape(_ context.Context) (pdata.MetricSlice, error) {
metrics := pdata.NewMetricSlice()

now := internal.TimeToUnixNano(time.Now())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
Expand Down Expand Up @@ -54,11 +55,11 @@ func TestScrape(t *testing.T) {
scraper.load = test.loadFunc
}

err := scraper.Initialize(context.Background())
err := scraper.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err, "Failed to initialize load scraper: %v", err)
defer func() { assert.NoError(t, scraper.Close(context.Background())) }()
defer func() { assert.NoError(t, scraper.shutdown(context.Background())) }()

metrics, err := scraper.Scrape(context.Background())
metrics, err := scraper.scrape(context.Background())
if test.expectedErr != "" {
assert.EqualError(t, err, test.expectedErr)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (f *Factory) CreateMetricsScraper(

ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
scraperhelper.WithInitialize(s.Initialize),
s.scrape,
scraperhelper.WithStart(s.start),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/net"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/processor/filterset"
Expand Down Expand Up @@ -70,8 +71,7 @@ func newNetworkScraper(_ context.Context, cfg *Config) (*scraper, error) {
return scraper, nil
}

// Initialize
func (s *scraper) Initialize(_ context.Context) error {
func (s *scraper) start(context.Context, component.Host) error {
bootTime, err := s.bootTime()
if err != nil {
return err
Expand All @@ -81,8 +81,7 @@ func (s *scraper) Initialize(_ context.Context) error {
return nil
}

// Scrape
func (s *scraper) Scrape(_ context.Context) (pdata.MetricSlice, error) {
func (s *scraper) scrape(_ context.Context) (pdata.MetricSlice, error) {
metrics := pdata.NewMetricSlice()

var errors []error
Expand Down
Loading

0 comments on commit 7874cd5

Please sign in to comment.