Skip to content

Commit

Permalink
refactor envMap to be part of config
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Jul 24, 2023
1 parent 0a21b21 commit fc40625
Show file tree
Hide file tree
Showing 47 changed files with 136 additions and 149 deletions.
2 changes: 2 additions & 0 deletions receiver/hostmetricsreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {
}

collectorCfg.SetRootPath(cfg.RootPath)
envMap := setGoPsutilEnvVars(cfg.RootPath, &osEnv{})
collectorCfg.SetEnvMap(envMap)

cfg.Scrapers[key] = collectorCfg
}
Expand Down
46 changes: 39 additions & 7 deletions receiver/hostmetricsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package hostmetricsreceiver

import (
"github.com/shirou/gopsutil/v3/common"
"path/filepath"
"testing"
"time"
Expand Down Expand Up @@ -44,7 +45,11 @@ func TestLoadConfig(t *testing.T) {
r0 := cfg.Receivers[component.NewID(metadata.Type)]
defaultConfigCPUScraper := factory.CreateDefaultConfig()
defaultConfigCPUScraper.(*Config).Scrapers = map[string]internal.Config{
cpuscraper.TypeStr: (&cpuscraper.Factory{}).CreateDefaultConfig(),
cpuscraper.TypeStr: func() internal.Config {
cfg := (&cpuscraper.Factory{}).CreateDefaultConfig()
cfg.SetEnvMap(common.EnvMap{})
return cfg
}(),
}

assert.Equal(t, defaultConfigCPUScraper, r0)
Expand All @@ -56,31 +61,58 @@ func TestLoadConfig(t *testing.T) {
InitialDelay: time.Second,
},
Scrapers: map[string]internal.Config{
cpuscraper.TypeStr: (&cpuscraper.Factory{}).CreateDefaultConfig(),
diskscraper.TypeStr: (&diskscraper.Factory{}).CreateDefaultConfig(),
cpuscraper.TypeStr: func() internal.Config {
cfg := (&cpuscraper.Factory{}).CreateDefaultConfig()
cfg.SetEnvMap(common.EnvMap{})
return cfg
}(),
diskscraper.TypeStr: func() internal.Config {
cfg := (&diskscraper.Factory{}).CreateDefaultConfig()
cfg.SetEnvMap(common.EnvMap{})
return cfg
}(),
loadscraper.TypeStr: (func() internal.Config {
cfg := (&loadscraper.Factory{}).CreateDefaultConfig()
cfg.(*loadscraper.Config).CPUAverage = true
cfg.SetEnvMap(common.EnvMap{})
return cfg
})(),
filesystemscraper.TypeStr: (&filesystemscraper.Factory{}).CreateDefaultConfig(),
memoryscraper.TypeStr: (&memoryscraper.Factory{}).CreateDefaultConfig(),
filesystemscraper.TypeStr: func() internal.Config {
cfg := (&filesystemscraper.Factory{}).CreateDefaultConfig()
cfg.SetEnvMap(common.EnvMap{})
return cfg
}(),
memoryscraper.TypeStr: func() internal.Config {
cfg := (&memoryscraper.Factory{}).CreateDefaultConfig()
cfg.SetEnvMap(common.EnvMap{})
return cfg
}(),
networkscraper.TypeStr: (func() internal.Config {
cfg := (&networkscraper.Factory{}).CreateDefaultConfig()
cfg.(*networkscraper.Config).Include = networkscraper.MatchConfig{
Interfaces: []string{"test1"},
Config: filterset.Config{MatchType: "strict"},
}
cfg.SetEnvMap(common.EnvMap{})
return cfg
})(),
processesscraper.TypeStr: (&processesscraper.Factory{}).CreateDefaultConfig(),
pagingscraper.TypeStr: (&pagingscraper.Factory{}).CreateDefaultConfig(),
processesscraper.TypeStr: func() internal.Config {
cfg := (&processesscraper.Factory{}).CreateDefaultConfig()
cfg.SetEnvMap(common.EnvMap{})
return cfg
}(),
pagingscraper.TypeStr: func() internal.Config {
cfg := (&pagingscraper.Factory{}).CreateDefaultConfig()
cfg.SetEnvMap(common.EnvMap{})
return cfg
}(),
processscraper.TypeStr: (func() internal.Config {
cfg := (&processscraper.Factory{}).CreateDefaultConfig()
cfg.(*processscraper.Config).Include = processscraper.MatchConfig{
Names: []string{"test2", "test3"},
Config: filterset.Config{MatchType: "regexp"},
}
cfg.SetEnvMap(common.EnvMap{})
return cfg
})(),
},
Expand Down
11 changes: 4 additions & 7 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"os"

"github.com/shirou/gopsutil/v3/common"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"
Expand Down Expand Up @@ -71,9 +70,8 @@ func createMetricsReceiver(
consumer consumer.Metrics,
) (receiver.Metrics, error) {
oCfg := cfg.(*Config)
envMap := setGoPsutilEnvVars(oCfg.RootPath, &osEnv{})

addScraperOptions, err := createAddScraperOptions(ctx, set, oCfg, scraperFactories, envMap)
addScraperOptions, err := createAddScraperOptions(ctx, set, oCfg, scraperFactories)
if err != nil {
return nil, err
}
Expand All @@ -91,12 +89,11 @@ func createAddScraperOptions(
set receiver.CreateSettings,
config *Config,
factories map[string]internal.ScraperFactory,
envMap common.EnvMap,
) ([]scraperhelper.ScraperControllerOption, error) {
scraperControllerOptions := make([]scraperhelper.ScraperControllerOption, 0, len(config.Scrapers))

for key, cfg := range config.Scrapers {
hostMetricsScraper, ok, err := createHostMetricsScraper(ctx, set, key, cfg, factories, envMap)
hostMetricsScraper, ok, err := createHostMetricsScraper(ctx, set, key, cfg, factories)
if err != nil {
return nil, fmt.Errorf("failed to create scraper for key %q: %w", key, err)
}
Expand All @@ -112,15 +109,15 @@ func createAddScraperOptions(
return scraperControllerOptions, nil
}

func createHostMetricsScraper(ctx context.Context, set receiver.CreateSettings, key string, cfg internal.Config, factories map[string]internal.ScraperFactory, envMap common.EnvMap) (scraper scraperhelper.Scraper, ok bool, err error) {
func createHostMetricsScraper(ctx context.Context, set receiver.CreateSettings, 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
return
}

ok = true
scraper, err = factory.CreateMetricsScraper(ctx, set, cfg, envMap)
scraper, err = factory.CreateMetricsScraper(ctx, set, cfg)
return
}

Expand Down
6 changes: 4 additions & 2 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,13 @@ type mockConfig struct{}

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

func (m *mockConfig) SetEnvMap(_ common.EnvMap) {}

type mockFactory struct{ mock.Mock }
type mockScraper struct{ mock.Mock }

func (m *mockFactory) CreateDefaultConfig() internal.Config { return &mockConfig{} }
func (m *mockFactory) CreateMetricsScraper(context.Context, receiver.CreateSettings, internal.Config, common.EnvMap) (scraperhelper.Scraper, error) {
func (m *mockFactory) CreateMetricsScraper(context.Context, receiver.CreateSettings, internal.Config) (scraperhelper.Scraper, error) {
args := m.MethodCalled("CreateMetricsScraper")
return args.Get(0).(scraperhelper.Scraper), args.Error(1)
}
Expand Down Expand Up @@ -278,7 +280,7 @@ func benchmarkScrapeMetrics(b *testing.B, cfg *Config) {
sink := &notifyingSink{ch: make(chan int, 10)}
tickerCh := make(chan time.Time)

options, err := createAddScraperOptions(context.Background(), receivertest.NewNopCreateSettings(), cfg, scraperFactories, common.EnvMap{})
options, err := createAddScraperOptions(context.Background(), receivertest.NewNopCreateSettings(), cfg, scraperFactories)
require.NoError(b, err)
options = append(options, scraperhelper.WithTickerChannel(tickerCh))

Expand Down
8 changes: 7 additions & 1 deletion receiver/hostmetricsreceiver/internal/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@ 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, settings receiver.CreateSettings, cfg Config, envMap common.EnvMap) (scraperhelper.Scraper, error)
CreateMetricsScraper(ctx context.Context, settings receiver.CreateSettings, cfg Config) (scraperhelper.Scraper, error)
}

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

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

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

func (p *ScraperConfig) SetEnvMap(envMap common.EnvMap) {
p.EnvMap = envMap
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ type scraper struct {
bootTime func(context.Context) (uint64, error)
times func(context.Context, bool) ([]cpu.TimesStat, error)
now func() time.Time
envMap common.EnvMap
}

// newCPUScraper creates a set of CPU related metrics
func newCPUScraper(_ context.Context, settings receiver.CreateSettings, cfg *Config, envMap common.EnvMap) *scraper {
return &scraper{settings: settings, config: cfg, bootTime: host.BootTimeWithContext, times: cpu.TimesWithContext, ucal: &ucal.CPUUtilizationCalculator{}, now: time.Now, envMap: envMap}
func newCPUScraper(_ context.Context, settings receiver.CreateSettings, cfg *Config) *scraper {
return &scraper{settings: settings, config: cfg, bootTime: host.BootTimeWithContext, times: cpu.TimesWithContext, ucal: &ucal.CPUUtilizationCalculator{}, now: time.Now}
}

func (s *scraper) start(ctx context.Context, _ component.Host) error {
ctx = context.WithValue(ctx, common.EnvKey, s.envMap)
ctx = context.WithValue(ctx, common.EnvKey, s.config.EnvMap)
bootTime, err := s.bootTime(ctx)
if err != nil {
return err
Expand All @@ -52,7 +51,7 @@ func (s *scraper) start(ctx context.Context, _ component.Host) error {
}

func (s *scraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
ctx = context.WithValue(ctx, common.EnvKey, s.envMap)
ctx = context.WithValue(ctx, common.EnvKey, s.config.EnvMap)
now := pcommon.NewTimestampFromTime(s.now())
cpuTimes, err := s.times(ctx, true /*percpu=*/)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"testing"
"time"

"github.com/shirou/gopsutil/v3/common"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -75,7 +74,7 @@ func TestScrape(t *testing.T) {

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
scraper := newCPUScraper(context.Background(), receivertest.NewNopCreateSettings(), &Config{MetricsBuilderConfig: test.metricsConfig}, common.EnvMap{})
scraper := newCPUScraper(context.Background(), receivertest.NewNopCreateSettings(), &Config{MetricsBuilderConfig: test.metricsConfig})
if test.bootTimeFunc != nil {
scraper.bootTime = test.bootTimeFunc
}
Expand Down Expand Up @@ -174,7 +173,7 @@ func TestScrape_CpuUtilization(t *testing.T) {
settings.Metrics.SystemCPUUtilization.Enabled = test.utilization
}

scraper := newCPUScraper(context.Background(), receivertest.NewNopCreateSettings(), &Config{MetricsBuilderConfig: settings}, common.EnvMap{})
scraper := newCPUScraper(context.Background(), receivertest.NewNopCreateSettings(), &Config{MetricsBuilderConfig: settings})
err := scraper.start(context.Background(), componenttest.NewNopHost())
require.NoError(t, err, "Failed to initialize cpu scraper: %v", err)

Expand Down Expand Up @@ -211,7 +210,7 @@ func TestScrape_CpuUtilization(t *testing.T) {

// Error in calculation should be returned as PartialScrapeError
func TestScrape_CpuUtilizationError(t *testing.T) {
scraper := newCPUScraper(context.Background(), receivertest.NewNopCreateSettings(), &Config{MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig()}, common.EnvMap{})
scraper := newCPUScraper(context.Background(), receivertest.NewNopCreateSettings(), &Config{MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig()})
// mock times function to force an error in next scrape
scraper.times = func(context.Context, bool) ([]cpu.TimesStat, error) {
return []cpu.TimesStat{{CPU: "1", System: 1, User: 2}}, nil
Expand Down Expand Up @@ -279,7 +278,7 @@ func TestScrape_CpuUtilizationStandard(t *testing.T) {
},
}

cpuScraper := newCPUScraper(context.Background(), receivertest.NewNopCreateSettings(), &Config{MetricsBuilderConfig: overriddenMetricsSettings}, common.EnvMap{})
cpuScraper := newCPUScraper(context.Background(), receivertest.NewNopCreateSettings(), &Config{MetricsBuilderConfig: overriddenMetricsSettings})
for _, scrapeData := range scrapesData {
// mock TimeStats and Now
cpuScraper.times = func(context.Context, bool) ([]cpu.TimesStat, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cpuscraper // import "github.com/open-telemetry/opentelemetry-collector-
import (
"context"

"github.com/shirou/gopsutil/v3/common"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"

Expand Down Expand Up @@ -36,10 +35,9 @@ func (f *Factory) CreateMetricsScraper(
ctx context.Context,
settings receiver.CreateSettings,
config internal.Config,
envMap common.EnvMap,
) (scraperhelper.Scraper, error) {
cfg := config.(*Config)
s := newCPUScraper(ctx, settings, cfg, envMap)
s := newCPUScraper(ctx, settings, cfg)

return scraperhelper.NewScraper(
TypeStr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"testing"

"github.com/shirou/gopsutil/v3/common"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/receiver/receivertest"
)
Expand All @@ -22,7 +21,7 @@ func TestCreateMetricsScraper(t *testing.T) {
factory := &Factory{}
cfg := &Config{}

scraper, err := factory.CreateMetricsScraper(context.Background(), receivertest.NewNopCreateSettings(), cfg, common.EnvMap{})
scraper, err := factory.CreateMetricsScraper(context.Background(), receivertest.NewNopCreateSettings(), cfg)

assert.NoError(t, err)
assert.NotNil(t, scraper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ type scraper struct {
mb *metadata.MetricsBuilder
includeFS filterset.FilterSet
excludeFS filterset.FilterSet
envMap common.EnvMap

// for mocking
bootTime func(context.Context) (uint64, error)
ioCounters func(ctx context.Context, names ...string) (map[string]disk.IOCountersStat, error)
}

// newDiskScraper creates a Disk Scraper
func newDiskScraper(_ context.Context, settings receiver.CreateSettings, cfg *Config, envMap common.EnvMap) (*scraper, error) {
scraper := &scraper{settings: settings, config: cfg, bootTime: host.BootTimeWithContext, ioCounters: disk.IOCountersWithContext, envMap: envMap}
func newDiskScraper(_ context.Context, settings receiver.CreateSettings, cfg *Config) (*scraper, error) {
scraper := &scraper{settings: settings, config: cfg, bootTime: host.BootTimeWithContext, ioCounters: disk.IOCountersWithContext}

var err error

Expand All @@ -68,7 +67,7 @@ func newDiskScraper(_ context.Context, settings receiver.CreateSettings, cfg *Co
}

func (s *scraper) start(ctx context.Context, _ component.Host) error {
ctx = context.WithValue(ctx, common.EnvKey, s.envMap)
ctx = context.WithValue(ctx, common.EnvKey, s.config.EnvMap)
bootTime, err := s.bootTime(ctx)
if err != nil {
return err
Expand All @@ -80,7 +79,7 @@ func (s *scraper) start(ctx context.Context, _ component.Host) error {
}

func (s *scraper) scrape(ctx context.Context) (pmetric.Metrics, error) {
ctx = context.WithValue(ctx, common.EnvKey, s.envMap)
ctx = context.WithValue(ctx, common.EnvKey, s.config.EnvMap)

now := pcommon.NewTimestampFromTime(time.Now())
ioCounters, err := s.ioCounters(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"errors"
"testing"

"github.com/shirou/gopsutil/v3/common"
"github.com/shirou/gopsutil/v3/disk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -39,7 +38,7 @@ func TestScrape_Others(t *testing.T) {

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
scraper, err := newDiskScraper(context.Background(), receivertest.NewNopCreateSettings(), &Config{}, common.EnvMap{})
scraper, err := newDiskScraper(context.Background(), receivertest.NewNopCreateSettings(), &Config{})
require.NoError(t, err, "Failed to create disk scraper: %v", err)

if test.ioCountersFunc != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"testing"

"github.com/shirou/gopsutil/v3/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
Expand Down Expand Up @@ -90,7 +89,7 @@ func TestScrape(t *testing.T) {

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
scraper, err := newDiskScraper(context.Background(), receivertest.NewNopCreateSettings(), &test.config, common.EnvMap{})
scraper, err := newDiskScraper(context.Background(), receivertest.NewNopCreateSettings(), &test.config)
if test.mutateScraper != nil {
test.mutateScraper(scraper)
}
Expand Down
Loading

0 comments on commit fc40625

Please sign in to comment.