From 399ce78c366238537d15005f24a36d9787558d24 Mon Sep 17 00:00:00 2001 From: Marc Tuduri Date: Wed, 27 Sep 2023 18:26:51 +0200 Subject: [PATCH] Fix defaults --- collector/diskstats_common.go | 7 +++++-- collector/filesystem_common.go | 15 +++++++++++---- collector/ipvs_linux.go | 8 +++++--- collector/ipvs_linux_test.go | 4 ++-- kingpinconfig/flags.go | 5 ++++- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/collector/diskstats_common.go b/collector/diskstats_common.go index 0b927e86..e67a915d 100644 --- a/collector/diskstats_common.go +++ b/collector/diskstats_common.go @@ -100,13 +100,16 @@ func newDiskstatsDeviceFilter(config DiskstatsDeviceFilterConfig, logger log.Log if *config.DeviceExclude != "" { level.Info(logger).Log("msg", "Parsed flag --collector.diskstats.device-exclude", "flag", *config.DeviceExclude) - } else { - *config.DeviceExclude = diskstatsDefaultIgnoredDevices } if *config.DeviceInclude != "" { level.Info(logger).Log("msg", "Parsed Flag --collector.diskstats.device-include", "flag", *config.DeviceInclude) } + if !config.DeviceExcludeSet { // use default exclude devices if flag is not set + devices := diskstatsDefaultIgnoredDevices + config.DeviceExclude = &devices + } + return newDeviceFilter(*config.DeviceExclude, *config.DeviceInclude), nil } diff --git a/collector/filesystem_common.go b/collector/filesystem_common.go index 80727818..c15df9f7 100644 --- a/collector/filesystem_common.go +++ b/collector/filesystem_common.go @@ -82,10 +82,14 @@ func NewFilesystemCollector(config *NodeCollectorConfig, logger log.Logger) (Col return nil, errors.New("--collector.filesystem.ignored-mount-points and --collector.filesystem.mount-points-exclude are mutually exclusive") } } + if *config.Filesystem.MountPointsExclude != "" { level.Info(logger).Log("msg", "Parsed flag --collector.filesystem.mount-points-exclude", "flag", *config.Filesystem.MountPointsExclude) - } else { - *config.Filesystem.MountPointsExclude = defMountPointsExcluded + } + + if !config.Filesystem.MountPointsExcludeSet { // use default mount points if flag is not set + mountPoints := defMountPointsExcluded + config.Filesystem.MountPointsExclude = &mountPoints } if *config.Filesystem.OldFSTypesExcluded != "" { @@ -96,11 +100,14 @@ func NewFilesystemCollector(config *NodeCollectorConfig, logger log.Logger) (Col return nil, errors.New("--collector.filesystem.ignored-fs-types and --collector.filesystem.fs-types-exclude are mutually exclusive") } } + if *config.Filesystem.FSTypesExclude != "" { level.Info(logger).Log("msg", "Parsed flag --collector.filesystem.fs-types-exclude", "flag", *config.Filesystem.FSTypesExclude) + } - } else { - *config.Filesystem.FSTypesExclude = defFSTypesExcluded + if !config.Filesystem.FSTypesExcludeSet { // use default fs types if flag is not set + fsTypes := defFSTypesExcluded + config.Filesystem.FSTypesExclude = &fsTypes } subsystem := "filesystem" diff --git a/collector/ipvs_linux.go b/collector/ipvs_linux.go index 266e9f80..2f266f93 100644 --- a/collector/ipvs_linux.go +++ b/collector/ipvs_linux.go @@ -70,7 +70,8 @@ func init() { } type IPVSConfig struct { - Labels *string + Labels *string + LabelsSet bool } // NewIPVSCollector sets up a new collector for IPVS metrics. It accepts the @@ -86,8 +87,9 @@ func newIPVSCollector(config *NodeCollectorConfig, logger log.Logger) (*ipvsColl subsystem = "ipvs" ) - if *config.IPVS.Labels == "" { - *config.IPVS.Labels = strings.Join(fullIpvsBackendLabels, ",") + if !config.IPVS.LabelsSet { // use default labels if flag is not set + labels := strings.Join(fullIpvsBackendLabels, ",") + config.IPVS.Labels = &labels } if c.backendLabels, err = c.parseIpvsLabels(*config.IPVS.Labels); err != nil { diff --git a/collector/ipvs_linux_test.go b/collector/ipvs_linux_test.go index 62fab681..c33c65f3 100644 --- a/collector/ipvs_linux_test.go +++ b/collector/ipvs_linux_test.go @@ -110,8 +110,8 @@ func TestIPVSCollector(t *testing.T) { } for _, test := range testcases { t.Run(test.labels, func(t *testing.T) { - config.IPVS.Labels = new(string) if test.labels != "" { + config.IPVS.LabelsSet = true config.IPVS.Labels = &test.labels } collector, err := newIPVSCollector(config, log.NewNopLogger()) @@ -179,8 +179,8 @@ func TestIPVSCollectorResponse(t *testing.T) { } for _, test := range testcases { t.Run(test.labels, func(t *testing.T) { - config.IPVS.Labels = new(string) if test.labels != "" { + config.IPVS.LabelsSet = true config.IPVS.Labels = &test.labels } collector, err := NewIPVSCollector(config, log.NewNopLogger()) diff --git a/kingpinconfig/flags.go b/kingpinconfig/flags.go index 0a543208..626678d4 100644 --- a/kingpinconfig/flags.go +++ b/kingpinconfig/flags.go @@ -83,7 +83,10 @@ func AddFlags(a *kingpin.Application) *collector.NodeCollectorConfig { config.HwMon.ChipInclude = a.Flag("collector.hwmon.chip-include", "Regexp of hwmon chip to include (mutually exclusive to device-exclude).").String() config.HwMon.ChipExclude = a.Flag("collector.hwmon.chip-exclude", "Regexp of hwmon chip to exclude (mutually exclusive to device-include).").String() - config.IPVS.Labels = a.Flag("collector.ipvs.backend-labels", "Comma separated list for IPVS backend stats labels.").String() + config.IPVS.Labels = a.Flag("collector.ipvs.backend-labels", "Comma separated list for IPVS backend stats labels.").PreAction(func(c *kingpin.ParseContext) error { + config.IPVS.LabelsSet = true + return nil + }).String() config.NetClass.IgnoredDevices = a.Flag("collector.netclass.ignored-devices", "Regexp of net devices to ignore for netclass collector.").Default("^$").String() config.NetClass.InvalidSpeed = a.Flag("collector.netclass.ignore-invalid-speed", "Ignore devices where the speed is invalid. This will be the default behavior in 2.x.").Bool()