Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the need to set environment variables with hostmetricsreceiver #23861

Merged
merged 8 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .chloggen/hostmetricsreceiver-env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: hostmetricsreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Remove the need to set environment variables with hostmetricsreceiver

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [23861]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
4 changes: 3 additions & 1 deletion receiver/hostmetricsreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (cfg *Config) Validate() error {
if len(cfg.Scrapers) == 0 {
err = multierr.Append(err, errors.New("must specify at least one scraper when using hostmetrics receiver"))
}
err = multierr.Append(err, validateRootPath(cfg.RootPath, &osEnv{}))
err = multierr.Append(err, validateRootPath(cfg.RootPath))
return err
}

Expand Down 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 @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/shirou/gopsutil/v3/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
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
4 changes: 0 additions & 4 deletions receiver/hostmetricsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ func createMetricsReceiver(
return nil, err
}

if err = setGoPsutilEnvVars(oCfg.RootPath, &osEnv{}); err != nil {
return nil, err
}

return scraperhelper.NewScraperControllerReceiver(
&oCfg.ScraperControllerSettings,
set,
Expand Down
31 changes: 16 additions & 15 deletions receiver/hostmetricsreceiver/hostmetrics_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@ import (
"fmt"
"os"
"path/filepath"

"github.com/shirou/gopsutil/v3/common"
)

var gopsutilEnvVars = map[string]string{
"HOST_PROC": "/proc",
"HOST_SYS": "/sys",
"HOST_ETC": "/etc",
"HOST_VAR": "/var",
"HOST_RUN": "/run",
"HOST_DEV": "/dev",
var gopsutilEnvVars = map[common.EnvKeyType]string{
common.HostProcEnvKey: "/proc",
common.HostSysEnvKey: "/sys",
common.HostEtcEnvKey: "/etc",
common.HostVarEnvKey: "/var",
common.HostRunEnvKey: "/run",
common.HostDevEnvKey: "/dev",
}

// This exists to validate that different instances of the hostmetricsreceiver do not
// have inconsistent root_path configurations. The root_path is passed down to gopsutil
// through env vars, so it must be consistent across the process.
var globalRootPath string

func validateRootPath(rootPath string, _ environment) error {
func validateRootPath(rootPath string) error {
if rootPath == "" || rootPath == "/" {
return nil
}
Expand All @@ -42,19 +44,18 @@ func validateRootPath(rootPath string, _ environment) error {
return nil
}

func setGoPsutilEnvVars(rootPath string, env environment) error {
func setGoPsutilEnvVars(rootPath string, env environment) common.EnvMap {
m := common.EnvMap{}
if rootPath == "" || rootPath == "/" {
return nil
return m
}

for envVarKey, defaultValue := range gopsutilEnvVars {
_, ok := env.Lookup(envVarKey)
_, ok := env.Lookup(string(envVarKey))
if ok {
continue // don't override if existing env var is set
}
if err := env.Set(envVarKey, filepath.Join(rootPath, defaultValue)); err != nil {
atoulme marked this conversation as resolved.
Show resolved Hide resolved
return err
}
m[envVarKey] = filepath.Join(rootPath, defaultValue)
}
return nil
return m
}
21 changes: 14 additions & 7 deletions receiver/hostmetricsreceiver/hostmetrics_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"testing"

"github.com/shirou/gopsutil/v3/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
Expand All @@ -20,16 +21,15 @@ import (
)

func TestConsistentRootPaths(t *testing.T) {
env := &testEnv{env: map[string]string{"HOST_PROC": "testdata"}}
// use testdata because it's a directory that exists - don't actually use any files in it
assert.Nil(t, testValidate("testdata", env))
assert.Nil(t, testValidate("", env))
assert.Nil(t, testValidate("/", env))
assert.Nil(t, testValidate("testdata"))
assert.Nil(t, testValidate(""))
assert.Nil(t, testValidate("/"))
}

func TestInconsistentRootPaths(t *testing.T) {
globalRootPath = "foo"
err := testValidate("testdata", &testEnv{})
err := testValidate("testdata")
assert.EqualError(t, err, "inconsistent root_path configuration detected between hostmetricsreceivers: `foo` != `testdata`")
}

Expand All @@ -47,6 +47,13 @@ func TestLoadConfigRootPath(t *testing.T) {
expectedConfig.RootPath = "testdata"
cpuScraperCfg := (&cpuscraper.Factory{}).CreateDefaultConfig()
cpuScraperCfg.SetRootPath("testdata")
cpuScraperCfg.SetEnvMap(common.EnvMap{
common.HostDevEnvKey: "testdata/dev",
common.HostEtcEnvKey: "testdata/etc",
common.HostRunEnvKey: "testdata/run",
common.HostSysEnvKey: "testdata/sys",
common.HostVarEnvKey: "testdata/var",
})
expectedConfig.Scrapers = map[string]internal.Config{cpuscraper.TypeStr: cpuScraperCfg}

assert.Equal(t, expectedConfig, r)
Expand All @@ -61,8 +68,8 @@ func TestLoadInvalidConfig_RootPathNotExist(t *testing.T) {
globalRootPath = ""
}

func testValidate(rootPath string, env environment) error {
err := validateRootPath(rootPath, env)
func testValidate(rootPath string) error {
err := validateRootPath(rootPath)
globalRootPath = ""
return err
}
12 changes: 8 additions & 4 deletions receiver/hostmetricsreceiver/hostmetrics_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@

package hostmetricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver"

import "fmt"
import (
"fmt"

func validateRootPath(rootPath string, _ environment) error {
"github.com/shirou/gopsutil/v3/common"
)

func validateRootPath(rootPath string) error {
if rootPath == "" {
return nil
}
return fmt.Errorf("root_path is supported on linux only")
}

func setGoPsutilEnvVars(_ string, _ environment) error {
return nil
func setGoPsutilEnvVars(_ string, _ environment) common.EnvMap {
return common.EnvMap{}
}
4 changes: 2 additions & 2 deletions receiver/hostmetricsreceiver/hostmetrics_others_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
)

func TestRootPathNotAllowedOnOS(t *testing.T) {
assert.NotNil(t, validateRootPath("testdata", &testEnv{}))
assert.NotNil(t, validateRootPath("testdata"))
}

func TestRootPathUnset(t *testing.T) {
assert.Nil(t, validateRootPath("", &testEnv{}))
assert.Nil(t, validateRootPath(""))
}
3 changes: 3 additions & 0 deletions receiver/hostmetricsreceiver/hostmetrics_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/shirou/gopsutil/v3/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -215,6 +216,8 @@ type mockConfig struct{}

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewMockPerfCounterScraperError(scrapeErr, getObjectErr, getValuesErr, initE
}

// start is a no-op
func (p *MockPerfCounterScraperError) Initialize(objects ...string) error {
func (p *MockPerfCounterScraperError) Initialize(_ ...string) error {
if p.initError != nil {
return p.initError
}
Expand All @@ -52,7 +52,7 @@ type mockPerfDataCollectionError struct {

// GetObject returns the specified getObjectErr or an object that will return a subsequent
// error if getObjectErr is nil
func (p mockPerfDataCollectionError) GetObject(objectName string) (PerfDataObject, error) {
func (p mockPerfDataCollectionError) GetObject(_ string) (PerfDataObject, error) {
if p.getObjectErr != nil {
return nil, p.getObjectErr
}
Expand All @@ -65,11 +65,11 @@ type mockPerfDataObjectError struct {
}

// Filter is a no-op
func (obj mockPerfDataObjectError) Filter(includeFS, excludeFS filterset.FilterSet, includeTotal bool) {
func (obj mockPerfDataObjectError) Filter(_, _ filterset.FilterSet, _ bool) {
}

// GetValues returns the specified getValuesErr
func (obj mockPerfDataObjectError) GetValues(counterNames ...string) ([]*CounterValues, error) {
func (obj mockPerfDataObjectError) GetValues(_ ...string) ([]*CounterValues, error) {
return nil, obj.getValuesErr
}

Expand Down Expand Up @@ -101,7 +101,7 @@ func NewMockPerfCounterScraper(objectsAndValuesToReturn map[string]map[string][]
}

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

Expand Down Expand Up @@ -144,7 +144,7 @@ type mockPerfDataObject struct {
}

// Filter is a no-op
func (obj mockPerfDataObject) Filter(includeFS, excludeFS filterset.FilterSet, includeTotal bool) {
func (obj mockPerfDataObject) Filter(_, _ filterset.FilterSet, _ bool) {
}

// GetValues returns the specified counter values
Expand Down
9 changes: 8 additions & 1 deletion receiver/hostmetricsreceiver/internal/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package internal // import "github.com/open-telemetry/opentelemetry-collector-co
import (
"context"

"github.com/shirou/gopsutil/v3/common"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/receiver/scraperhelper"
)
Expand All @@ -23,12 +24,18 @@ type ScraperFactory interface {
// Config is the configuration of a scraper.
type Config interface {
SetRootPath(rootPath string)
SetEnvMap(envMap common.EnvMap)
}

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

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

func (p *ScraperConfig) SetEnvMap(envMap common.EnvMap) {
p.EnvMap = envMap
}
Loading