Skip to content

Commit

Permalink
scraperhelper: use common scraping interface (#3487)
Browse files Browse the repository at this point in the history
* scraperhelper: use common scraping interface

This removes the MetricsScraper and ResourceMetricsScraper interfaces in favor
of a single Scraper interface that scrapes and returns pdata.Metrics. There
were already helper wrappers that allow users to return MetricSlices and
ResourceMetricSlices. These helpers package those results into pdata.Metrics
for the base interface.

Resolves #3085: This also removes the multiMetricScraper which wrapped multiple
scrapers to appear as a single scraper but then functions like scraper.ID()
would be empty. This was evident when trying to log errors and you couldn't
figure out what scraper made the error because it was wrapped in the
multiscraper.

* review updates
  • Loading branch information
jrcamp authored Jun 28, 2021
1 parent ad5e50c commit 40d6a55
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 129 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 🧰 Bug fixes 🧰

- `scraperhelper`: Include the scraper name in log messages (#3487)

## v0.29.0 Beta

## 🛑 Breaking changes 🛑
Expand Down
8 changes: 4 additions & 4 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func createAddScraperOptions(
}

if ok {
scraperControllerOptions = append(scraperControllerOptions, scraperhelper.AddMetricsScraper(hostMetricsScraper))
scraperControllerOptions = append(scraperControllerOptions, scraperhelper.AddScraper(hostMetricsScraper))
continue
}

Expand All @@ -134,7 +134,7 @@ func createAddScraperOptions(
}

if ok {
scraperControllerOptions = append(scraperControllerOptions, scraperhelper.AddResourceMetricsScraper(resourceMetricsScraper))
scraperControllerOptions = append(scraperControllerOptions, scraperhelper.AddScraper(resourceMetricsScraper))
continue
}

Expand All @@ -144,7 +144,7 @@ func createAddScraperOptions(
return scraperControllerOptions, nil
}

func createHostMetricsScraper(ctx context.Context, logger *zap.Logger, key string, cfg internal.Config, factories map[string]internal.ScraperFactory) (scraper scraperhelper.MetricsScraper, ok bool, err error) {
func createHostMetricsScraper(ctx context.Context, logger *zap.Logger, key string, cfg internal.Config, factories map[string]internal.ScraperFactory) (scraper scraperhelper.Scraper, ok bool, err error) {
factory := factories[key]
if factory == nil {
ok = false
Expand All @@ -156,7 +156,7 @@ func createHostMetricsScraper(ctx context.Context, logger *zap.Logger, key strin
return
}

func createResourceMetricsScraper(ctx context.Context, logger *zap.Logger, key string, cfg internal.Config, factories map[string]internal.ResourceScraperFactory) (scraper scraperhelper.ResourceMetricsScraper, ok bool, err error) {
func createResourceMetricsScraper(ctx context.Context, logger *zap.Logger, key string, cfg internal.Config, factories map[string]internal.ResourceScraperFactory) (scraper scraperhelper.Scraper, ok bool, err error) {
factory := factories[key]
if factory == nil {
ok = false
Expand Down
16 changes: 8 additions & 8 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,32 +213,32 @@ type mockFactory struct{ mock.Mock }
type mockScraper struct{ mock.Mock }

func (m *mockFactory) CreateDefaultConfig() internal.Config { return &mockConfig{} }
func (m *mockFactory) CreateMetricsScraper(context.Context, *zap.Logger, internal.Config) (scraperhelper.MetricsScraper, error) {
func (m *mockFactory) CreateMetricsScraper(context.Context, *zap.Logger, internal.Config) (scraperhelper.Scraper, error) {
args := m.MethodCalled("CreateMetricsScraper")
return args.Get(0).(scraperhelper.MetricsScraper), args.Error(1)
return args.Get(0).(scraperhelper.Scraper), args.Error(1)
}

func (m *mockScraper) ID() config.ComponentID { return config.NewID("") }
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, config.ComponentID) (pdata.MetricSlice, error) {
return pdata.NewMetricSlice(), errors.New("err1")
func (m *mockScraper) Scrape(context.Context, config.ComponentID) (pdata.Metrics, error) {
return pdata.NewMetrics(), errors.New("err1")
}

type mockResourceFactory struct{ mock.Mock }
type mockResourceScraper struct{ mock.Mock }

func (m *mockResourceFactory) CreateDefaultConfig() internal.Config { return &mockConfig{} }
func (m *mockResourceFactory) CreateResourceMetricsScraper(context.Context, *zap.Logger, internal.Config) (scraperhelper.ResourceMetricsScraper, error) {
func (m *mockResourceFactory) CreateResourceMetricsScraper(context.Context, *zap.Logger, internal.Config) (scraperhelper.Scraper, error) {
args := m.MethodCalled("CreateResourceMetricsScraper")
return args.Get(0).(scraperhelper.ResourceMetricsScraper), args.Error(1)
return args.Get(0).(scraperhelper.Scraper), args.Error(1)
}

func (m *mockResourceScraper) ID() config.ComponentID { return config.NewID("") }
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, config.ComponentID) (pdata.ResourceMetricsSlice, error) {
return pdata.NewResourceMetricsSlice(), errors.New("err2")
func (m *mockResourceScraper) Scrape(context.Context, config.ComponentID) (pdata.Metrics, error) {
return pdata.NewMetrics(), errors.New("err2")
}

func TestGatherMetrics_ScraperKeyConfigError(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions receiver/hostmetricsreceiver/internal/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ScraperFactory interface {

// CreateMetricsScraper creates a scraper based on this config.
// If the config is not valid, error will be returned instead.
CreateMetricsScraper(ctx context.Context, logger *zap.Logger, cfg Config) (scraperhelper.MetricsScraper, error)
CreateMetricsScraper(ctx context.Context, logger *zap.Logger, cfg Config) (scraperhelper.Scraper, error)
}

// ResourceScraperFactory can create a ResourceScraper.
Expand All @@ -43,7 +43,7 @@ type ResourceScraperFactory interface {

// CreateResourceMetricsScraper creates a resource scraper based on this
// config. If the config is not valid, error will be returned instead.
CreateResourceMetricsScraper(ctx context.Context, logger *zap.Logger, cfg Config) (scraperhelper.ResourceMetricsScraper, error)
CreateResourceMetricsScraper(ctx context.Context, logger *zap.Logger, cfg Config) (scraperhelper.Scraper, error)
}

// Config is the configuration of a scraper.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
_ *zap.Logger,
config internal.Config,
) (scraperhelper.MetricsScraper, error) {
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)
s := newCPUScraper(ctx, cfg)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
_ *zap.Logger,
config internal.Config,
) (scraperhelper.MetricsScraper, error) {
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)
s, err := newDiskScraper(ctx, cfg)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
_ *zap.Logger,
config internal.Config,
) (scraperhelper.MetricsScraper, error) {
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)
s, err := newFileSystemScraper(ctx, cfg)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
logger *zap.Logger,
config internal.Config,
) (scraperhelper.MetricsScraper, error) {
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)
s := newLoadScraper(ctx, logger, cfg)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
_ *zap.Logger,
config internal.Config,
) (scraperhelper.MetricsScraper, error) {
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)
s := newMemoryScraper(ctx, cfg)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
_ *zap.Logger,
config internal.Config,
) (scraperhelper.MetricsScraper, error) {
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)
s, err := newNetworkScraper(ctx, cfg)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
_ *zap.Logger,
config internal.Config,
) (scraperhelper.MetricsScraper, error) {
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)
s := newPagingScraper(ctx, cfg)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
_ *zap.Logger,
config internal.Config,
) (scraperhelper.MetricsScraper, error) {
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)
s := newProcessesScraper(ctx, cfg)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (f *Factory) CreateResourceMetricsScraper(
_ context.Context,
_ *zap.Logger,
cfg internal.Config,
) (scraperhelper.ResourceMetricsScraper, error) {
) (scraperhelper.Scraper, error) {
if runtime.GOOS != "linux" && runtime.GOOS != "windows" {
return nil, errors.New("process scraper only available on Linux or Windows")
}
Expand Down
45 changes: 21 additions & 24 deletions receiver/scraperhelper/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,15 @@ type baseSettings struct {
// ScraperOption apply changes to internal options.
type ScraperOption func(*baseSettings)

// BaseScraper is the base interface for scrapers.
type BaseScraper interface {
// Scraper is the base interface for scrapers.
type Scraper interface {
component.Component

// ID returns the scraper id.
ID() config.ComponentID
Scrape(context.Context, config.ComponentID) (pdata.Metrics, error)
}

// MetricsScraper is an interface for scrapers that scrape metrics.
type MetricsScraper interface {
BaseScraper
Scrape(context.Context, config.ComponentID) (pdata.MetricSlice, error)
}

// ResourceMetricsScraper is an interface for scrapers that scrape resource metrics.
type ResourceMetricsScraper interface {
BaseScraper
Scrape(context.Context, config.ComponentID) (pdata.ResourceMetricsSlice, error)
}

var _ BaseScraper = (*baseScraper)(nil)

type baseScraper struct {
component.Component
id config.ComponentID
Expand Down Expand Up @@ -87,7 +74,7 @@ type metricsScraper struct {
ScrapeMetrics
}

var _ MetricsScraper = (*metricsScraper)(nil)
var _ Scraper = (*metricsScraper)(nil)

// NewMetricsScraper creates a Scraper that calls Scrape at the specified
// collection interval, reports observability information, and passes the
Expand All @@ -96,7 +83,7 @@ func NewMetricsScraper(
name string,
scrape ScrapeMetrics,
options ...ScraperOption,
) MetricsScraper {
) Scraper {
set := &baseSettings{}
for _, op := range options {
op(set)
Expand All @@ -113,7 +100,7 @@ func NewMetricsScraper(
return ms
}

func (ms metricsScraper) Scrape(ctx context.Context, receiverID config.ComponentID) (pdata.MetricSlice, error) {
func (ms metricsScraper) Scrape(ctx context.Context, receiverID config.ComponentID) (pdata.Metrics, error) {
ctx = obsreport.ScraperContext(ctx, receiverID, ms.ID())
scrp := obsreport.NewScraper(obsreport.ScraperSettings{ReceiverID: receiverID, Scraper: ms.ID()})
ctx = scrp.StartMetricsOp(ctx)
Expand All @@ -122,16 +109,20 @@ func (ms metricsScraper) Scrape(ctx context.Context, receiverID config.Component
if err == nil {
count = metrics.Len()
}
md := pdata.NewMetrics()
if metrics.Len() > 0 {
metrics.MoveAndAppendTo(md.ResourceMetrics().AppendEmpty().InstrumentationLibraryMetrics().AppendEmpty().Metrics())
}
scrp.EndMetricsOp(ctx, count, err)
return metrics, err
return md, err
}

type resourceMetricsScraper struct {
baseScraper
ScrapeResourceMetrics
}

var _ ResourceMetricsScraper = (*resourceMetricsScraper)(nil)
var _ Scraper = (*resourceMetricsScraper)(nil)

// NewResourceMetricsScraper creates a Scraper that calls Scrape at the
// specified collection interval, reports observability information, and
Expand All @@ -140,7 +131,7 @@ func NewResourceMetricsScraper(
id config.ComponentID,
scrape ScrapeResourceMetrics,
options ...ScraperOption,
) ResourceMetricsScraper {
) Scraper {
set := &baseSettings{}
for _, op := range options {
op(set)
Expand All @@ -157,7 +148,7 @@ func NewResourceMetricsScraper(
return rms
}

func (rms resourceMetricsScraper) Scrape(ctx context.Context, receiverID config.ComponentID) (pdata.ResourceMetricsSlice, error) {
func (rms resourceMetricsScraper) Scrape(ctx context.Context, receiverID config.ComponentID) (pdata.Metrics, error) {
ctx = obsreport.ScraperContext(ctx, receiverID, rms.ID())
scrp := obsreport.NewScraper(obsreport.ScraperSettings{ReceiverID: receiverID, Scraper: rms.ID()})
ctx = scrp.StartMetricsOp(ctx)
Expand All @@ -166,8 +157,14 @@ func (rms resourceMetricsScraper) Scrape(ctx context.Context, receiverID config.
if err == nil {
count = metricCount(resourceMetrics)
}

md := pdata.NewMetrics()
if resourceMetrics.Len() > 0 {
resourceMetrics.MoveAndAppendTo(md.ResourceMetrics())
}

scrp.EndMetricsOp(ctx, count, err)
return resourceMetrics, err
return md, err
}

func metricCount(resourceMetrics pdata.ResourceMetricsSlice) int {
Expand Down
Loading

0 comments on commit 40d6a55

Please sign in to comment.