Fix more stuff

pull/2813/head
Marc Tuduri 2023-09-27 10:55:06 +02:00
parent 96f13a2e27
commit 26fc637aed
No known key found for this signature in database
GPG Key ID: 761973D5AE312AF4
11 changed files with 91 additions and 60 deletions

View File

@ -32,8 +32,6 @@ const (
var (
diskLabelNames = []string{"device"}
diskstatsDeviceExcludeSet bool
readsCompletedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, diskSubsystem, "reads_completed_total"),
"The total number of reads completed successfully.",
@ -81,13 +79,14 @@ var (
type DiskstatsDeviceFilterConfig struct {
DiskstatsDeviceExclude *string
DiskstatsDeviceExcludeSet bool
OldDiskstatsDeviceExclude *string
DiskstatsDeviceInclude *string
}
func newDiskstatsDeviceFilter(config DiskstatsDeviceFilterConfig, logger log.Logger) (deviceFilter, error) {
if *config.OldDiskstatsDeviceExclude != "" {
if !diskstatsDeviceExcludeSet {
if !config.DiskstatsDeviceExcludeSet {
level.Warn(logger).Log("msg", "--collector.diskstats.ignored-devices is DEPRECATED and will be removed in 2.0.0, use --collector.diskstats.device-exclude")
*config.DiskstatsDeviceExclude = *config.OldDiskstatsDeviceExclude
} else {

View File

@ -47,10 +47,25 @@ func NewTestDiskStatsCollector(config NodeCollectorConfig, logger log.Logger) (p
}
func TestDiskStats(t *testing.T) {
*sysPath = "fixtures/sys"
*procPath = "fixtures/proc"
*udevDataPath = "fixtures/udev/data"
*diskstatsDeviceExclude = "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$"
empty := ""
config := NodeCollectorConfig{
DiskstatsDeviceFilter: DiskstatsDeviceFilterConfig{
DiskstatsDeviceExclude: &empty,
DiskstatsDeviceInclude: &empty,
OldDiskstatsDeviceExclude: &empty,
},
}
sysPath := "fixtures/sys"
config.Path.SysPath = &sysPath
procPath := "fixtures/proc"
config.Path.ProcPath = &procPath
udevDataPath := "fixtures/udev/data"
config.Path.UdevDataPath = &udevDataPath
diskstatsDeviceExclude := "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$"
config.DiskstatsDeviceFilter.DiskstatsDeviceExclude = &diskstatsDeviceExclude
testcase := `# HELP node_disk_ata_rotation_rate_rpm ATA disk rotation rate in RPMs (0 for SSDs).
# TYPE node_disk_ata_rotation_rate_rpm gauge
node_disk_ata_rotation_rate_rpm{device="sda"} 7200
@ -314,7 +329,6 @@ node_disk_written_bytes_total{device="sr0"} 0
node_disk_written_bytes_total{device="vda"} 1.0938236928e+11
`
config := NodeCollectorConfig{}
logger := log.NewLogfmtLogger(os.Stderr)
collector, err := NewDiskstatsCollector(config, logger)
if err != nil {

View File

@ -23,7 +23,6 @@ import (
"syscall"
"testing"
"github.com/docker/cli/cli/config"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
@ -283,6 +282,7 @@ func TestBuildEthtoolFQName(t *testing.T) {
}
func TestEthToolCollector(t *testing.T) {
config := NodeCollectorConfig{}
testcase := `# HELP node_ethtool_align_errors Network interface align_errors
# TYPE node_ethtool_align_errors untyped
node_ethtool_align_errors{device="eth0"} 0
@ -366,9 +366,9 @@ node_network_supported_speed_bytes{device="eth0",duplex="full",mode="10baseT"} 1
node_network_supported_speed_bytes{device="eth0",duplex="half",mode="100baseT"} 1.25e+07
node_network_supported_speed_bytes{device="eth0",duplex="half",mode="10baseT"} 1.25e+06
`
*config.Path.SysPath = "fixtures/sys"
sysPath := "fixtures/sys"
config.Path.SysPath = &sysPath
config := NodeCollectorConfig{}
logger := log.NewLogfmtLogger(os.Stderr)
collector, err := NewEthtoolTestCollector(config, logger)
if err != nil {

View File

@ -34,9 +34,6 @@ import (
// * filesystemCollector.GetStats
var (
mountPointsExcludeSet bool
fsTypesExcludeSet bool
filesystemLabelNames = []string{"device", "mountpoint", "fstype"}
)
@ -62,8 +59,10 @@ type filesystemStats struct {
}
type FilesystemConfig struct {
MountPointsExclude *string
MountPointsExcludeSet bool
OldMountPointsExcluded *string
FSTypesExclude *string
FSTypesExcludeSet bool
OldFSTypesExcluded *string
MountTimeout *time.Duration
StatWorkerCount *int
@ -76,7 +75,7 @@ func init() {
// NewFilesystemCollector returns a new Collector exposing filesystems stats.
func NewFilesystemCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) {
if *config.Filesystem.OldMountPointsExcluded != "" {
if !mountPointsExcludeSet {
if !config.Filesystem.MountPointsExcludeSet {
level.Warn(logger).Log("msg", "--collector.filesystem.ignored-mount-points is DEPRECATED and will be removed in 2.0.0, use --collector.filesystem.mount-points-exclude")
*config.Filesystem.MountPointsExclude = *config.Filesystem.OldMountPointsExcluded
} else {
@ -90,7 +89,7 @@ func NewFilesystemCollector(config NodeCollectorConfig, logger log.Logger) (Coll
}
if *config.Filesystem.OldFSTypesExcluded != "" {
if !fsTypesExcludeSet {
if !config.Filesystem.FSTypesExcludeSet {
level.Warn(logger).Log("msg", "--collector.filesystem.ignored-fs-types is DEPRECATED and will be removed in 2.0.0, use --collector.filesystem.fs-types-exclude")
*config.Filesystem.FSTypesExclude = *config.Filesystem.OldFSTypesExcluded
} else {

View File

@ -14,14 +14,16 @@
package collector
import (
"github.com/go-kit/log"
"strings"
"testing"
"github.com/go-kit/log"
"github.com/alecthomas/kingpin/v2"
)
func Test_parseFilesystemLabelsError(t *testing.T) {
config := NodeCollectorConfig{}
tests := []struct {
name string
in string
@ -34,7 +36,7 @@ func Test_parseFilesystemLabelsError(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := parseFilesystemLabels(strings.NewReader(tt.in)); err == nil {
if _, err := parseFilesystemLabels(config, strings.NewReader(tt.in)); err == nil {
t.Fatal("expected an error, but none occurred")
}
})
@ -42,9 +44,12 @@ func Test_parseFilesystemLabelsError(t *testing.T) {
}
func TestMountPointDetails(t *testing.T) {
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures/proc"}); err != nil {
t.Fatal(err)
}
path := "./fixtures/proc"
config := NodeCollectorConfig{Path: PathConfig{ProcPath: &path, SysPath: &path, RootfsPath: &path, UdevDataPath: &path}}
// if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures/proc"}); err != nil {
// t.Fatal(err)
// }
expected := map[string]string{
"/": "",
@ -79,7 +84,7 @@ func TestMountPointDetails(t *testing.T) {
"/var/lib/kubelet/plugins/kubernetes.io/vsphere-volume/mounts/[vsanDatastore] bafb9e5a-8856-7e6c-699c-801844e77a4a/kubernetes-dynamic-pvc-3eba5bba-48a3-11e8-89ab-005056b92113.vmdk": "",
}
filesystems, err := mountPointDetails(log.NewNopLogger())
filesystems, err := mountPointDetails(config, log.NewNopLogger())
if err != nil {
t.Log(err)
}
@ -92,6 +97,8 @@ func TestMountPointDetails(t *testing.T) {
}
func TestMountsFallback(t *testing.T) {
config := NodeCollectorConfig{}
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_hidepid/proc"}); err != nil {
t.Fatal(err)
}
@ -100,7 +107,7 @@ func TestMountsFallback(t *testing.T) {
"/": "",
}
filesystems, err := mountPointDetails(log.NewNopLogger())
filesystems, err := mountPointDetails(config, log.NewNopLogger())
if err != nil {
t.Log(err)
}
@ -113,6 +120,8 @@ func TestMountsFallback(t *testing.T) {
}
func TestPathRootfs(t *testing.T) {
config := NodeCollectorConfig{}
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./fixtures_bindmount/proc", "--path.rootfs", "/host"}); err != nil {
t.Fatal(err)
}
@ -128,7 +137,7 @@ func TestPathRootfs(t *testing.T) {
"/sys/fs/cgroup": "",
}
filesystems, err := mountPointDetails(log.NewNopLogger())
filesystems, err := mountPointDetails(config, log.NewNopLogger())
if err != nil {
t.Log(err)
}

View File

@ -16,8 +16,10 @@ package collector
import "testing"
func TestLoad(t *testing.T) {
config := NodeCollectorConfig{}
want := []float64{0.21, 0.37, 0.39}
loads, err := parseLoad("0.21 0.37 0.39 1/719 19737")
loads, err := parseLoad(config, "0.21 0.37 0.39 1/719 19737")
if err != nil {
t.Fatal(err)
}

View File

@ -21,57 +21,64 @@ import (
)
func TestDefaultProcPath(t *testing.T) {
config := PathConfig{}
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", procfs.DefaultMountPoint}); err != nil {
t.Fatal(err)
}
if got, want := c.config.Path.procFilePath("somefile"), "/proc/somefile"; got != want {
if got, want := config.procFilePath("somefile"), "/proc/somefile"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got)
}
if got, want := c.config.Path.procFilePath("some/file"), "/proc/some/file"; got != want {
if got, want := config.procFilePath("some/file"), "/proc/some/file"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got)
}
}
func TestCustomProcPath(t *testing.T) {
config := PathConfig{}
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "./../some/./place/"}); err != nil {
t.Fatal(err)
}
if got, want := c.config.Path.procFilePath("somefile"), "../some/place/somefile"; got != want {
if got, want := config.procFilePath("somefile"), "../some/place/somefile"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got)
}
if got, want := c.config.Path.procFilePath("some/file"), "../some/place/some/file"; got != want {
if got, want := config.procFilePath("some/file"), "../some/place/some/file"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got)
}
}
func TestDefault*config.Path.SysPath(t *testing.T) {
func TestDefaultSysPath(t *testing.T) {
config := PathConfig{}
if _, err := kingpin.CommandLine.Parse([]string{"--path.sysfs", "/sys"}); err != nil {
t.Fatal(err)
}
if got, want := *config.Path.SysPath("somefile"), "/sys/somefile"; got != want {
if got, want := config.sysFilePath("somefile"), "/sys/somefile"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got)
}
if got, want := *config.Path.SysPath("some/file"), "/sys/some/file"; got != want {
if got, want := config.sysFilePath("some/file"), "/sys/some/file"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got)
}
}
func TestCustom*config.Path.SysPath(t *testing.T) {
func TestCustomSysPath(t *testing.T) {
config := PathConfig{}
if _, err := kingpin.CommandLine.Parse([]string{"--path.sysfs", "./../some/./place/"}); err != nil {
t.Fatal(err)
}
if got, want := *config.Path.SysPath("somefile"), "../some/place/somefile"; got != want {
if got, want := config.sysFilePath("somefile"), "../some/place/somefile"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got)
}
if got, want := *config.Path.SysPath("some/file"), "../some/place/some/file"; got != want {
if got, want := config.sysFilePath("some/file"), "../some/place/some/file"; got != want {
t.Errorf("Expected: %s, Got: %s", want, got)
}
}

View File

@ -65,6 +65,8 @@ func TestPerfCollector(t *testing.T) {
}
func TestPerfCollectorStride(t *testing.T) {
config := NodeCollectorConfig{}
canTestPerf(t)
tests := []struct {
@ -97,8 +99,8 @@ func TestPerfCollectorStride(t *testing.T) {
t.Skipf("Skipping test because runtime.NumCPU < %d", cpu)
}
}
perfCPUsFlag = &test.flag
collector, err := NewPerfCollector(NodeCollectorConfig{}, log.NewNopLogger())
config.Perf.CPUs = &test.flag
collector, err := NewPerfCollector(config, log.NewNopLogger())
if err != nil {
t.Fatal(err)
}

View File

@ -20,12 +20,12 @@ import (
"testing"
"github.com/alecthomas/kingpin/v2"
"github.com/docker/cli/cli/config"
"github.com/go-kit/log"
"github.com/prometheus/procfs"
)
func TestReadProcessStatus(t *testing.T) {
config := NodeCollectorConfig{}
if _, err := kingpin.CommandLine.Parse([]string{"--path.procfs", "fixtures/proc"}); err != nil {
t.Fatal(err)
}

View File

@ -41,9 +41,6 @@ const (
)
var (
systemdUnitIncludeSet bool
systemdUnitExcludeSet bool
systemdVersionRE = regexp.MustCompile(`[0-9]{3,}(\.[0-9]+)?`)
)
@ -75,7 +72,9 @@ func init() {
type SystemdConfig struct {
UnitInclude *string
UnitIncludeSet bool
UnitExclude *string
UnitExcludeSet bool
OldUnitInclude *string
OldUnitExclude *string
Private *bool
@ -132,7 +131,7 @@ func NewSystemdCollector(config NodeCollectorConfig, logger log.Logger) (Collect
"Detected systemd version", []string{"version"}, nil)
if *config.Systemd.OldUnitExclude != "" {
if !systemdUnitExcludeSet {
if !config.Systemd.UnitExcludeSet {
level.Warn(logger).Log("msg", "--collector.systemd.unit-blacklist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-exclude")
*config.Systemd.UnitExclude = *config.Systemd.OldUnitExclude
} else {
@ -140,7 +139,7 @@ func NewSystemdCollector(config NodeCollectorConfig, logger log.Logger) (Collect
}
}
if *config.Systemd.OldUnitInclude != "" {
if !systemdUnitIncludeSet {
if !config.Systemd.UnitIncludeSet {
level.Warn(logger).Log("msg", "--collector.systemd.unit-whitelist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-include")
*config.Systemd.UnitInclude = *config.Systemd.OldUnitInclude
} else {

View File

@ -36,7 +36,7 @@ func AddFlags(a *kingpin.Application) collector.NodeCollectorConfig {
"collector.diskstats.device-exclude",
"Regexp of diskstats devices to exclude (mutually exclusive to device-include).",
).PreAction(func(c *kingpin.ParseContext) error {
diskstatsDeviceExcludeSet = true
config.DiskstatsDeviceFilter.DiskstatsDeviceExcludeSet = true
return nil
}).String()
config.DiskstatsDeviceFilter.OldDiskstatsDeviceExclude = a.Flag(
@ -53,7 +53,7 @@ func AddFlags(a *kingpin.Application) collector.NodeCollectorConfig {
"collector.filesystem.mount-points-exclude",
"Regexp of mount points to exclude for filesystem collector.",
).PreAction(func(c *kingpin.ParseContext) error {
mountPointsExcludeSet = true
config.Filesystem.MountPointsExcludeSet = true
return nil
}).String()
config.Filesystem.OldMountPointsExcluded = a.Flag(
@ -65,7 +65,7 @@ func AddFlags(a *kingpin.Application) collector.NodeCollectorConfig {
"collector.filesystem.fs-types-exclude",
"Regexp of filesystem types to exclude for filesystem collector.",
).PreAction(func(c *kingpin.ParseContext) error {
fsTypesExcludeSet = true
config.Filesystem.FSTypesExcludeSet = true
return nil
}).String()
config.Filesystem.OldFSTypesExcluded = a.Flag(
@ -109,10 +109,10 @@ func AddFlags(a *kingpin.Application) collector.NodeCollectorConfig {
config.NTP.MaxDistance = a.Flag("collector.ntp.max-distance", "Max accumulated distance to the root").Default("3.46608s").Duration()
config.NTP.OffsetTolerance = a.Flag("collector.ntp.local-offset-tolerance", "Offset between local clock and local ntpd time to tolerate").Default("1ms").Duration()
*config.Path.ProcPath = kingpin.Flag("path.procfs", "procfs mountpoint.").Default(procfs.DefaultMountPoint).String()
config.Path.*config.Path.SysPath = kingpin.Flag("path.sysfs", "sysfs mountpoint.").Default("/sys").String()
config.Path.RootfsPath = kingpin.Flag("path.rootfs", "rootfs mountpoint.").Default("/").String()
config.Path.UdevDataPath = kingpin.Flag("path.udev.data", "udev data path.").Default("/run/udev/data").String()
config.Path.ProcPath = a.Flag("path.procfs", "procfs mountpoint.").Default(procfs.DefaultMountPoint).String()
config.Path.SysPath = a.Flag("path.sysfs", "sysfs mountpoint.").Default("/sys").String()
config.Path.RootfsPath = a.Flag("path.rootfs", "rootfs mountpoint.").Default("/").String()
config.Path.UdevDataPath = a.Flag("path.udev.data", "udev data path.").Default("/run/udev/data").String()
config.Perf.CPUs = a.Flag("collector.perf.cpus", "List of CPUs from which perf metrics should be collected").Default("").String()
config.Perf.Tracepoint = a.Flag("collector.perf.tracepoint", "perf tracepoint that should be collected").Strings()
@ -141,21 +141,21 @@ func AddFlags(a *kingpin.Application) collector.NodeCollectorConfig {
config.Sysctl.IncludeInfo = a.Flag("collector.sysctl.include-info", "Select sysctl metrics to include as info metrics").Strings()
config.Systemd.UnitInclude = a.Flag("collector.systemd.unit-include", "Regexp of systemd units to include. Units must both match include and not match exclude to be included.").Default(".+").PreAction(func(c *kingpin.ParseContext) error {
systemdUnitIncludeSet = true
config.Systemd.UnitIncludeSet = true
return nil
}).String()
config.Systemd.UnitExclude = kingpin.Flag("collector.systemd.unit-exclude", "Regexp of systemd units to exclude. Units must both match include and not match exclude to be included.").Default(".+\\.(automount|device|mount|scope|slice)").PreAction(func(c *kingpin.ParseContext) error {
systemdUnitExcludeSet = true
config.Systemd.UnitExclude = a.Flag("collector.systemd.unit-exclude", "Regexp of systemd units to exclude. Units must both match include and not match exclude to be included.").Default(".+\\.(automount|device|mount|scope|slice)").PreAction(func(c *kingpin.ParseContext) error {
config.Systemd.UnitExcludeSet = true
return nil
}).String()
config.Systemd.OldUnitInclude = kingpin.Flag("collector.systemd.unit-whitelist", "DEPRECATED: Use --collector.systemd.unit-include").Hidden().String()
config.Systemd.OldUnitExclude = kingpin.Flag("collector.systemd.unit-blacklist", "DEPRECATED: Use collector.systemd.unit-exclude").Hidden().String()
config.Systemd.Private = kingpin.Flag("collector.systemd.private", "Establish a private, direct connection to systemd without dbus (Strongly discouraged since it requires root. For testing purposes only).").Hidden().Bool()
config.Systemd.EnableTaskMetrics = kingpin.Flag("collector.systemd.enable-task-metrics", "Enables service unit tasks metrics unit_tasks_current and unit_tasks_max").Bool()
config.Systemd.EnableRestartsMetrics = kingpin.Flag("collector.systemd.enable-restarts-metrics", "Enables service unit metric service_restart_total").Bool()
config.Systemd.EnableStartTimeMetrics = kingpin.Flag("collector.systemd.enable-start-time-metrics", "Enables service unit metric unit_start_time_seconds").Bool()
config.Systemd.OldUnitInclude = a.Flag("collector.systemd.unit-whitelist", "DEPRECATED: Use --collector.systemd.unit-include").Hidden().String()
config.Systemd.OldUnitExclude = a.Flag("collector.systemd.unit-blacklist", "DEPRECATED: Use collector.systemd.unit-exclude").Hidden().String()
config.Systemd.Private = a.Flag("collector.systemd.private", "Establish a private, direct connection to systemd without dbus (Strongly discouraged since it requires root. For testing purposes only).").Hidden().Bool()
config.Systemd.EnableTaskMetrics = a.Flag("collector.systemd.enable-task-metrics", "Enables service unit tasks metrics unit_tasks_current and unit_tasks_max").Bool()
config.Systemd.EnableRestartsMetrics = a.Flag("collector.systemd.enable-restarts-metrics", "Enables service unit metric service_restart_total").Bool()
config.Systemd.EnableStartTimeMetrics = a.Flag("collector.systemd.enable-start-time-metrics", "Enables service unit metric unit_start_time_seconds").Bool()
config.Tapestats.IgnoredDevices = kingpin.Flag("collector.tapestats.ignored-devices", "Regexp of devices to ignore for tapestats.").Default("^$").String()
config.Tapestats.IgnoredDevices = a.Flag("collector.tapestats.ignored-devices", "Regexp of devices to ignore for tapestats.").Default("^$").String()
config.TextFile.Directory = a.Flag("collector.textfile.directory", "Directory to read text files with metrics from.").Default("").String()