-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[receiver/hostmetrics] Add an optional root_path configuration (#16026)
- Make the receiver configurable to be aware of the root filesystem, for Linux only. - Give the ability to the filesystem scraper to translate mountpoints between host and container mountpoint attribute emitted without the root path prefix, i.e. from the host's perspective - No longer requires specification of where to find the mountinfo file through env var in order to avoid errors. - All scrapers can see RootPath - useful for other scrapers e.g. pagingscraper as discussed in Ability to scrape filesystem host metrics from a container - Set the environment variables for the user if they are not set. - Validate that the root_path aligns with the gopsutil env vars (if they are previously set).
- Loading branch information
1 parent
9f68e4e
commit eee0daf
Showing
24 changed files
with
405 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
change_type: enhancement | ||
component: hostmetricsreceiver | ||
note: "Added `root_path` config option, allowing the user to specify where the host filesystem is." | ||
issues: [5879, 16026] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux | ||
|
||
package hostmetricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver" | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
var gopsutilEnvVars = map[string]string{ | ||
"HOST_PROC": "/proc", | ||
"HOST_SYS": "/sys", | ||
"HOST_ETC": "/etc", | ||
"HOST_VAR": "/var", | ||
"HOST_RUN": "/run", | ||
"HOST_DEV": "/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, env environment) error { | ||
if rootPath == "" || rootPath == "/" { | ||
return nil | ||
} | ||
|
||
if globalRootPath != "" && rootPath != globalRootPath { | ||
return fmt.Errorf("inconsistent root_path configuration detected between hostmetricsreceivers: `%s` != `%s`", globalRootPath, rootPath) | ||
} | ||
globalRootPath = rootPath | ||
|
||
if _, err := os.Stat(rootPath); err != nil { | ||
return fmt.Errorf("invalid root_path: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func setGoPsutilEnvVars(rootPath string, env environment) error { | ||
if rootPath == "" || rootPath == "/" { | ||
return nil | ||
} | ||
|
||
for envVarKey, defaultValue := range gopsutilEnvVars { | ||
_, ok := env.Lookup(envVarKey) | ||
if ok { | ||
continue // don't override if existing env var is set | ||
} | ||
if err := env.Set(envVarKey, filepath.Join(rootPath, defaultValue)); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux | ||
|
||
package hostmetricsreceiver | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/service/servicetest" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/cpuscraper" | ||
) | ||
|
||
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)) | ||
} | ||
|
||
func TestInconsistentRootPaths(t *testing.T) { | ||
globalRootPath = "foo" | ||
err := testValidate("testdata", &testEnv{}) | ||
assert.EqualError(t, err, "inconsistent root_path configuration detected between hostmetricsreceivers: `foo` != `testdata`") | ||
} | ||
|
||
func TestLoadConfigRootPath(t *testing.T) { | ||
t.Setenv("HOST_PROC", "testdata") | ||
factories, _ := componenttest.NopFactories() | ||
factory := NewFactory() | ||
factories.Receivers[typeStr] = factory | ||
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config-root-path.yaml"), factories) | ||
require.NoError(t, err) | ||
globalRootPath = "" | ||
|
||
r := cfg.Receivers[config.NewComponentID(typeStr)].(*Config) | ||
expectedConfig := factory.CreateDefaultConfig().(*Config) | ||
expectedConfig.RootPath = "testdata" | ||
cpuScraperCfg := (&cpuscraper.Factory{}).CreateDefaultConfig() | ||
cpuScraperCfg.SetRootPath("testdata") | ||
expectedConfig.Scrapers = map[string]internal.Config{cpuscraper.TypeStr: cpuScraperCfg} | ||
|
||
assert.Equal(t, expectedConfig, r) | ||
} | ||
|
||
func TestLoadInvalidConfig_RootPathNotExist(t *testing.T) { | ||
factories, _ := componenttest.NopFactories() | ||
factory := NewFactory() | ||
factories.Receivers[typeStr] = factory | ||
_, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config-bad-root-path.yaml"), factories) | ||
assert.ErrorContains(t, err, "invalid root_path:") | ||
globalRootPath = "" | ||
} | ||
|
||
func testValidate(rootPath string, env environment) error { | ||
err := validateRootPath(rootPath, env) | ||
globalRootPath = "" | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build !linux | ||
|
||
package hostmetricsreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver" | ||
|
||
import "fmt" | ||
|
||
func validateRootPath(rootPath string, _ environment) error { | ||
if rootPath == "" { | ||
return nil | ||
} | ||
return fmt.Errorf("root_path is supported on linux only") | ||
} | ||
|
||
func setGoPsutilEnvVars(_ string, _ environment) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build !linux | ||
|
||
package hostmetricsreceiver | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRootPathNotAllowedOnOS(t *testing.T) { | ||
assert.NotNil(t, validateRootPath("testdata", &testEnv{})) | ||
} | ||
|
||
func TestRootPathUnset(t *testing.T) { | ||
assert.Nil(t, validateRootPath("", &testEnv{})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.