Skip to content

Commit

Permalink
Move scraper helpers to its own package scraperhelper
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu committed Nov 19, 2020
1 parent 29859f2 commit fdc8624
Show file tree
Hide file tree
Showing 24 changed files with 103 additions and 102 deletions.
6 changes: 3 additions & 3 deletions receiver/hostmetricsreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package hostmetricsreceiver

import (
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

// Config defines configuration for HostMetrics receiver.
type Config struct {
receiverhelper.ScraperControllerSettings `mapstructure:",squash"`
Scrapers map[string]internal.Config `mapstructure:"-"`
scraperhelper.ScraperControllerSettings `mapstructure:",squash"`
Scrapers map[string]internal.Config `mapstructure:"-"`
}
4 changes: 2 additions & 2 deletions receiver/hostmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/processesscraper"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/processscraper"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/swapscraper"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

func TestLoadConfig(t *testing.T) {
Expand All @@ -62,7 +62,7 @@ func TestLoadConfig(t *testing.T) {

r1 := cfg.Receivers["hostmetrics/customname"].(*Config)
expectedConfig := &Config{
ScraperControllerSettings: receiverhelper.ScraperControllerSettings{
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
ReceiverSettings: configmodels.ReceiverSettings{
TypeVal: typeStr,
NameVal: "hostmetrics/customname",
Expand Down
17 changes: 9 additions & 8 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/processscraper"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/swapscraper"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

// This file implements Factory for HostMetrics receiver.
Expand Down Expand Up @@ -136,7 +137,7 @@ func getScraperFactory(key string) (internal.BaseFactory, bool) {

// createDefaultConfig creates the default configuration for receiver.
func createDefaultConfig() configmodels.Receiver {
return &Config{ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(typeStr)}
return &Config{ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(typeStr)}
}

// createMetricsReceiver creates a metrics receiver based on provided config.
Expand All @@ -153,7 +154,7 @@ func createMetricsReceiver(
return nil, err
}

return receiverhelper.NewScraperControllerReceiver(
return scraperhelper.NewScraperControllerReceiver(
&oCfg.ScraperControllerSettings,
params.Logger,
consumer,
Expand All @@ -167,8 +168,8 @@ func createAddScraperOptions(
config *Config,
factories map[string]internal.ScraperFactory,
resourceFactories map[string]internal.ResourceScraperFactory,
) ([]receiverhelper.ScraperControllerOption, error) {
scraperControllerOptions := make([]receiverhelper.ScraperControllerOption, 0, len(config.Scrapers))
) ([]scraperhelper.ScraperControllerOption, error) {
scraperControllerOptions := make([]scraperhelper.ScraperControllerOption, 0, len(config.Scrapers))

for key, cfg := range config.Scrapers {
hostMetricsScraper, ok, err := createHostMetricsScraper(ctx, logger, key, cfg, factories)
Expand All @@ -177,7 +178,7 @@ func createAddScraperOptions(
}

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

Expand All @@ -187,7 +188,7 @@ func createAddScraperOptions(
}

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

Expand All @@ -197,7 +198,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 receiverhelper.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.MetricsScraper, ok bool, err error) {
factory := factories[key]
if factory == nil {
ok = false
Expand All @@ -209,7 +210,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 receiverhelper.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.ResourceMetricsScraper, ok bool, err error) {
factory := factories[key]
if factory == nil {
ok = false
Expand Down
38 changes: 19 additions & 19 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/processesscraper"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/processscraper"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal/scraper/swapscraper"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

var standardMetrics = []string{
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestGatherMetrics_EndToEnd(t *testing.T) {
sink := new(consumertest.MetricsSink)

config := &Config{
ScraperControllerSettings: receiverhelper.ScraperControllerSettings{
ScraperControllerSettings: scraperhelper.ScraperControllerSettings{
CollectionInterval: 100 * time.Millisecond,
},
Scrapers: map[string]internal.Config{
Expand Down Expand Up @@ -207,9 +207,9 @@ 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) (receiverhelper.MetricsScraper, error) {
func (m *mockFactory) CreateMetricsScraper(context.Context, *zap.Logger, internal.Config) (scraperhelper.MetricsScraper, error) {
args := m.MethodCalled("CreateMetricsScraper")
return args.Get(0).(receiverhelper.MetricsScraper), args.Error(1)
return args.Get(0).(scraperhelper.MetricsScraper), args.Error(1)
}

func (m *mockScraper) Name() string { return "" }
Expand All @@ -223,9 +223,9 @@ 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) (receiverhelper.ResourceMetricsScraper, error) {
func (m *mockResourceFactory) CreateResourceMetricsScraper(context.Context, *zap.Logger, internal.Config) (scraperhelper.ResourceMetricsScraper, error) {
args := m.MethodCalled("CreateResourceMetricsScraper")
return args.Get(0).(receiverhelper.ResourceMetricsScraper), args.Error(1)
return args.Get(0).(scraperhelper.ResourceMetricsScraper), args.Error(1)
}

func (m *mockResourceScraper) Name() string { return "" }
Expand Down Expand Up @@ -294,9 +294,9 @@ func benchmarkScrapeMetrics(b *testing.B, cfg *Config) {

options, err := createAddScraperOptions(context.Background(), zap.NewNop(), cfg, scraperFactories, resourceScraperFactories)
require.NoError(b, err)
options = append(options, receiverhelper.WithTickerChannel(tickerCh))
options = append(options, scraperhelper.WithTickerChannel(tickerCh))

receiver, err := receiverhelper.NewScraperControllerReceiver(&cfg.ScraperControllerSettings, zap.NewNop(), sink, options...)
receiver, err := scraperhelper.NewScraperControllerReceiver(&cfg.ScraperControllerSettings, zap.NewNop(), sink, options...)
require.NoError(b, err)

require.NoError(b, receiver.Start(context.Background(), componenttest.NewNopHost()))
Expand All @@ -314,7 +314,7 @@ func benchmarkScrapeMetrics(b *testing.B, cfg *Config) {

func Benchmark_ScrapeCpuMetrics(b *testing.B) {
cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{cpuscraper.TypeStr: (&cpuscraper.Factory{}).CreateDefaultConfig()},
}

Expand All @@ -323,7 +323,7 @@ func Benchmark_ScrapeCpuMetrics(b *testing.B) {

func Benchmark_ScrapeDiskMetrics(b *testing.B) {
cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{diskscraper.TypeStr: (&diskscraper.Factory{}).CreateDefaultConfig()},
}

Expand All @@ -332,7 +332,7 @@ func Benchmark_ScrapeDiskMetrics(b *testing.B) {

func Benchmark_ScrapeFileSystemMetrics(b *testing.B) {
cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{filesystemscraper.TypeStr: (&filesystemscraper.Factory{}).CreateDefaultConfig()},
}

Expand All @@ -341,7 +341,7 @@ func Benchmark_ScrapeFileSystemMetrics(b *testing.B) {

func Benchmark_ScrapeLoadMetrics(b *testing.B) {
cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{loadscraper.TypeStr: (&loadscraper.Factory{}).CreateDefaultConfig()},
}

Expand All @@ -350,7 +350,7 @@ func Benchmark_ScrapeLoadMetrics(b *testing.B) {

func Benchmark_ScrapeMemoryMetrics(b *testing.B) {
cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{memoryscraper.TypeStr: (&memoryscraper.Factory{}).CreateDefaultConfig()},
}

Expand All @@ -359,7 +359,7 @@ func Benchmark_ScrapeMemoryMetrics(b *testing.B) {

func Benchmark_ScrapeNetworkMetrics(b *testing.B) {
cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{networkscraper.TypeStr: (&networkscraper.Factory{}).CreateDefaultConfig()},
}

Expand All @@ -368,7 +368,7 @@ func Benchmark_ScrapeNetworkMetrics(b *testing.B) {

func Benchmark_ScrapeProcessesMetrics(b *testing.B) {
cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{processesscraper.TypeStr: (&processesscraper.Factory{}).CreateDefaultConfig()},
}

Expand All @@ -377,7 +377,7 @@ func Benchmark_ScrapeProcessesMetrics(b *testing.B) {

func Benchmark_ScrapeSwapMetrics(b *testing.B) {
cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{swapscraper.TypeStr: (&swapscraper.Factory{}).CreateDefaultConfig()},
}

Expand All @@ -390,7 +390,7 @@ func Benchmark_ScrapeProcessMetrics(b *testing.B) {
}

cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{processscraper.TypeStr: (&processscraper.Factory{}).CreateDefaultConfig()},
}

Expand All @@ -399,7 +399,7 @@ func Benchmark_ScrapeProcessMetrics(b *testing.B) {

func Benchmark_ScrapeSystemMetrics(b *testing.B) {
cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{
cpuscraper.TypeStr: (&cpuscraper.Factory{}).CreateDefaultConfig(),
diskscraper.TypeStr: (&diskscraper.Factory{}).CreateDefaultConfig(),
Expand All @@ -417,7 +417,7 @@ func Benchmark_ScrapeSystemMetrics(b *testing.B) {

func Benchmark_ScrapeSystemAndProcessMetrics(b *testing.B) {
cfg := &Config{
ScraperControllerSettings: receiverhelper.DefaultScraperControllerSettings(""),
ScraperControllerSettings: scraperhelper.DefaultScraperControllerSettings(""),
Scrapers: map[string]internal.Config{
cpuscraper.TypeStr: &cpuscraper.Config{},
diskscraper.TypeStr: &diskscraper.Config{},
Expand Down
6 changes: 3 additions & 3 deletions receiver/hostmetricsreceiver/internal/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"go.uber.org/zap"

"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

// BaseFactory for creating Scrapers.
Expand All @@ -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) (receiverhelper.MetricsScraper, error)
CreateMetricsScraper(ctx context.Context, logger *zap.Logger, cfg Config) (scraperhelper.MetricsScraper, 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) (receiverhelper.ResourceMetricsScraper, error)
CreateResourceMetricsScraper(ctx context.Context, logger *zap.Logger, cfg Config) (scraperhelper.ResourceMetricsScraper, error)
}

// Config is the configuration of a scraper.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"go.uber.org/zap"

"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

// This file implements Factory for CPU scraper.
Expand All @@ -44,14 +44,14 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
_ *zap.Logger,
config internal.Config,
) (receiverhelper.MetricsScraper, error) {
) (scraperhelper.MetricsScraper, error) {
cfg := config.(*Config)
s := newCPUScraper(ctx, cfg)

ms := receiverhelper.NewMetricsScraper(
ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
receiverhelper.WithInitialize(s.Initialize),
scraperhelper.WithInitialize(s.Initialize),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"go.uber.org/zap"

"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

// This file implements Factory for Disk scraper.
Expand All @@ -44,17 +44,17 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
_ *zap.Logger,
config internal.Config,
) (receiverhelper.MetricsScraper, error) {
) (scraperhelper.MetricsScraper, error) {
cfg := config.(*Config)
s, err := newDiskScraper(ctx, cfg)
if err != nil {
return nil, err
}

ms := receiverhelper.NewMetricsScraper(
ms := scraperhelper.NewMetricsScraper(
TypeStr,
s.Scrape,
receiverhelper.WithInitialize(s.Initialize),
scraperhelper.WithInitialize(s.Initialize),
)

return ms, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"go.uber.org/zap"

"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

// This file implements Factory for FileSystem scraper.
Expand Down Expand Up @@ -49,14 +49,14 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
_ *zap.Logger,
config internal.Config,
) (receiverhelper.MetricsScraper, error) {
) (scraperhelper.MetricsScraper, error) {
cfg := config.(*Config)
s, err := newFileSystemScraper(ctx, cfg)
if err != nil {
return nil, err
}

ms := receiverhelper.NewMetricsScraper(TypeStr, s.Scrape)
ms := scraperhelper.NewMetricsScraper(TypeStr, s.Scrape)

return ms, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/receiver/hostmetricsreceiver/internal"
"go.opentelemetry.io/collector/receiver/receiverhelper"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)

const (
Expand Down Expand Up @@ -91,7 +91,7 @@ func (s *scraper) Scrape(_ context.Context) (pdata.MetricSlice, error) {
appendSystemSpecificMetrics(metrics, 1, now, usages)
}

err = receiverhelper.CombineScrapeErrors(errors)
err = scraperhelper.CombineScrapeErrors(errors)
if err != nil && len(usages) == 0 {
partialErr := err.(consumererror.PartialScrapeError)
partialErr.Failed = metricsLen
Expand Down
Loading

0 comments on commit fdc8624

Please sign in to comment.