-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathhostmetrics_linux.go
60 lines (49 loc) · 1.54 KB
/
hostmetrics_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//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, _ 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
}