From 9c6ff78e74e962c76f8b7efafcc25617adb3787c Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Fri, 21 Jul 2023 15:02:36 +0200 Subject: [PATCH] Refactor collector package configuration Add a new pattern to allow moving the flag handling out of the collector package and into a separate package. This allows collector package users to create their own config or flag handling. Signed-off-by: Ben Kochie --- collector/arp_linux.go | 11 +++------- collector/bcache_linux.go | 2 +- collector/bonding_linux.go | 2 +- collector/boot_time_bsd.go | 2 +- collector/boot_time_solaris.go | 2 +- collector/btrfs_linux.go | 2 +- collector/buddyinfo.go | 2 +- collector/cgroups_linux.go | 2 +- collector/collector.go | 8 ++++---- collector/config.go | 26 ++++++++++++++++++++++++ collector/conntrack_linux.go | 2 +- collector/cpu_darwin.go | 2 +- collector/cpu_dragonfly.go | 2 +- collector/cpu_freebsd.go | 2 +- collector/cpu_linux.go | 2 +- collector/cpu_netbsd.go | 2 +- collector/cpu_openbsd.go | 2 +- collector/cpu_solaris.go | 2 +- collector/cpu_vulnerabilities_linux.go | 2 +- collector/cpufreq_linux.go | 2 +- collector/cpufreq_solaris.go | 2 +- collector/devstat_dragonfly.go | 2 +- collector/devstat_freebsd.go | 2 +- collector/diskstats_darwin.go | 2 +- collector/diskstats_linux.go | 2 +- collector/diskstats_linux_test.go | 9 +++++---- collector/diskstats_openbsd.go | 2 +- collector/diskstats_openbsd_amd64.go | 2 +- collector/dmi.go | 2 +- collector/drbd_linux.go | 2 +- collector/drm_linux.go | 2 +- collector/edac_linux.go | 2 +- collector/entropy_linux.go | 2 +- collector/ethtool_linux.go | 6 +++--- collector/ethtool_linux_test.go | 13 ++++++------ collector/exec_bsd.go | 2 +- collector/fibrechannel_linux.go | 2 +- collector/filefd_linux.go | 2 +- collector/filesystem_common.go | 2 +- collector/hwmon_linux.go | 2 +- collector/infiniband_linux.go | 2 +- collector/interrupts_common.go | 2 +- collector/ipvs_linux.go | 6 +++--- collector/ipvs_linux_test.go | 4 ++-- collector/ksmd_linux.go | 2 +- collector/lnstat_linux.go | 2 +- collector/loadavg.go | 2 +- collector/logind_linux.go | 2 +- collector/mdadm_linux.go | 2 +- collector/meminfo.go | 2 +- collector/meminfo_numa_linux.go | 2 +- collector/memory_bsd.go | 2 +- collector/mountstats_linux.go | 2 +- collector/netclass_linux.go | 2 +- collector/netdev_common.go | 2 +- collector/netisr_freebsd.go | 2 +- collector/netstat_linux.go | 2 +- collector/network_route_linux.go | 2 +- collector/nfs_linux.go | 2 +- collector/nfsd_linux.go | 2 +- collector/ntp.go | 2 +- collector/nvme_linux.go | 2 +- collector/os_release.go | 2 +- collector/os_release_test.go | 2 +- collector/perf_linux.go | 2 +- collector/perf_linux_test.go | 4 ++-- collector/powersupplyclass.go | 2 +- collector/pressure_linux.go | 2 +- collector/processes_linux.go | 2 +- collector/qdisc_linux.go | 2 +- collector/rapl_linux.go | 2 +- collector/runit.go | 2 +- collector/schedstat_linux.go | 2 +- collector/selinux_linux.go | 2 +- collector/slabinfo_linux.go | 2 +- collector/sockstat_linux.go | 2 +- collector/softirqs_common.go | 2 +- collector/softnet_linux.go | 2 +- collector/stat_linux.go | 2 +- collector/supervisord.go | 2 +- collector/sysctl_linux.go | 2 +- collector/systemd_linux.go | 2 +- collector/systemd_linux_test.go | 2 +- collector/tapestats_linux.go | 2 +- collector/tcpstat_linux.go | 2 +- collector/textfile.go | 2 +- collector/thermal_darwin.go | 2 +- collector/thermal_zone_linux.go | 2 +- collector/time.go | 2 +- collector/timex.go | 2 +- collector/udp_queues_linux.go | 2 +- collector/uname.go | 2 +- collector/vmstat_linux.go | 2 +- collector/wifi_linux.go | 2 +- collector/xfs_linux.go | 2 +- collector/zfs.go | 2 +- collector/zfs_freebsd.go | 2 +- collector/zfs_solaris.go | 2 +- collector/zoneinfo_linux.go | 2 +- kingpinconfig/flags.go | 28 ++++++++++++++++++++++++++ node_exporter.go | 12 +++++++---- 101 files changed, 181 insertions(+), 126 deletions(-) create mode 100644 collector/config.go create mode 100644 kingpinconfig/flags.go diff --git a/collector/arp_linux.go b/collector/arp_linux.go index 46b8893e..f997c111 100644 --- a/collector/arp_linux.go +++ b/collector/arp_linux.go @@ -18,17 +18,12 @@ package collector import ( "fmt" - "github.com/alecthomas/kingpin/v2" + "github.com/go-kit/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/procfs" ) -var ( - arpDeviceInclude = kingpin.Flag("collector.arp.device-include", "Regexp of arp devices to include (mutually exclusive to device-exclude).").String() - arpDeviceExclude = kingpin.Flag("collector.arp.device-exclude", "Regexp of arp devices to exclude (mutually exclusive to device-include).").String() -) - type arpCollector struct { fs procfs.FS deviceFilter deviceFilter @@ -41,7 +36,7 @@ func init() { } // NewARPCollector returns a new Collector exposing ARP stats. -func NewARPCollector(logger log.Logger) (Collector, error) { +func NewARPCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) @@ -49,7 +44,7 @@ func NewARPCollector(logger log.Logger) (Collector, error) { return &arpCollector{ fs: fs, - deviceFilter: newDeviceFilter(*arpDeviceExclude, *arpDeviceInclude), + deviceFilter: newDeviceFilter(*config.Arp.DeviceExclude, *config.Arp.DeviceInclude), entries: prometheus.NewDesc( prometheus.BuildFQName(namespace, "arp", "entries"), "ARP entries by device", diff --git a/collector/bcache_linux.go b/collector/bcache_linux.go index 1d402d3f..c61ac274 100644 --- a/collector/bcache_linux.go +++ b/collector/bcache_linux.go @@ -41,7 +41,7 @@ type bcacheCollector struct { // NewBcacheCollector returns a newly allocated bcacheCollector. // It exposes a number of Linux bcache statistics. -func NewBcacheCollector(logger log.Logger) (Collector, error) { +func NewBcacheCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := bcache.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) diff --git a/collector/bonding_linux.go b/collector/bonding_linux.go index d9d04e22..c69fc312 100644 --- a/collector/bonding_linux.go +++ b/collector/bonding_linux.go @@ -39,7 +39,7 @@ func init() { // NewBondingCollector returns a newly allocated bondingCollector. // It exposes the number of configured and active slave of linux bonding interfaces. -func NewBondingCollector(logger log.Logger) (Collector, error) { +func NewBondingCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &bondingCollector{ slaves: typedDesc{prometheus.NewDesc( prometheus.BuildFQName(namespace, "bonding", "slaves"), diff --git a/collector/boot_time_bsd.go b/collector/boot_time_bsd.go index 8a9c17b3..f8831854 100644 --- a/collector/boot_time_bsd.go +++ b/collector/boot_time_bsd.go @@ -32,7 +32,7 @@ func init() { } // newBootTimeCollector returns a new Collector exposing system boot time on BSD systems. -func newBootTimeCollector(logger log.Logger) (Collector, error) { +func newBootTimeCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &bootTimeCollector{ logger: logger, }, nil diff --git a/collector/boot_time_solaris.go b/collector/boot_time_solaris.go index 3d55e783..6782cd72 100644 --- a/collector/boot_time_solaris.go +++ b/collector/boot_time_solaris.go @@ -31,7 +31,7 @@ func init() { registerCollector("boottime", defaultEnabled, newBootTimeCollector) } -func newBootTimeCollector(logger log.Logger) (Collector, error) { +func newBootTimeCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &bootTimeCollector{ boottime: typedDesc{ prometheus.NewDesc( diff --git a/collector/btrfs_linux.go b/collector/btrfs_linux.go index b5642b33..f551f169 100644 --- a/collector/btrfs_linux.go +++ b/collector/btrfs_linux.go @@ -40,7 +40,7 @@ func init() { } // NewBtrfsCollector returns a new Collector exposing Btrfs statistics. -func NewBtrfsCollector(logger log.Logger) (Collector, error) { +func NewBtrfsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := btrfs.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) diff --git a/collector/buddyinfo.go b/collector/buddyinfo.go index c3cc5e04..d0ad8cae 100644 --- a/collector/buddyinfo.go +++ b/collector/buddyinfo.go @@ -41,7 +41,7 @@ func init() { } // NewBuddyinfoCollector returns a new Collector exposing buddyinfo stats. -func NewBuddyinfoCollector(logger log.Logger) (Collector, error) { +func NewBuddyinfoCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { desc := prometheus.NewDesc( prometheus.BuildFQName(namespace, buddyInfoSubsystem, "blocks"), "Count of free blocks according to size.", diff --git a/collector/cgroups_linux.go b/collector/cgroups_linux.go index 9f6d01a4..47f0c117 100644 --- a/collector/cgroups_linux.go +++ b/collector/cgroups_linux.go @@ -38,7 +38,7 @@ func init() { } // NewCgroupSummaryCollector returns a new Collector exposing a summary of cgroups. -func NewCgroupSummaryCollector(logger log.Logger) (Collector, error) { +func NewCgroupSummaryCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/collector.go b/collector/collector.go index 3112c789..85c9a5f4 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -50,14 +50,14 @@ const ( ) var ( - factories = make(map[string]func(logger log.Logger) (Collector, error)) + factories = make(map[string]func(config NodeCollectorConfig, logger log.Logger) (Collector, error)) initiatedCollectorsMtx = sync.Mutex{} initiatedCollectors = make(map[string]Collector) collectorState = make(map[string]*bool) forcedCollectors = map[string]bool{} // collectors which have been explicitly enabled or disabled ) -func registerCollector(collector string, isDefaultEnabled bool, factory func(logger log.Logger) (Collector, error)) { +func registerCollector(collector string, isDefaultEnabled bool, factory func(config NodeCollectorConfig, logger log.Logger) (Collector, error)) { var helpDefaultState string if isDefaultEnabled { helpDefaultState = "enabled" @@ -104,7 +104,7 @@ func collectorFlagAction(collector string) func(ctx *kingpin.ParseContext) error } // NewNodeCollector creates a new NodeCollector. -func NewNodeCollector(logger log.Logger, filters ...string) (*NodeCollector, error) { +func NewNodeCollector(config NodeCollectorConfig, logger log.Logger, filters ...string) (*NodeCollector, error) { f := make(map[string]bool) for _, filter := range filters { enabled, exist := collectorState[filter] @@ -126,7 +126,7 @@ func NewNodeCollector(logger log.Logger, filters ...string) (*NodeCollector, err if collector, ok := initiatedCollectors[key]; ok { collectors[key] = collector } else { - collector, err := factories[key](log.With(logger, "collector", key)) + collector, err := factories[key](config, log.With(logger, "collector", key)) if err != nil { return nil, err } diff --git a/collector/config.go b/collector/config.go new file mode 100644 index 00000000..fb3e576b --- /dev/null +++ b/collector/config.go @@ -0,0 +1,26 @@ +// Copyright 2023 The Prometheus 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 !noarp +// +build !noarp + +package collector + +type NodeCollectorConfig struct { + Arp ArpConfig +} + +type ArpConfig struct { + DeviceInclude *string + DeviceExclude *string +} diff --git a/collector/conntrack_linux.go b/collector/conntrack_linux.go index e4ea9549..989f74f2 100644 --- a/collector/conntrack_linux.go +++ b/collector/conntrack_linux.go @@ -57,7 +57,7 @@ func init() { } // NewConntrackCollector returns a new Collector exposing conntrack stats. -func NewConntrackCollector(logger log.Logger) (Collector, error) { +func NewConntrackCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &conntrackCollector{ current: prometheus.NewDesc( prometheus.BuildFQName(namespace, "", "nf_conntrack_entries"), diff --git a/collector/cpu_darwin.go b/collector/cpu_darwin.go index 6c461cc3..bacde20a 100644 --- a/collector/cpu_darwin.go +++ b/collector/cpu_darwin.go @@ -61,7 +61,7 @@ func init() { } // NewCPUCollector returns a new Collector exposing CPU stats. -func NewCPUCollector(logger log.Logger) (Collector, error) { +func NewCPUCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &statCollector{ cpu: nodeCPUSecondsDesc, logger: logger, diff --git a/collector/cpu_dragonfly.go b/collector/cpu_dragonfly.go index 61cba1ee..1420c436 100644 --- a/collector/cpu_dragonfly.go +++ b/collector/cpu_dragonfly.go @@ -86,7 +86,7 @@ func init() { } // NewStatCollector returns a new Collector exposing CPU stats. -func NewStatCollector(logger log.Logger) (Collector, error) { +func NewStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &statCollector{ cpu: nodeCPUSecondsDesc, logger: logger, diff --git a/collector/cpu_freebsd.go b/collector/cpu_freebsd.go index 96b0f033..d75b8adc 100644 --- a/collector/cpu_freebsd.go +++ b/collector/cpu_freebsd.go @@ -93,7 +93,7 @@ func init() { } // NewStatCollector returns a new Collector exposing CPU stats. -func NewStatCollector(logger log.Logger) (Collector, error) { +func NewStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &statCollector{ cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue}, temp: typedDesc{prometheus.NewDesc( diff --git a/collector/cpu_linux.go b/collector/cpu_linux.go index 69e0a175..ce6608ce 100644 --- a/collector/cpu_linux.go +++ b/collector/cpu_linux.go @@ -69,7 +69,7 @@ func init() { } // NewCPUCollector returns a new Collector exposing kernel/system statistics. -func NewCPUCollector(logger log.Logger) (Collector, error) { +func NewCPUCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/cpu_netbsd.go b/collector/cpu_netbsd.go index 7fc95c80..4f1e63fb 100644 --- a/collector/cpu_netbsd.go +++ b/collector/cpu_netbsd.go @@ -222,7 +222,7 @@ func init() { } // NewStatCollector returns a new Collector exposing CPU stats. -func NewStatCollector(logger log.Logger) (Collector, error) { +func NewStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &statCollector{ cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue}, temp: typedDesc{prometheus.NewDesc( diff --git a/collector/cpu_openbsd.go b/collector/cpu_openbsd.go index 8715ff98..31c3d49e 100644 --- a/collector/cpu_openbsd.go +++ b/collector/cpu_openbsd.go @@ -52,7 +52,7 @@ func init() { registerCollector("cpu", defaultEnabled, NewCPUCollector) } -func NewCPUCollector(logger log.Logger) (Collector, error) { +func NewCPUCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &cpuCollector{ cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue}, logger: logger, diff --git a/collector/cpu_solaris.go b/collector/cpu_solaris.go index c28b4d6d..702ba3f8 100644 --- a/collector/cpu_solaris.go +++ b/collector/cpu_solaris.go @@ -36,7 +36,7 @@ func init() { registerCollector("cpu", defaultEnabled, NewCpuCollector) } -func NewCpuCollector(logger log.Logger) (Collector, error) { +func NewCpuCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &cpuCollector{ cpu: typedDesc{nodeCPUSecondsDesc, prometheus.CounterValue}, logger: logger, diff --git a/collector/cpu_vulnerabilities_linux.go b/collector/cpu_vulnerabilities_linux.go index 1875488b..ccbda762 100644 --- a/collector/cpu_vulnerabilities_linux.go +++ b/collector/cpu_vulnerabilities_linux.go @@ -40,7 +40,7 @@ func init() { registerCollector(cpuVulerabilitiesCollector, defaultDisabled, NewVulnerabilitySysfsCollector) } -func NewVulnerabilitySysfsCollector(logger log.Logger) (Collector, error) { +func NewVulnerabilitySysfsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &cpuVulnerabilitiesCollector{}, nil } diff --git a/collector/cpufreq_linux.go b/collector/cpufreq_linux.go index 3372be1a..b926218f 100644 --- a/collector/cpufreq_linux.go +++ b/collector/cpufreq_linux.go @@ -34,7 +34,7 @@ func init() { } // NewCPUFreqCollector returns a new Collector exposing kernel/system statistics. -func NewCPUFreqCollector(logger log.Logger) (Collector, error) { +func NewCPUFreqCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := sysfs.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) diff --git a/collector/cpufreq_solaris.go b/collector/cpufreq_solaris.go index 6c76ad01..00046e48 100644 --- a/collector/cpufreq_solaris.go +++ b/collector/cpufreq_solaris.go @@ -36,7 +36,7 @@ func init() { registerCollector("cpufreq", defaultEnabled, NewCpuFreqCollector) } -func NewCpuFreqCollector(logger log.Logger) (Collector, error) { +func NewCpuFreqCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &cpuFreqCollector{ logger: logger, }, nil diff --git a/collector/devstat_dragonfly.go b/collector/devstat_dragonfly.go index 11678054..746d0ea7 100644 --- a/collector/devstat_dragonfly.go +++ b/collector/devstat_dragonfly.go @@ -106,7 +106,7 @@ func init() { } // NewDevstatCollector returns a new Collector exposing Device stats. -func NewDevstatCollector(logger log.Logger) (Collector, error) { +func NewDevstatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &devstatCollector{ bytesDesc: prometheus.NewDesc( prometheus.BuildFQName(namespace, devstatSubsystem, "bytes_total"), diff --git a/collector/devstat_freebsd.go b/collector/devstat_freebsd.go index 20cdc276..a8c96a1a 100644 --- a/collector/devstat_freebsd.go +++ b/collector/devstat_freebsd.go @@ -51,7 +51,7 @@ func init() { } // NewDevstatCollector returns a new Collector exposing Device stats. -func NewDevstatCollector(logger log.Logger) (Collector, error) { +func NewDevstatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &devstatCollector{ devinfo: &C.struct_devinfo{}, bytes: typedDesc{prometheus.NewDesc( diff --git a/collector/diskstats_darwin.go b/collector/diskstats_darwin.go index b5f6c538..18b92637 100644 --- a/collector/diskstats_darwin.go +++ b/collector/diskstats_darwin.go @@ -43,7 +43,7 @@ func init() { } // NewDiskstatsCollector returns a new Collector exposing disk device stats. -func NewDiskstatsCollector(logger log.Logger) (Collector, error) { +func NewDiskstatsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { var diskLabelNames = []string{"device"} deviceFilter, err := newDiskstatsDeviceFilter(logger) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index ffda6ae4..f49f67d9 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -95,7 +95,7 @@ func init() { // NewDiskstatsCollector returns a new Collector exposing disk device stats. // Docs from https://www.kernel.org/doc/Documentation/iostats.txt -func NewDiskstatsCollector(logger log.Logger) (Collector, error) { +func NewDiskstatsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { var diskLabelNames = []string{"device"} fs, err := blockdevice.NewFS(*procPath, *sysPath) if err != nil { diff --git a/collector/diskstats_linux_test.go b/collector/diskstats_linux_test.go index 526b25d6..2c2e63ea 100644 --- a/collector/diskstats_linux_test.go +++ b/collector/diskstats_linux_test.go @@ -36,8 +36,8 @@ func (c testDiskStatsCollector) Describe(ch chan<- *prometheus.Desc) { prometheus.DescribeByCollect(c, ch) } -func NewTestDiskStatsCollector(logger log.Logger) (prometheus.Collector, error) { - dsc, err := NewDiskstatsCollector(logger) +func NewTestDiskStatsCollector(config NodeCollectorConfig, logger log.Logger) (prometheus.Collector, error) { + dsc, err := NewDiskstatsCollector(config, logger) if err != nil { return testDiskStatsCollector{}, err } @@ -314,12 +314,13 @@ 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(logger) + collector, err := NewDiskstatsCollector(config, logger) if err != nil { panic(err) } - c, err := NewTestDiskStatsCollector(logger) + c, err := NewTestDiskStatsCollector(config, logger) if err != nil { t.Fatal(err) } diff --git a/collector/diskstats_openbsd.go b/collector/diskstats_openbsd.go index 46d9bc1a..90b5f219 100644 --- a/collector/diskstats_openbsd.go +++ b/collector/diskstats_openbsd.go @@ -49,7 +49,7 @@ func init() { } // NewDiskstatsCollector returns a new Collector exposing disk device stats. -func NewDiskstatsCollector(logger log.Logger) (Collector, error) { +func NewDiskstatsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { deviceFilter, err := newDiskstatsDeviceFilter(logger) if err != nil { return nil, fmt.Errorf("failed to parse device filter flags: %w", err) diff --git a/collector/diskstats_openbsd_amd64.go b/collector/diskstats_openbsd_amd64.go index a39cabb5..5cca7d8f 100644 --- a/collector/diskstats_openbsd_amd64.go +++ b/collector/diskstats_openbsd_amd64.go @@ -60,7 +60,7 @@ func init() { } // NewDiskstatsCollector returns a new Collector exposing disk device stats. -func NewDiskstatsCollector(logger log.Logger) (Collector, error) { +func NewDiskstatsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { deviceFilter, err := newDiskstatsDeviceFilter(logger) if err != nil { return nil, fmt.Errorf("failed to parse device filter flags: %w", err) diff --git a/collector/dmi.go b/collector/dmi.go index 79475507..959554aa 100644 --- a/collector/dmi.go +++ b/collector/dmi.go @@ -38,7 +38,7 @@ func init() { } // NewDMICollector returns a new Collector exposing DMI information. -func NewDMICollector(logger log.Logger) (Collector, error) { +func NewDMICollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := sysfs.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) diff --git a/collector/drbd_linux.go b/collector/drbd_linux.go index f192a24c..43e0785e 100644 --- a/collector/drbd_linux.go +++ b/collector/drbd_linux.go @@ -86,7 +86,7 @@ func init() { registerCollector("drbd", defaultDisabled, newDRBDCollector) } -func newDRBDCollector(logger log.Logger) (Collector, error) { +func newDRBDCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &drbdCollector{ numerical: map[string]drbdNumericalMetric{ "ns": newDRBDNumericalMetric( diff --git a/collector/drm_linux.go b/collector/drm_linux.go index 11b8c628..bafe1667 100644 --- a/collector/drm_linux.go +++ b/collector/drm_linux.go @@ -46,7 +46,7 @@ func init() { } // NewDrmCollector returns a new Collector exposing /sys/class/drm/card?/device stats. -func NewDrmCollector(logger log.Logger) (Collector, error) { +func NewDrmCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := sysfs.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) diff --git a/collector/edac_linux.go b/collector/edac_linux.go index c7719b7a..e6980af9 100644 --- a/collector/edac_linux.go +++ b/collector/edac_linux.go @@ -47,7 +47,7 @@ func init() { } // NewEdacCollector returns a new Collector exposing edac stats. -func NewEdacCollector(logger log.Logger) (Collector, error) { +func NewEdacCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &edacCollector{ ceCount: prometheus.NewDesc( prometheus.BuildFQName(namespace, edacSubsystem, "correctable_errors_total"), diff --git a/collector/entropy_linux.go b/collector/entropy_linux.go index 909d2ff4..06b81289 100644 --- a/collector/entropy_linux.go +++ b/collector/entropy_linux.go @@ -36,7 +36,7 @@ func init() { } // NewEntropyCollector returns a new Collector exposing entropy stats. -func NewEntropyCollector(logger log.Logger) (Collector, error) { +func NewEntropyCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/ethtool_linux.go b/collector/ethtool_linux.go index 0111701b..f974c3dd 100644 --- a/collector/ethtool_linux.go +++ b/collector/ethtool_linux.go @@ -85,7 +85,7 @@ type ethtoolCollector struct { // makeEthtoolCollector is the internal constructor for EthtoolCollector. // This allows NewEthtoolTestCollector to override its .ethtool interface // for testing. -func makeEthtoolCollector(logger log.Logger) (*ethtoolCollector, error) { +func makeEthtoolCollector(config NodeCollectorConfig, logger log.Logger) (*ethtoolCollector, error) { fs, err := sysfs.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) @@ -213,8 +213,8 @@ func buildEthtoolFQName(metric string) string { } // NewEthtoolCollector returns a new Collector exposing ethtool stats. -func NewEthtoolCollector(logger log.Logger) (Collector, error) { - return makeEthtoolCollector(logger) +func NewEthtoolCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { + return makeEthtoolCollector(config, logger) } // updatePortCapabilities generates metrics for autonegotiate, pause and asymmetricpause. diff --git a/collector/ethtool_linux_test.go b/collector/ethtool_linux_test.go index 761351c6..50db2be4 100644 --- a/collector/ethtool_linux_test.go +++ b/collector/ethtool_linux_test.go @@ -46,8 +46,8 @@ func (c testEthtoolCollector) Describe(ch chan<- *prometheus.Desc) { prometheus.DescribeByCollect(c, ch) } -func NewTestEthtoolCollector(logger log.Logger) (prometheus.Collector, error) { - dsc, err := NewEthtoolTestCollector(logger) +func NewTestEthtoolCollector(config NodeCollectorConfig, logger log.Logger) (prometheus.Collector, error) { + dsc, err := NewEthtoolTestCollector(config, logger) if err != nil { return testEthtoolCollector{}, err } @@ -252,8 +252,8 @@ func (e *EthtoolFixture) LinkInfo(intf string) (ethtool.EthtoolCmd, error) { return res, err } -func NewEthtoolTestCollector(logger log.Logger) (Collector, error) { - collector, err := makeEthtoolCollector(logger) +func NewEthtoolTestCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { + collector, err := makeEthtoolCollector(config, logger) collector.ethtool = &EthtoolFixture{ fixturePath: "fixtures/ethtool/", } @@ -367,12 +367,13 @@ node_network_supported_speed_bytes{device="eth0",duplex="half",mode="10baseT"} 1 ` *sysPath = "fixtures/sys" + config := NodeCollectorConfig{} logger := log.NewLogfmtLogger(os.Stderr) - collector, err := NewEthtoolTestCollector(logger) + collector, err := NewEthtoolTestCollector(config, logger) if err != nil { panic(err) } - c, err := NewTestEthtoolCollector(logger) + c, err := NewTestEthtoolCollector(config, logger) if err != nil { t.Fatal(err) } diff --git a/collector/exec_bsd.go b/collector/exec_bsd.go index 87cb3cee..06e31865 100644 --- a/collector/exec_bsd.go +++ b/collector/exec_bsd.go @@ -32,7 +32,7 @@ func init() { } // NewExecCollector returns a new Collector exposing system execution statistics. -func NewExecCollector(logger log.Logger) (Collector, error) { +func NewExecCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { // From sys/vm/vm_meter.c: // All are of type CTLTYPE_UINT. // diff --git a/collector/fibrechannel_linux.go b/collector/fibrechannel_linux.go index 3993b277..26f5514d 100644 --- a/collector/fibrechannel_linux.go +++ b/collector/fibrechannel_linux.go @@ -40,7 +40,7 @@ func init() { } // NewFibreChannelCollector returns a new Collector exposing FibreChannel stats. -func NewFibreChannelCollector(logger log.Logger) (Collector, error) { +func NewFibreChannelCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { var i fibrechannelCollector var err error diff --git a/collector/filefd_linux.go b/collector/filefd_linux.go index 55069d6c..8ed39770 100644 --- a/collector/filefd_linux.go +++ b/collector/filefd_linux.go @@ -40,7 +40,7 @@ func init() { } // NewFileFDStatCollector returns a new Collector exposing file-nr stats. -func NewFileFDStatCollector(logger log.Logger) (Collector, error) { +func NewFileFDStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &fileFDStatCollector{logger}, nil } diff --git a/collector/filesystem_common.go b/collector/filesystem_common.go index b5473c37..80eb94c8 100644 --- a/collector/filesystem_common.go +++ b/collector/filesystem_common.go @@ -88,7 +88,7 @@ func init() { } // NewFilesystemCollector returns a new Collector exposing filesystems stats. -func NewFilesystemCollector(logger log.Logger) (Collector, error) { +func NewFilesystemCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { if *oldMountPointsExcluded != "" { if !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") diff --git a/collector/hwmon_linux.go b/collector/hwmon_linux.go index 3ad469a6..88d6b863 100644 --- a/collector/hwmon_linux.go +++ b/collector/hwmon_linux.go @@ -57,7 +57,7 @@ type hwMonCollector struct { // NewHwMonCollector returns a new Collector exposing /sys/class/hwmon stats // (similar to lm-sensors). -func NewHwMonCollector(logger log.Logger) (Collector, error) { +func NewHwMonCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &hwMonCollector{ logger: logger, diff --git a/collector/infiniband_linux.go b/collector/infiniband_linux.go index 3afb7c45..485a49d1 100644 --- a/collector/infiniband_linux.go +++ b/collector/infiniband_linux.go @@ -40,7 +40,7 @@ func init() { } // NewInfiniBandCollector returns a new Collector exposing InfiniBand stats. -func NewInfiniBandCollector(logger log.Logger) (Collector, error) { +func NewInfiniBandCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { var i infinibandCollector var err error diff --git a/collector/interrupts_common.go b/collector/interrupts_common.go index eea703f9..e54947a6 100644 --- a/collector/interrupts_common.go +++ b/collector/interrupts_common.go @@ -32,7 +32,7 @@ func init() { } // NewInterruptsCollector returns a new Collector exposing interrupts stats. -func NewInterruptsCollector(logger log.Logger) (Collector, error) { +func NewInterruptsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &interruptsCollector{ desc: typedDesc{prometheus.NewDesc( namespace+"_interrupts_total", diff --git a/collector/ipvs_linux.go b/collector/ipvs_linux.go index 63a3a1a8..0c74bea5 100644 --- a/collector/ipvs_linux.go +++ b/collector/ipvs_linux.go @@ -73,11 +73,11 @@ func init() { // NewIPVSCollector sets up a new collector for IPVS metrics. It accepts the // "procfs" config parameter to override the default proc location (/proc). -func NewIPVSCollector(logger log.Logger) (Collector, error) { - return newIPVSCollector(logger) +func NewIPVSCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { + return newIPVSCollector(config, logger) } -func newIPVSCollector(logger log.Logger) (*ipvsCollector, error) { +func newIPVSCollector(config NodeCollectorConfig, logger log.Logger) (*ipvsCollector, error) { var ( c ipvsCollector err error diff --git a/collector/ipvs_linux_test.go b/collector/ipvs_linux_test.go index 8c43ad8b..82588532 100644 --- a/collector/ipvs_linux_test.go +++ b/collector/ipvs_linux_test.go @@ -111,7 +111,7 @@ func TestIPVSCollector(t *testing.T) { if _, err := kingpin.CommandLine.Parse(args); err != nil { t.Fatal(err) } - collector, err := newIPVSCollector(log.NewNopLogger()) + collector, err := newIPVSCollector(NodeCollectorConfig{}, log.NewNopLogger()) if err != nil { if test.err == nil { t.Fatal(err) @@ -179,7 +179,7 @@ func TestIPVSCollectorResponse(t *testing.T) { if _, err := kingpin.CommandLine.Parse(args); err != nil { t.Fatal(err) } - collector, err := NewIPVSCollector(log.NewNopLogger()) + collector, err := NewIPVSCollector(NodeCollectorConfig{}, log.NewNopLogger()) if err != nil { t.Fatal(err) } diff --git a/collector/ksmd_linux.go b/collector/ksmd_linux.go index 6d4142ae..d084298c 100644 --- a/collector/ksmd_linux.go +++ b/collector/ksmd_linux.go @@ -50,7 +50,7 @@ func getCanonicalMetricName(filename string) string { } // NewKsmdCollector returns a new Collector exposing kernel/system statistics. -func NewKsmdCollector(logger log.Logger) (Collector, error) { +func NewKsmdCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { subsystem := "ksmd" descs := make(map[string]*prometheus.Desc) diff --git a/collector/lnstat_linux.go b/collector/lnstat_linux.go index b3c90dc0..d2ac8cd8 100644 --- a/collector/lnstat_linux.go +++ b/collector/lnstat_linux.go @@ -33,7 +33,7 @@ func init() { registerCollector("lnstat", defaultDisabled, NewLnstatCollector) } -func NewLnstatCollector(logger log.Logger) (Collector, error) { +func NewLnstatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &lnstatCollector{logger}, nil } diff --git a/collector/loadavg.go b/collector/loadavg.go index cb7b2cb1..15c5a109 100644 --- a/collector/loadavg.go +++ b/collector/loadavg.go @@ -35,7 +35,7 @@ func init() { } // NewLoadavgCollector returns a new Collector exposing load average stats. -func NewLoadavgCollector(logger log.Logger) (Collector, error) { +func NewLoadavgCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &loadavgCollector{ metric: []typedDesc{ {prometheus.NewDesc(namespace+"_load1", "1m load average.", nil, nil), prometheus.GaugeValue}, diff --git a/collector/logind_linux.go b/collector/logind_linux.go index de5b0d18..0a087fbf 100644 --- a/collector/logind_linux.go +++ b/collector/logind_linux.go @@ -86,7 +86,7 @@ func init() { } // NewLogindCollector returns a new Collector exposing logind statistics. -func NewLogindCollector(logger log.Logger) (Collector, error) { +func NewLogindCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &logindCollector{logger}, nil } diff --git a/collector/mdadm_linux.go b/collector/mdadm_linux.go index 89c56b75..5035276e 100644 --- a/collector/mdadm_linux.go +++ b/collector/mdadm_linux.go @@ -36,7 +36,7 @@ func init() { } // NewMdadmCollector returns a new Collector exposing raid statistics. -func NewMdadmCollector(logger log.Logger) (Collector, error) { +func NewMdadmCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &mdadmCollector{logger}, nil } diff --git a/collector/meminfo.go b/collector/meminfo.go index b59337dd..0950a488 100644 --- a/collector/meminfo.go +++ b/collector/meminfo.go @@ -39,7 +39,7 @@ func init() { } // NewMeminfoCollector returns a new Collector exposing memory stats. -func NewMeminfoCollector(logger log.Logger) (Collector, error) { +func NewMeminfoCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &meminfoCollector{logger}, nil } diff --git a/collector/meminfo_numa_linux.go b/collector/meminfo_numa_linux.go index 5ce08e99..f58e5c9a 100644 --- a/collector/meminfo_numa_linux.go +++ b/collector/meminfo_numa_linux.go @@ -53,7 +53,7 @@ func init() { } // NewMeminfoNumaCollector returns a new Collector exposing memory stats. -func NewMeminfoNumaCollector(logger log.Logger) (Collector, error) { +func NewMeminfoNumaCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &meminfoNumaCollector{ metricDescs: map[string]*prometheus.Desc{}, logger: logger, diff --git a/collector/memory_bsd.go b/collector/memory_bsd.go index 6af9d8aa..6189a141 100644 --- a/collector/memory_bsd.go +++ b/collector/memory_bsd.go @@ -41,7 +41,7 @@ func init() { } // NewMemoryCollector returns a new Collector exposing memory stats. -func NewMemoryCollector(logger log.Logger) (Collector, error) { +func NewMemoryCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { tmp32, err := unix.SysctlUint32("vm.stats.vm.v_page_size") if err != nil { return nil, fmt.Errorf("sysctl(vm.stats.vm.v_page_size) failed: %w", err) diff --git a/collector/mountstats_linux.go b/collector/mountstats_linux.go index 9dea6fad..7267cccc 100644 --- a/collector/mountstats_linux.go +++ b/collector/mountstats_linux.go @@ -111,7 +111,7 @@ func init() { } // NewMountStatsCollector returns a new Collector exposing NFS statistics. -func NewMountStatsCollector(logger log.Logger) (Collector, error) { +func NewMountStatsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/netclass_linux.go b/collector/netclass_linux.go index 5d9324cd..3c5bd0cd 100644 --- a/collector/netclass_linux.go +++ b/collector/netclass_linux.go @@ -49,7 +49,7 @@ func init() { } // NewNetClassCollector returns a new Collector exposing network class stats. -func NewNetClassCollector(logger log.Logger) (Collector, error) { +func NewNetClassCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := sysfs.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) diff --git a/collector/netdev_common.go b/collector/netdev_common.go index 089f1e58..e9a25b07 100644 --- a/collector/netdev_common.go +++ b/collector/netdev_common.go @@ -54,7 +54,7 @@ func init() { } // NewNetDevCollector returns a new Collector exposing network device stats. -func NewNetDevCollector(logger log.Logger) (Collector, error) { +func NewNetDevCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { if *oldNetdevDeviceInclude != "" { if *netdevDeviceInclude == "" { level.Warn(logger).Log("msg", "--collector.netdev.device-whitelist is DEPRECATED and will be removed in 2.0.0, use --collector.netdev.device-include") diff --git a/collector/netisr_freebsd.go b/collector/netisr_freebsd.go index 4ac10dae..d8e5dd35 100644 --- a/collector/netisr_freebsd.go +++ b/collector/netisr_freebsd.go @@ -36,7 +36,7 @@ func init() { registerCollector("netisr", defaultEnabled, NewNetisrCollector) } -func NewNetisrCollector(logger log.Logger) (Collector, error) { +func NewNetisrCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &netisrCollector{ sysctls: []bsdSysctl{ { diff --git a/collector/netstat_linux.go b/collector/netstat_linux.go index 9115ef57..952aed4c 100644 --- a/collector/netstat_linux.go +++ b/collector/netstat_linux.go @@ -50,7 +50,7 @@ func init() { // NewNetStatCollector takes and returns // a new Collector exposing network stats. -func NewNetStatCollector(logger log.Logger) (Collector, error) { +func NewNetStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { pattern := regexp.MustCompile(*netStatFields) return &netStatCollector{ fieldPattern: pattern, diff --git a/collector/network_route_linux.go b/collector/network_route_linux.go index c77e1751..eccb9d7d 100644 --- a/collector/network_route_linux.go +++ b/collector/network_route_linux.go @@ -38,7 +38,7 @@ func init() { } // NewNetworkRouteCollector returns a new Collector exposing systemd statistics. -func NewNetworkRouteCollector(logger log.Logger) (Collector, error) { +func NewNetworkRouteCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { const subsystem = "network" routeInfoDesc := prometheus.NewDesc( diff --git a/collector/nfs_linux.go b/collector/nfs_linux.go index e08acdba..6c0893c3 100644 --- a/collector/nfs_linux.go +++ b/collector/nfs_linux.go @@ -48,7 +48,7 @@ func init() { } // NewNfsCollector returns a new Collector exposing NFS statistics. -func NewNfsCollector(logger log.Logger) (Collector, error) { +func NewNfsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := nfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/nfsd_linux.go b/collector/nfsd_linux.go index faa6c960..551d848d 100644 --- a/collector/nfsd_linux.go +++ b/collector/nfsd_linux.go @@ -44,7 +44,7 @@ const ( ) // NewNFSdCollector returns a new Collector exposing /proc/net/rpc/nfsd statistics. -func NewNFSdCollector(logger log.Logger) (Collector, error) { +func NewNFSdCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := nfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/ntp.go b/collector/ntp.go index 8b8db8a9..c4b2d55a 100644 --- a/collector/ntp.go +++ b/collector/ntp.go @@ -62,7 +62,7 @@ func init() { // Default definition of "local" is: // - collector.ntp.server address is a loopback address (or collector.ntp.server-is-mine flag is turned on) // - the server is reachable with outgoin IP_TTL = 1 -func NewNtpCollector(logger log.Logger) (Collector, error) { +func NewNtpCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { ipaddr := net.ParseIP(*ntpServer) if !*ntpServerIsLocal && (ipaddr == nil || !ipaddr.IsLoopback()) { return nil, fmt.Errorf("only IP address of local NTP server is valid for --collector.ntp.server") diff --git a/collector/nvme_linux.go b/collector/nvme_linux.go index 15af5d75..218f7422 100644 --- a/collector/nvme_linux.go +++ b/collector/nvme_linux.go @@ -37,7 +37,7 @@ func init() { } // NewNVMeCollector returns a new Collector exposing NVMe stats. -func NewNVMeCollector(logger log.Logger) (Collector, error) { +func NewNVMeCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := sysfs.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) diff --git a/collector/os_release.go b/collector/os_release.go index 14a025a4..02bbe64a 100644 --- a/collector/os_release.go +++ b/collector/os_release.go @@ -81,7 +81,7 @@ func init() { } // NewOSCollector returns a new Collector exposing os-release information. -func NewOSCollector(logger log.Logger) (Collector, error) { +func NewOSCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &osReleaseCollector{ logger: logger, infoDesc: prometheus.NewDesc( diff --git a/collector/os_release_test.go b/collector/os_release_test.go index 46838f15..87451aa3 100644 --- a/collector/os_release_test.go +++ b/collector/os_release_test.go @@ -87,7 +87,7 @@ func TestUpdateStruct(t *testing.T) { } wantedVersion := 20.04 - collector, err := NewOSCollector(log.NewNopLogger()) + collector, err := NewOSCollector(NodeCollectorConfig{}, log.NewNopLogger()) if err != nil { t.Fatal(err) } diff --git a/collector/perf_linux.go b/collector/perf_linux.go index 8934371a..c882404f 100644 --- a/collector/perf_linux.go +++ b/collector/perf_linux.go @@ -301,7 +301,7 @@ func newPerfTracepointCollector( // NewPerfCollector returns a new perf based collector, it creates a profiler // per CPU. -func NewPerfCollector(logger log.Logger) (Collector, error) { +func NewPerfCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { collector := &perfCollector{ perfHwProfilers: map[int]*perf.HardwareProfiler{}, perfSwProfilers: map[int]*perf.SoftwareProfiler{}, diff --git a/collector/perf_linux_test.go b/collector/perf_linux_test.go index c4990d77..2986be60 100644 --- a/collector/perf_linux_test.go +++ b/collector/perf_linux_test.go @@ -45,7 +45,7 @@ func canTestPerf(t *testing.T) { func TestPerfCollector(t *testing.T) { canTestPerf(t) - collector, err := NewPerfCollector(log.NewNopLogger()) + collector, err := NewPerfCollector(NodeCollectorConfig{}, log.NewNopLogger()) if err != nil { t.Fatal(err) } @@ -98,7 +98,7 @@ func TestPerfCollectorStride(t *testing.T) { } } perfCPUsFlag = &test.flag - collector, err := NewPerfCollector(log.NewNopLogger()) + collector, err := NewPerfCollector(NodeCollectorConfig{}, log.NewNopLogger()) if err != nil { t.Fatal(err) } diff --git a/collector/powersupplyclass.go b/collector/powersupplyclass.go index 7f231dac..13cb5148 100644 --- a/collector/powersupplyclass.go +++ b/collector/powersupplyclass.go @@ -40,7 +40,7 @@ func init() { registerCollector("powersupplyclass", defaultEnabled, NewPowerSupplyClassCollector) } -func NewPowerSupplyClassCollector(logger log.Logger) (Collector, error) { +func NewPowerSupplyClassCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { pattern := regexp.MustCompile(*powerSupplyClassIgnoredPowerSupplies) return &powerSupplyClassCollector{ subsystem: "power_supply", diff --git a/collector/pressure_linux.go b/collector/pressure_linux.go index ceaced7e..d63bc9d1 100644 --- a/collector/pressure_linux.go +++ b/collector/pressure_linux.go @@ -49,7 +49,7 @@ func init() { } // NewPressureStatsCollector returns a Collector exposing pressure stall information -func NewPressureStatsCollector(logger log.Logger) (Collector, error) { +func NewPressureStatsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/processes_linux.go b/collector/processes_linux.go index 798aeaeb..316ca593 100644 --- a/collector/processes_linux.go +++ b/collector/processes_linux.go @@ -47,7 +47,7 @@ func init() { } // NewProcessStatCollector returns a new Collector exposing process data read from the proc filesystem. -func NewProcessStatCollector(logger log.Logger) (Collector, error) { +func NewProcessStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/qdisc_linux.go b/collector/qdisc_linux.go index 639608df..6944d598 100644 --- a/collector/qdisc_linux.go +++ b/collector/qdisc_linux.go @@ -51,7 +51,7 @@ func init() { } // NewQdiscStatCollector returns a new Collector exposing queuing discipline statistics. -func NewQdiscStatCollector(logger log.Logger) (Collector, error) { +func NewQdiscStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { if *collectorQdiskDeviceExclude != "" && *collectorQdiskDeviceInclude != "" { return nil, fmt.Errorf("collector.qdisk.device-include and collector.qdisk.device-exclude are mutaly exclusive") } diff --git a/collector/rapl_linux.go b/collector/rapl_linux.go index 642de6c1..a908c0ec 100644 --- a/collector/rapl_linux.go +++ b/collector/rapl_linux.go @@ -47,7 +47,7 @@ var ( ) // NewRaplCollector returns a new Collector exposing RAPL metrics. -func NewRaplCollector(logger log.Logger) (Collector, error) { +func NewRaplCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := sysfs.NewFS(*sysPath) if err != nil { diff --git a/collector/runit.go b/collector/runit.go index 3cae657c..913c4833 100644 --- a/collector/runit.go +++ b/collector/runit.go @@ -39,7 +39,7 @@ func init() { } // NewRunitCollector returns a new Collector exposing runit statistics. -func NewRunitCollector(logger log.Logger) (Collector, error) { +func NewRunitCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { var ( subsystem = "service" constLabels = prometheus.Labels{"supervisor": "runit"} diff --git a/collector/schedstat_linux.go b/collector/schedstat_linux.go index e5016561..1d4d85e2 100644 --- a/collector/schedstat_linux.go +++ b/collector/schedstat_linux.go @@ -53,7 +53,7 @@ var ( ) // NewSchedstatCollector returns a new Collector exposing task scheduler statistics -func NewSchedstatCollector(logger log.Logger) (Collector, error) { +func NewSchedstatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/selinux_linux.go b/collector/selinux_linux.go index 71a3c021..fd00da56 100644 --- a/collector/selinux_linux.go +++ b/collector/selinux_linux.go @@ -34,7 +34,7 @@ func init() { } // NewSelinuxCollector returns a new Collector exposing SELinux statistics. -func NewSelinuxCollector(logger log.Logger) (Collector, error) { +func NewSelinuxCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { const subsystem = "selinux" return &selinuxCollector{ diff --git a/collector/slabinfo_linux.go b/collector/slabinfo_linux.go index 8c032cbb..ad7667a8 100644 --- a/collector/slabinfo_linux.go +++ b/collector/slabinfo_linux.go @@ -35,7 +35,7 @@ func init() { registerCollector("slabinfo", defaultDisabled, NewSlabinfoCollector) } -func NewSlabinfoCollector(logger log.Logger) (Collector, error) { +func NewSlabinfoCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/sockstat_linux.go b/collector/sockstat_linux.go index 480c83b1..7ae194e0 100644 --- a/collector/sockstat_linux.go +++ b/collector/sockstat_linux.go @@ -43,7 +43,7 @@ func init() { } // NewSockStatCollector returns a new Collector exposing socket stats. -func NewSockStatCollector(logger log.Logger) (Collector, error) { +func NewSockStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &sockStatCollector{logger}, nil } diff --git a/collector/softirqs_common.go b/collector/softirqs_common.go index 08ef780f..7306b7aa 100644 --- a/collector/softirqs_common.go +++ b/collector/softirqs_common.go @@ -34,7 +34,7 @@ func init() { } // NewSoftirqsCollector returns a new Collector exposing softirq stats. -func NewSoftirqsCollector(logger log.Logger) (Collector, error) { +func NewSoftirqsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { desc := typedDesc{prometheus.NewDesc( namespace+"_softirqs_functions_total", "Softirq counts per CPU.", diff --git a/collector/softnet_linux.go b/collector/softnet_linux.go index 42d47780..654226d9 100644 --- a/collector/softnet_linux.go +++ b/collector/softnet_linux.go @@ -46,7 +46,7 @@ func init() { } // NewSoftnetCollector returns a new Collector exposing softnet metrics. -func NewSoftnetCollector(logger log.Logger) (Collector, error) { +func NewSoftnetCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/stat_linux.go b/collector/stat_linux.go index 9974ae7a..2e75a5fd 100644 --- a/collector/stat_linux.go +++ b/collector/stat_linux.go @@ -44,7 +44,7 @@ func init() { } // NewStatCollector returns a new Collector exposing kernel/system statistics. -func NewStatCollector(logger log.Logger) (Collector, error) { +func NewStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/supervisord.go b/collector/supervisord.go index 9b517f0a..d53a9fcb 100644 --- a/collector/supervisord.go +++ b/collector/supervisord.go @@ -49,7 +49,7 @@ func init() { } // NewSupervisordCollector returns a new Collector exposing supervisord statistics. -func NewSupervisordCollector(logger log.Logger) (Collector, error) { +func NewSupervisordCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { var ( subsystem = "supervisord" labelNames = []string{"name", "group"} diff --git a/collector/sysctl_linux.go b/collector/sysctl_linux.go index c14341db..bc1a3307 100644 --- a/collector/sysctl_linux.go +++ b/collector/sysctl_linux.go @@ -41,7 +41,7 @@ func init() { registerCollector("sysctl", defaultDisabled, NewSysctlCollector) } -func NewSysctlCollector(logger log.Logger) (Collector, error) { +func NewSysctlCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) diff --git a/collector/systemd_linux.go b/collector/systemd_linux.go index 33232511..4ef3fbd2 100644 --- a/collector/systemd_linux.go +++ b/collector/systemd_linux.go @@ -88,7 +88,7 @@ func init() { } // NewSystemdCollector returns a new Collector exposing systemd statistics. -func NewSystemdCollector(logger log.Logger) (Collector, error) { +func NewSystemdCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { const subsystem = "systemd" unitDesc := prometheus.NewDesc( diff --git a/collector/systemd_linux_test.go b/collector/systemd_linux_test.go index 86573d2a..9fe2147b 100644 --- a/collector/systemd_linux_test.go +++ b/collector/systemd_linux_test.go @@ -100,7 +100,7 @@ func TestSystemdIgnoreFilter(t *testing.T) { } func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) { logger := log.NewNopLogger() - c, err := NewSystemdCollector(logger) + c, err := NewSystemdCollector(NodeCollectorConfig{}, logger) if err != nil { t.Fatal(err) } diff --git a/collector/tapestats_linux.go b/collector/tapestats_linux.go index 264c2210..b0a91014 100644 --- a/collector/tapestats_linux.go +++ b/collector/tapestats_linux.go @@ -54,7 +54,7 @@ func init() { // NewTapestatsCollector returns a new Collector exposing tape device stats. // Docs from https://www.kernel.org/doc/html/latest/scsi/st.html#sysfs-and-statistics-for-tape-devices -func NewTapestatsCollector(logger log.Logger) (Collector, error) { +func NewTapestatsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { var tapeLabelNames = []string{"device"} fs, err := sysfs.NewFS(*sysPath) diff --git a/collector/tcpstat_linux.go b/collector/tcpstat_linux.go index 99e33bc6..f18058d6 100644 --- a/collector/tcpstat_linux.go +++ b/collector/tcpstat_linux.go @@ -68,7 +68,7 @@ func init() { } // NewTCPStatCollector returns a new Collector exposing network stats. -func NewTCPStatCollector(logger log.Logger) (Collector, error) { +func NewTCPStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &tcpStatCollector{ desc: typedDesc{prometheus.NewDesc( prometheus.BuildFQName(namespace, "tcp", "connection_states"), diff --git a/collector/textfile.go b/collector/textfile.go index 48133f7c..3d944727 100644 --- a/collector/textfile.go +++ b/collector/textfile.go @@ -55,7 +55,7 @@ func init() { // NewTextFileCollector returns a new Collector exposing metrics read from files // in the given textfile directory. -func NewTextFileCollector(logger log.Logger) (Collector, error) { +func NewTextFileCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { c := &textFileCollector{ path: *textFileDirectory, logger: logger, diff --git a/collector/thermal_darwin.go b/collector/thermal_darwin.go index 25673dcc..d7330970 100644 --- a/collector/thermal_darwin.go +++ b/collector/thermal_darwin.go @@ -67,7 +67,7 @@ func init() { } // NewThermCollector returns a new Collector exposing current CPU power levels. -func NewThermCollector(logger log.Logger) (Collector, error) { +func NewThermCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &thermCollector{ cpuSchedulerLimit: typedDesc{ desc: prometheus.NewDesc( diff --git a/collector/thermal_zone_linux.go b/collector/thermal_zone_linux.go index 6eff2732..dde39203 100644 --- a/collector/thermal_zone_linux.go +++ b/collector/thermal_zone_linux.go @@ -43,7 +43,7 @@ func init() { } // NewThermalZoneCollector returns a new Collector exposing kernel/system statistics. -func NewThermalZoneCollector(logger log.Logger) (Collector, error) { +func NewThermalZoneCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := sysfs.NewFS(*sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) diff --git a/collector/time.go b/collector/time.go index 31a6e74e..a245eeb5 100644 --- a/collector/time.go +++ b/collector/time.go @@ -38,7 +38,7 @@ func init() { // NewTimeCollector returns a new Collector exposing the current system time in // seconds since epoch. -func NewTimeCollector(logger log.Logger) (Collector, error) { +func NewTimeCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { const subsystem = "time" return &timeCollector{ now: typedDesc{prometheus.NewDesc( diff --git a/collector/timex.go b/collector/timex.go index c444d3ea..5fa6014e 100644 --- a/collector/timex.go +++ b/collector/timex.go @@ -66,7 +66,7 @@ func init() { } // NewTimexCollector returns a new Collector exposing adjtime(3) stats. -func NewTimexCollector(logger log.Logger) (Collector, error) { +func NewTimexCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { const subsystem = "timex" return &timexCollector{ diff --git a/collector/udp_queues_linux.go b/collector/udp_queues_linux.go index 3fac8691..c58634d7 100644 --- a/collector/udp_queues_linux.go +++ b/collector/udp_queues_linux.go @@ -40,7 +40,7 @@ func init() { } // NewUDPqueuesCollector returns a new Collector exposing network udp queued bytes. -func NewUDPqueuesCollector(logger log.Logger) (Collector, error) { +func NewUDPqueuesCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/collector/uname.go b/collector/uname.go index 76e66b7e..e03101db 100644 --- a/collector/uname.go +++ b/collector/uname.go @@ -53,7 +53,7 @@ func init() { } // NewUnameCollector returns new unameCollector. -func newUnameCollector(logger log.Logger) (Collector, error) { +func newUnameCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &unameCollector{logger}, nil } diff --git a/collector/vmstat_linux.go b/collector/vmstat_linux.go index cde2df5d..ac08e8bb 100644 --- a/collector/vmstat_linux.go +++ b/collector/vmstat_linux.go @@ -47,7 +47,7 @@ func init() { } // NewvmStatCollector returns a new Collector exposing vmstat stats. -func NewvmStatCollector(logger log.Logger) (Collector, error) { +func NewvmStatCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { pattern := regexp.MustCompile(*vmStatFields) return &vmStatCollector{ fieldPattern: pattern, diff --git a/collector/wifi_linux.go b/collector/wifi_linux.go index aff8eb2d..782a25eb 100644 --- a/collector/wifi_linux.go +++ b/collector/wifi_linux.go @@ -67,7 +67,7 @@ type wifiStater interface { } // NewWifiCollector returns a new Collector exposing Wifi statistics. -func NewWifiCollector(logger log.Logger) (Collector, error) { +func NewWifiCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { const ( subsystem = "wifi" ) diff --git a/collector/xfs_linux.go b/collector/xfs_linux.go index bb25acab..e24b1da4 100644 --- a/collector/xfs_linux.go +++ b/collector/xfs_linux.go @@ -35,7 +35,7 @@ func init() { } // NewXFSCollector returns a new Collector exposing XFS statistics. -func NewXFSCollector(logger log.Logger) (Collector, error) { +func NewXFSCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := xfs.NewFS(*procPath, *sysPath) if err != nil { return nil, fmt.Errorf("failed to open sysfs: %w", err) diff --git a/collector/zfs.go b/collector/zfs.go index df1a97ae..f26f9921 100644 --- a/collector/zfs.go +++ b/collector/zfs.go @@ -43,7 +43,7 @@ type zfsCollector struct { } // NewZFSCollector returns a new Collector exposing ZFS statistics. -func NewZFSCollector(logger log.Logger) (Collector, error) { +func NewZFSCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &zfsCollector{ linuxProcpathBase: "spl/kstat/zfs", linuxZpoolIoPath: "/*/io", diff --git a/collector/zfs_freebsd.go b/collector/zfs_freebsd.go index 2418b229..e02061cd 100644 --- a/collector/zfs_freebsd.go +++ b/collector/zfs_freebsd.go @@ -36,7 +36,7 @@ func init() { registerCollector("zfs", defaultEnabled, NewZfsCollector) } -func NewZfsCollector(logger log.Logger) (Collector, error) { +func NewZfsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &zfsCollector{ sysctls: []bsdSysctl{ { diff --git a/collector/zfs_solaris.go b/collector/zfs_solaris.go index 040f31db..3cc3fbb9 100644 --- a/collector/zfs_solaris.go +++ b/collector/zfs_solaris.go @@ -65,7 +65,7 @@ func init() { registerCollector("zfs", defaultEnabled, NewZfsCollector) } -func NewZfsCollector(logger log.Logger) (Collector, error) { +func NewZfsCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { return &zfsCollector{ abdstatsLinearCount: prometheus.NewDesc( prometheus.BuildFQName(namespace, zfsCollectorSubsystem, "abdstats_linear_count_total"), diff --git a/collector/zoneinfo_linux.go b/collector/zoneinfo_linux.go index 8f7e35d9..9ea23558 100644 --- a/collector/zoneinfo_linux.go +++ b/collector/zoneinfo_linux.go @@ -37,7 +37,7 @@ func init() { } // NewZoneinfoCollector returns a new Collector exposing zone stats. -func NewZoneinfoCollector(logger log.Logger) (Collector, error) { +func NewZoneinfoCollector(config NodeCollectorConfig, logger log.Logger) (Collector, error) { fs, err := procfs.NewFS(*procPath) if err != nil { return nil, fmt.Errorf("failed to open procfs: %w", err) diff --git a/kingpinconfig/flags.go b/kingpinconfig/flags.go new file mode 100644 index 00000000..6c6b3e1c --- /dev/null +++ b/kingpinconfig/flags.go @@ -0,0 +1,28 @@ +// Copyright 2023 The Prometheus 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. + +package kingpinconfig + +import ( + "github.com/alecthomas/kingpin/v2" + "github.com/prometheus/node_exporter/collector" +) + +func AddFlags(a *kingpin.Application) collector.NodeCollectorConfig { + config := collector.NodeCollectorConfig{} + + config.Arp.DeviceInclude = a.Flag("collector.arp.device-include", "Regexp of arp devices to include (mutually exclusive to device-exclude).").String() + config.Arp.DeviceExclude = a.Flag("collector.arp.device-exclude", "Regexp of arp devices to exclude (mutually exclusive to device-include).").String() + + return config +} diff --git a/node_exporter.go b/node_exporter.go index 709810a4..612358f5 100644 --- a/node_exporter.go +++ b/node_exporter.go @@ -36,6 +36,7 @@ import ( "github.com/prometheus/exporter-toolkit/web" "github.com/prometheus/exporter-toolkit/web/kingpinflag" "github.com/prometheus/node_exporter/collector" + "github.com/prometheus/node_exporter/kingpinconfig" ) // handler wraps an unfiltered http.Handler but uses a filtered handler, @@ -48,14 +49,16 @@ type handler struct { exporterMetricsRegistry *prometheus.Registry includeExporterMetrics bool maxRequests int + collectorConfig collector.NodeCollectorConfig logger log.Logger } -func newHandler(includeExporterMetrics bool, maxRequests int, logger log.Logger) *handler { +func newHandler(includeExporterMetrics bool, maxRequests int, collectorConfig collector.NodeCollectorConfig, logger log.Logger) *handler { h := &handler{ exporterMetricsRegistry: prometheus.NewRegistry(), includeExporterMetrics: includeExporterMetrics, maxRequests: maxRequests, + collectorConfig: collectorConfig, logger: logger, } if h.includeExporterMetrics { @@ -99,7 +102,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // (in which case it will log all the collectors enabled via command-line // flags). func (h *handler) innerHandler(filters ...string) (http.Handler, error) { - nc, err := collector.NewNodeCollector(h.logger, filters...) + nc, err := collector.NewNodeCollector(h.collectorConfig, h.logger, filters...) if err != nil { return nil, fmt.Errorf("couldn't create collector: %s", err) } @@ -163,7 +166,8 @@ func main() { maxProcs = kingpin.Flag( "runtime.gomaxprocs", "The target number of CPUs Go will run on (GOMAXPROCS)", ).Envar("GOMAXPROCS").Default("1").Int() - toolkitFlags = kingpinflag.AddFlags(kingpin.CommandLine, ":9100") + toolkitFlags = kingpinflag.AddFlags(kingpin.CommandLine, ":9100") + collectorConfig = kingpinconfig.AddFlags(kingpin.CommandLine) ) promlogConfig := &promlog.Config{} @@ -185,7 +189,7 @@ func main() { runtime.GOMAXPROCS(*maxProcs) level.Debug(logger).Log("msg", "Go MAXPROCS", "procs", runtime.GOMAXPROCS(0)) - http.Handle(*metricsPath, newHandler(!*disableExporterMetrics, *maxRequests, logger)) + http.Handle(*metricsPath, newHandler(!*disableExporterMetrics, *maxRequests, collectorConfig, logger)) if *metricsPath != "/" { landingConfig := web.LandingConfig{ Name: "Node Exporter",