Skip to content

Commit

Permalink
[chore] Make hostmetrics receiver scrapers closer to core scrapers
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Jan 10, 2025
1 parent 765ddb7 commit 9ee37b6
Show file tree
Hide file tree
Showing 29 changed files with 71 additions and 117 deletions.
8 changes: 5 additions & 3 deletions receiver/hostmetricsreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// Config defines configuration for HostMetrics receiver.
type Config struct {
scraperhelper.ControllerConfig `mapstructure:",squash"`
Scrapers map[component.Type]internal.Config `mapstructure:"-"`
Scrapers map[component.Type]component.Config `mapstructure:"-"`
// RootPath is the host's root directory (linux only).
RootPath string `mapstructure:"root_path"`

Expand Down Expand Up @@ -58,7 +58,7 @@ func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {

// dynamically load the individual collector configs based on the key name

cfg.Scrapers = map[component.Type]internal.Config{}
cfg.Scrapers = map[component.Type]component.Config{}

scrapersSection, err := componentParser.Sub("scrapers")
if err != nil {
Expand All @@ -84,7 +84,9 @@ func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {
return fmt.Errorf("error reading settings for scraper type %q: %w", key, err)
}

scraperCfg.SetRootPath(cfg.RootPath)
if iCfg, ok := scraperCfg.(internal.Config); ok {
iCfg.SetRootPath(cfg.RootPath)
}

cfg.Scrapers[key] = scraperCfg
}
Expand Down
27 changes: 13 additions & 14 deletions receiver/hostmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"go.opentelemetry.io/collector/scraper/scraperhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/diskscraper"
Expand Down Expand Up @@ -43,8 +42,8 @@ func TestLoadConfig(t *testing.T) {
id: component.NewID(metadata.Type),
expected: func() component.Config {
cfg := createDefaultConfig().(*Config)
cfg.Scrapers = map[component.Type]internal.Config{
cpuscraper.Type: func() internal.Config {
cfg.Scrapers = map[component.Type]component.Config{
cpuscraper.Type: func() component.Config {
cfg := (&cpuscraper.Factory{}).CreateDefaultConfig()
return cfg
}(),
Expand All @@ -60,53 +59,53 @@ func TestLoadConfig(t *testing.T) {
CollectionInterval: 30 * time.Second,
InitialDelay: time.Second,
},
Scrapers: map[component.Type]internal.Config{
cpuscraper.Type: func() internal.Config {
Scrapers: map[component.Type]component.Config{
cpuscraper.Type: func() component.Config {
cfg := (&cpuscraper.Factory{}).CreateDefaultConfig()
return cfg
}(),
diskscraper.Type: func() internal.Config {
diskscraper.Type: func() component.Config {
cfg := (&diskscraper.Factory{}).CreateDefaultConfig()
return cfg
}(),
loadscraper.Type: (func() internal.Config {
loadscraper.Type: (func() component.Config {
cfg := (&loadscraper.Factory{}).CreateDefaultConfig()
cfg.(*loadscraper.Config).CPUAverage = true
return cfg
})(),
filesystemscraper.Type: func() internal.Config {
filesystemscraper.Type: func() component.Config {
cfg := (&filesystemscraper.Factory{}).CreateDefaultConfig()
return cfg
}(),
memoryscraper.Type: func() internal.Config {
memoryscraper.Type: func() component.Config {
cfg := (&memoryscraper.Factory{}).CreateDefaultConfig()
return cfg
}(),
networkscraper.Type: (func() internal.Config {
networkscraper.Type: (func() component.Config {
cfg := (&networkscraper.Factory{}).CreateDefaultConfig()
cfg.(*networkscraper.Config).Include = networkscraper.MatchConfig{
Interfaces: []string{"test1"},
Config: filterset.Config{MatchType: "strict"},
}
return cfg
})(),
processesscraper.Type: func() internal.Config {
processesscraper.Type: func() component.Config {
cfg := (&processesscraper.Factory{}).CreateDefaultConfig()
return cfg
}(),
pagingscraper.Type: func() internal.Config {
pagingscraper.Type: func() component.Config {
cfg := (&pagingscraper.Factory{}).CreateDefaultConfig()
return cfg
}(),
processscraper.Type: (func() internal.Config {
processscraper.Type: (func() component.Config {
cfg := (&processscraper.Factory{}).CreateDefaultConfig()
cfg.(*processscraper.Config).Include = processscraper.MatchConfig{
Names: []string{"test2", "test3"},
Config: filterset.Config{MatchType: "regexp"},
}
return cfg
})(),
systemscraper.Type: (func() internal.Config {
systemscraper.Type: (func() component.Config {
cfg := (&systemscraper.Factory{}).CreateDefaultConfig()
return cfg
})(),
Expand Down
2 changes: 1 addition & 1 deletion receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func createAddScraperOptions(
return scraperControllerOptions, nil
}

func createHostMetricsScraper(ctx context.Context, set receiver.Settings, key component.Type, cfg internal.Config, factories map[component.Type]internal.ScraperFactory) (s scraper.Metrics, ok bool, err error) {
func createHostMetricsScraper(ctx context.Context, set receiver.Settings, key component.Type, cfg component.Config, factories map[component.Type]internal.ScraperFactory) (s scraper.Metrics, ok bool, err error) {
factory := factories[key]
if factory == nil {
ok = false
Expand Down
4 changes: 1 addition & 3 deletions receiver/hostmetricsreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pipeline"
"go.opentelemetry.io/collector/receiver/receivertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
)

var creationSet = receivertest.NewNopSettings()
Expand Down Expand Up @@ -48,7 +46,7 @@ func TestCreateReceiver_ScraperKeyConfigError(t *testing.T) {
const errorKey string = "error"

factory := NewFactory()
cfg := &Config{Scrapers: map[component.Type]internal.Config{component.MustNewType(errorKey): &mockConfig{}}}
cfg := &Config{Scrapers: map[component.Type]component.Config{component.MustNewType(errorKey): &mockConfig{}}}

_, err := factory.CreateMetrics(context.Background(), creationSet, cfg, consumertest.NewNop())
assert.EqualError(t, err, fmt.Sprintf("host metrics scraper factory not found for key: %q", errorKey))
Expand Down
1 change: 0 additions & 1 deletion receiver/hostmetricsreceiver/hostmetrics_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func TestLoadConfigRootPath(t *testing.T) {
expectedConfig := factory.CreateDefaultConfig().(*Config)
expectedConfig.RootPath = "testdata"
cpuScraperCfg := (&cpuscraper.Factory{}).CreateDefaultConfig()
cpuScraperCfg.SetRootPath("testdata")
expectedConfig.Scrapers = map[component.Type]internal.Config{cpuscraper.Type: cpuScraperCfg}
assert.Equal(t, expectedConfig, cfg)
expectedEnvMap := common.EnvMap{
Expand Down
43 changes: 17 additions & 26 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestGatherMetrics_EndToEnd(t *testing.T) {
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: 100 * time.Millisecond,
},
Scrapers: map[component.Type]internal.Config{
Scrapers: map[component.Type]component.Config{
cpuscraper.Type: scraperFactories[cpuscraper.Type].CreateDefaultConfig(),
diskscraper.Type: scraperFactories[diskscraper.Type].CreateDefaultConfig(),
filesystemscraper.Type: (&filesystemscraper.Factory{}).CreateDefaultConfig(),
Expand Down Expand Up @@ -186,12 +186,10 @@ var mockType = component.MustNewType("mock")

type mockConfig struct{}

func (m *mockConfig) SetRootPath(_ string) {}

type errFactory struct{}

func (m *errFactory) CreateDefaultConfig() internal.Config { return &mockConfig{} }
func (m *errFactory) CreateMetricsScraper(context.Context, receiver.Settings, internal.Config) (scraper.Metrics, error) {
func (m *errFactory) CreateDefaultConfig() component.Config { return &mockConfig{} }
func (m *errFactory) CreateMetricsScraper(context.Context, receiver.Settings, component.Config) (scraper.Metrics, error) {
return nil, errors.New("err1")
}

Expand All @@ -202,7 +200,7 @@ func TestGatherMetrics_ScraperKeyConfigError(t *testing.T) {
scraperFactories = tmp
}()

cfg := &Config{Scrapers: map[component.Type]internal.Config{component.MustNewType("error"): &mockConfig{}}}
cfg := &Config{Scrapers: map[component.Type]component.Config{component.MustNewType("error"): &mockConfig{}}}
_, err := NewFactory().CreateMetrics(context.Background(), creationSet, cfg, consumertest.NewNop())
require.Error(t, err)
}
Expand All @@ -215,7 +213,7 @@ func TestGatherMetrics_CreateMetricsScraperError(t *testing.T) {
scraperFactories = tmp
}()

cfg := &Config{Scrapers: map[component.Type]internal.Config{mockType: &mockConfig{}}}
cfg := &Config{Scrapers: map[component.Type]component.Config{mockType: &mockConfig{}}}
_, err := NewFactory().CreateMetrics(context.Background(), creationSet, cfg, consumertest.NewNop())
require.Error(t, err)
}
Expand Down Expand Up @@ -267,7 +265,7 @@ func benchmarkScrapeMetrics(b *testing.B, cfg *Config) {
func Benchmark_ScrapeCpuMetrics(b *testing.B) {
cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{cpuscraper.Type: (&cpuscraper.Factory{}).CreateDefaultConfig()},
Scrapers: map[component.Type]component.Config{cpuscraper.Type: (&cpuscraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
Expand All @@ -276,7 +274,7 @@ func Benchmark_ScrapeCpuMetrics(b *testing.B) {
func Benchmark_ScrapeDiskMetrics(b *testing.B) {
cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{diskscraper.Type: (&diskscraper.Factory{}).CreateDefaultConfig()},
Scrapers: map[component.Type]component.Config{diskscraper.Type: (&diskscraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
Expand All @@ -285,7 +283,7 @@ func Benchmark_ScrapeDiskMetrics(b *testing.B) {
func Benchmark_ScrapeFileSystemMetrics(b *testing.B) {
cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{filesystemscraper.Type: (&filesystemscraper.Factory{}).CreateDefaultConfig()},
Scrapers: map[component.Type]component.Config{filesystemscraper.Type: (&filesystemscraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
Expand All @@ -294,7 +292,7 @@ func Benchmark_ScrapeFileSystemMetrics(b *testing.B) {
func Benchmark_ScrapeLoadMetrics(b *testing.B) {
cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{loadscraper.Type: (&loadscraper.Factory{}).CreateDefaultConfig()},
Scrapers: map[component.Type]component.Config{loadscraper.Type: (&loadscraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
Expand All @@ -303,7 +301,7 @@ func Benchmark_ScrapeLoadMetrics(b *testing.B) {
func Benchmark_ScrapeMemoryMetrics(b *testing.B) {
cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{memoryscraper.Type: (&memoryscraper.Factory{}).CreateDefaultConfig()},
Scrapers: map[component.Type]component.Config{memoryscraper.Type: (&memoryscraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
Expand All @@ -312,7 +310,7 @@ func Benchmark_ScrapeMemoryMetrics(b *testing.B) {
func Benchmark_ScrapeNetworkMetrics(b *testing.B) {
cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{networkscraper.Type: (&networkscraper.Factory{}).CreateDefaultConfig()},
Scrapers: map[component.Type]component.Config{networkscraper.Type: (&networkscraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
Expand All @@ -321,7 +319,7 @@ func Benchmark_ScrapeNetworkMetrics(b *testing.B) {
func Benchmark_ScrapeProcessesMetrics(b *testing.B) {
cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{processesscraper.Type: (&processesscraper.Factory{}).CreateDefaultConfig()},
Scrapers: map[component.Type]component.Config{processesscraper.Type: (&processesscraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
Expand All @@ -330,7 +328,7 @@ func Benchmark_ScrapeProcessesMetrics(b *testing.B) {
func Benchmark_ScrapePagingMetrics(b *testing.B) {
cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{pagingscraper.Type: (&pagingscraper.Factory{}).CreateDefaultConfig()},
Scrapers: map[component.Type]component.Config{pagingscraper.Type: (&pagingscraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
Expand All @@ -343,7 +341,7 @@ func Benchmark_ScrapeProcessMetrics(b *testing.B) {

cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{processscraper.Type: (&processscraper.Factory{}).CreateDefaultConfig()},
Scrapers: map[component.Type]component.Config{processscraper.Type: (&processscraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
Expand All @@ -356,7 +354,7 @@ func Benchmark_ScrapeUptimeMetrics(b *testing.B) {

cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{systemscraper.Type: (&systemscraper.Factory{}).CreateDefaultConfig()},
Scrapers: map[component.Type]component.Config{systemscraper.Type: (&systemscraper.Factory{}).CreateDefaultConfig()},
}

benchmarkScrapeMetrics(b, cfg)
Expand All @@ -365,7 +363,7 @@ func Benchmark_ScrapeUptimeMetrics(b *testing.B) {
func Benchmark_ScrapeSystemMetrics(b *testing.B) {
cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{
Scrapers: map[component.Type]component.Config{
cpuscraper.Type: (&cpuscraper.Factory{}).CreateDefaultConfig(),
diskscraper.Type: (&diskscraper.Factory{}).CreateDefaultConfig(),
filesystemscraper.Type: (&filesystemscraper.Factory{}).CreateDefaultConfig(),
Expand All @@ -387,16 +385,9 @@ func Benchmark_ScrapeSystemAndProcessMetrics(b *testing.B) {

cfg := &Config{
ControllerConfig: scraperhelper.NewDefaultControllerConfig(),
Scrapers: map[component.Type]internal.Config{
cpuscraper.Type: &cpuscraper.Config{},
diskscraper.Type: &diskscraper.Config{},
Scrapers: map[component.Type]component.Config{
filesystemscraper.Type: (&filesystemscraper.Factory{}).CreateDefaultConfig(),
loadscraper.Type: &loadscraper.Config{},
memoryscraper.Type: &memoryscraper.Config{},
networkscraper.Type: &networkscraper.Config{},
pagingscraper.Type: (&pagingscraper.Factory{}).CreateDefaultConfig(),
processesscraper.Type: &processesscraper.Config{},
systemscraper.Type: &systemscraper.Config{},
},
}

Expand Down
11 changes: 4 additions & 7 deletions receiver/hostmetricsreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/scraperinttest"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterset"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/processscraper"
)

Expand All @@ -41,7 +40,7 @@ func Test_ProcessScrape(t *testing.T) {
Config: filterset.Config{MatchType: filterset.Regexp},
Names: []string{"sleep"},
}
rCfg.Scrapers = map[component.Type]internal.Config{
rCfg.Scrapers = map[component.Type]component.Config{
processscraper.Type: pCfg,
}
}),
Expand Down Expand Up @@ -71,8 +70,7 @@ func Test_ProcessScrapeWithCustomRootPath(t *testing.T) {
rCfg.CollectionInterval = time.Second
rCfg.RootPath = rootPath
pCfg := (&processscraper.Factory{}).CreateDefaultConfig().(*processscraper.Config)
pCfg.SetRootPath(rootPath)
rCfg.Scrapers = map[component.Type]internal.Config{
rCfg.Scrapers = map[component.Type]component.Config{
processscraper.Type: pCfg,
}
}),
Expand All @@ -99,12 +97,11 @@ func Test_ProcessScrapeWithBadRootPathAndEnvVar(t *testing.T) {
func(_ *testing.T, cfg component.Config, _ *scraperinttest.ContainerInfo) {
rCfg := cfg.(*Config)
rCfg.CollectionInterval = time.Second
rCfg.RootPath = badRootPath
pCfg := (&processscraper.Factory{}).CreateDefaultConfig().(*processscraper.Config)
pCfg.SetRootPath(badRootPath)
rCfg.Scrapers = map[component.Type]internal.Config{
rCfg.Scrapers = map[component.Type]component.Config{
processscraper.Type: pCfg,
}
rCfg.RootPath = badRootPath
}),
scraperinttest.WithExpectedFile(expectedFile),
scraperinttest.WithCompareOptions(
Expand Down
12 changes: 2 additions & 10 deletions receiver/hostmetricsreceiver/internal/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@ import (
// ScraperFactory can create a MetricScraper.
type ScraperFactory interface {
// CreateDefaultConfig creates the default configuration for the Scraper.
CreateDefaultConfig() Config
CreateDefaultConfig() component.Config

// CreateMetricsScraper creates a scraper based on this config.
// If the config is not valid, error will be returned instead.
CreateMetricsScraper(ctx context.Context, settings receiver.Settings, cfg Config) (scraper.Metrics, error)
CreateMetricsScraper(ctx context.Context, settings receiver.Settings, cfg component.Config) (scraper.Metrics, error)
}

// Config is the configuration of a scraper.
type Config interface {
SetRootPath(rootPath string)
}

type ScraperConfig struct {
RootPath string `mapstructure:"-"`
}

func (p *ScraperConfig) SetRootPath(rootPath string) {
p.RootPath = rootPath
}

type EnvVarScraper struct {
delegate scraper.Metrics
envMap common.EnvMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
package cpuscraper // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper"

import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper/internal/metadata"
)

// Config relating to CPU Metric Scraper.
type Config struct {
metadata.MetricsBuilderConfig `mapstructure:",squash"`
internal.ScraperConfig
}
Loading

0 comments on commit 9ee37b6

Please sign in to comment.