Skip to content

Commit 7790f96

Browse files
authored
Merge pull request prometheus#1743 from prometheus/superq/flags
Improve filter flag names.
2 parents 2cefe3d + 7e49b68 commit 7790f96

5 files changed

Lines changed: 86 additions & 37 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## master / unreleased
22

3+
* [CHANGE] Improve filter flag names.
34
* [CHANGE]
45
* [FEATURE]
56
* [ENHANCEMENT]

collector/filesystem_common.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"regexp"
2121

2222
"github.com/go-kit/kit/log"
23+
"github.com/go-kit/kit/log/level"
2324
"github.com/prometheus/client_golang/prometheus"
2425
"gopkg.in/alecthomas/kingpin.v2"
2526
)
@@ -70,7 +71,9 @@ func init() {
7071
// NewFilesystemCollector returns a new Collector exposing filesystems stats.
7172
func NewFilesystemCollector(logger log.Logger) (Collector, error) {
7273
subsystem := "filesystem"
74+
level.Info(logger).Log("msg", "Parsed flag --collector.filesystem.ignored-mount-points", "flag", *ignoredMountPoints)
7375
mountPointPattern := regexp.MustCompile(*ignoredMountPoints)
76+
level.Info(logger).Log("msg", "Parsed flag --collector.filesystem.ignored-fs-types", "flag", *ignoredMountPoints)
7477
filesystemsTypesPattern := regexp.MustCompile(*ignoredFSTypes)
7578

7679
sizeDesc := prometheus.NewDesc(

collector/netdev_common.go

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,24 @@ import (
2323
"strconv"
2424

2525
"github.com/go-kit/kit/log"
26+
"github.com/go-kit/kit/log/level"
2627
"github.com/prometheus/client_golang/prometheus"
2728
"gopkg.in/alecthomas/kingpin.v2"
2829
)
2930

3031
var (
31-
netdevIgnoredDevices = kingpin.Flag("collector.netdev.device-blacklist", "Regexp of net devices to blacklist (mutually exclusive to device-whitelist).").String()
32-
netdevAcceptDevices = kingpin.Flag("collector.netdev.device-whitelist", "Regexp of net devices to whitelist (mutually exclusive to device-blacklist).").String()
32+
netdevDeviceInclude = kingpin.Flag("collector.netdev.device-include", "Regexp of net devices to include (mutually exclusive to device-exclude).").String()
33+
oldNetdevDeviceInclude = kingpin.Flag("collector.netdev.device-whitelist", "DEPRECATED: Use collector.netdev.device-include").Hidden().String()
34+
netdevDeviceExclude = kingpin.Flag("collector.netdev.device-exclude", "Regexp of net devices to exclude (mutually exclusive to device-include).").String()
35+
oldNetdevDeviceExclude = kingpin.Flag("collector.netdev.device-blacklist", "DEPRECATED: Use collector.netdev.device-exclude").Hidden().String()
3336
)
3437

3538
type netDevCollector struct {
36-
subsystem string
37-
ignoredDevicesPattern *regexp.Regexp
38-
acceptDevicesPattern *regexp.Regexp
39-
metricDescs map[string]*prometheus.Desc
40-
logger log.Logger
39+
subsystem string
40+
deviceExcludePattern *regexp.Regexp
41+
deviceIncludePattern *regexp.Regexp
42+
metricDescs map[string]*prometheus.Desc
43+
logger log.Logger
4144
}
4245

4346
func init() {
@@ -46,31 +49,51 @@ func init() {
4649

4750
// NewNetDevCollector returns a new Collector exposing network device stats.
4851
func NewNetDevCollector(logger log.Logger) (Collector, error) {
49-
if *netdevIgnoredDevices != "" && *netdevAcceptDevices != "" {
50-
return nil, errors.New("device-blacklist & accept-devices are mutually exclusive")
52+
if *oldNetdevDeviceInclude != "" {
53+
if *netdevDeviceInclude == "" {
54+
level.Warn(logger).Log("msg", "--collector.netdev.device-whitelist is DEPRECATED and will be removed in 2.0.0, use --collector.netdev.device-include")
55+
*netdevDeviceInclude = *oldNetdevDeviceInclude
56+
} else {
57+
return nil, errors.New("--collector.netdev.device-whitelist and --collector.netdev.device-include are mutually exclusive")
58+
}
59+
}
60+
61+
if *oldNetdevDeviceExclude != "" {
62+
if *netdevDeviceExclude == "" {
63+
level.Warn(logger).Log("msg", "--collector.netdev.device-blacklist is DEPRECATED and will be removed in 2.0.0, use --collector.netdev.device-exclude")
64+
*netdevDeviceExclude = *oldNetdevDeviceExclude
65+
} else {
66+
return nil, errors.New("--collector.netdev.device-blacklist and --collector.netdev.device-exclude are mutually exclusive")
67+
}
68+
}
69+
70+
if *netdevDeviceExclude != "" && *netdevDeviceInclude != "" {
71+
return nil, errors.New("device-exclude & device-include are mutually exclusive")
5172
}
5273

53-
var ignorePattern *regexp.Regexp
54-
if *netdevIgnoredDevices != "" {
55-
ignorePattern = regexp.MustCompile(*netdevIgnoredDevices)
74+
var excludePattern *regexp.Regexp
75+
if *netdevDeviceExclude != "" {
76+
level.Info(logger).Log("msg", "Parsed flag --collector.netdev.device-exclude", "flag", *netdevDeviceExclude)
77+
excludePattern = regexp.MustCompile(*netdevDeviceExclude)
5678
}
5779

58-
var acceptPattern *regexp.Regexp
59-
if *netdevAcceptDevices != "" {
60-
acceptPattern = regexp.MustCompile(*netdevAcceptDevices)
80+
var includePattern *regexp.Regexp
81+
if *netdevDeviceInclude != "" {
82+
level.Info(logger).Log("msg", "Parsed Flag --collector.netdev.device-include", "flag", *netdevDeviceInclude)
83+
includePattern = regexp.MustCompile(*netdevDeviceInclude)
6184
}
6285

6386
return &netDevCollector{
64-
subsystem: "network",
65-
ignoredDevicesPattern: ignorePattern,
66-
acceptDevicesPattern: acceptPattern,
67-
metricDescs: map[string]*prometheus.Desc{},
68-
logger: logger,
87+
subsystem: "network",
88+
deviceExcludePattern: excludePattern,
89+
deviceIncludePattern: includePattern,
90+
metricDescs: map[string]*prometheus.Desc{},
91+
logger: logger,
6992
}, nil
7093
}
7194

7295
func (c *netDevCollector) Update(ch chan<- prometheus.Metric) error {
73-
netDev, err := getNetDevStats(c.ignoredDevicesPattern, c.acceptDevicesPattern, c.logger)
96+
netDev, err := getNetDevStats(c.deviceExcludePattern, c.deviceIncludePattern, c.logger)
7497
if err != nil {
7598
return fmt.Errorf("couldn't get netstats: %s", err)
7699
}

collector/systemd_linux.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package collector
1717

1818
import (
19+
"errors"
1920
"fmt"
2021
"math"
2122
"regexp"
@@ -39,8 +40,10 @@ const (
3940
)
4041

4142
var (
42-
unitWhitelist = kingpin.Flag("collector.systemd.unit-whitelist", "Regexp of systemd units to whitelist. Units must both match whitelist and not match blacklist to be included.").Default(".+").String()
43-
unitBlacklist = kingpin.Flag("collector.systemd.unit-blacklist", "Regexp of systemd units to blacklist. Units must both match whitelist and not match blacklist to be included.").Default(".+\\.(automount|device|mount|scope|slice)").String()
43+
unitInclude = kingpin.Flag("collector.systemd.unit-include", "Regexp of systemd units to include. Units must both match include and not match exclude to be included.").Default(".+").String()
44+
oldUnitInclude = kingpin.Flag("collector.systemd.unit-whitelist", "DEPRECATED: Use --collector.systemd.unit-include").Hidden().String()
45+
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)").String()
46+
oldUnitExclude = kingpin.Flag("collector.systemd.unit-blacklist", "DEPRECATED: Use collector.systemd.unit-exclude").Hidden().String()
4447
systemdPrivate = 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()
4548
enableTaskMetrics = kingpin.Flag("collector.systemd.enable-task-metrics", "Enables service unit tasks metrics unit_tasks_current and unit_tasks_max").Bool()
4649
enableRestartsMetrics = kingpin.Flag("collector.systemd.enable-restarts-metrics", "Enables service unit metric service_restart_total").Bool()
@@ -61,8 +64,8 @@ type systemdCollector struct {
6164
socketRefusedConnectionsDesc *prometheus.Desc
6265
systemdVersionDesc *prometheus.Desc
6366
systemdVersion int
64-
unitWhitelistPattern *regexp.Regexp
65-
unitBlacklistPattern *regexp.Regexp
67+
unitIncludePattern *regexp.Regexp
68+
unitExcludePattern *regexp.Regexp
6669
logger log.Logger
6770
}
6871

@@ -118,8 +121,27 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) {
118121
systemdVersionDesc := prometheus.NewDesc(
119122
prometheus.BuildFQName(namespace, subsystem, "version"),
120123
"Detected systemd version", []string{}, nil)
121-
unitWhitelistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitWhitelist))
122-
unitBlacklistPattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitBlacklist))
124+
125+
if *oldUnitExclude != "" {
126+
if *unitExclude == "" {
127+
level.Warn(logger).Log("msg", "--collector.systemd.unit-blacklist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-exclude")
128+
*unitExclude = *oldUnitExclude
129+
} else {
130+
return nil, errors.New("--collector.systemd.unit-blacklist and --collector.systemd.unit-exclude are mutually exclusive")
131+
}
132+
}
133+
if *oldUnitInclude != "" {
134+
if *unitInclude == "" {
135+
level.Warn(logger).Log("msg", "--collector.systemd.unit-whitelist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-include")
136+
*unitInclude = *oldUnitInclude
137+
} else {
138+
return nil, errors.New("--collector.systemd.unit-whitelist and --collector.systemd.unit-include are mutually exclusive")
139+
}
140+
}
141+
level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-include", "flag", *unitInclude)
142+
unitIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitInclude))
143+
level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-exclude", "flag", *unitExclude)
144+
unitExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitExclude))
123145

124146
systemdVersion := getSystemdVersion(logger)
125147
if systemdVersion < minSystemdVersionSystemState {
@@ -141,8 +163,8 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) {
141163
socketRefusedConnectionsDesc: socketRefusedConnectionsDesc,
142164
systemdVersionDesc: systemdVersionDesc,
143165
systemdVersion: systemdVersion,
144-
unitWhitelistPattern: unitWhitelistPattern,
145-
unitBlacklistPattern: unitBlacklistPattern,
166+
unitIncludePattern: unitIncludePattern,
167+
unitExcludePattern: unitExcludePattern,
146168
logger: logger,
147169
}, nil
148170
}
@@ -169,7 +191,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
169191
level.Debug(c.logger).Log("msg", "collectSummaryMetrics took", "duration_seconds", time.Since(begin).Seconds())
170192

171193
begin = time.Now()
172-
units := filterUnits(allUnits, c.unitWhitelistPattern, c.unitBlacklistPattern, c.logger)
194+
units := filterUnits(allUnits, c.unitIncludePattern, c.unitExcludePattern, c.logger)
173195
level.Debug(c.logger).Log("msg", "filterUnits took", "duration_seconds", time.Since(begin).Seconds())
174196

175197
var wg sync.WaitGroup
@@ -443,10 +465,10 @@ func summarizeUnits(units []unit) map[string]float64 {
443465
return summarized
444466
}
445467

446-
func filterUnits(units []unit, whitelistPattern, blacklistPattern *regexp.Regexp, logger log.Logger) []unit {
468+
func filterUnits(units []unit, includePattern, excludePattern *regexp.Regexp, logger log.Logger) []unit {
447469
filtered := make([]unit, 0, len(units))
448470
for _, unit := range units {
449-
if whitelistPattern.MatchString(unit.Name) && !blacklistPattern.MatchString(unit.Name) && unit.LoadState == "loaded" {
471+
if includePattern.MatchString(unit.Name) && !excludePattern.MatchString(unit.Name) && unit.LoadState == "loaded" {
450472
level.Debug(logger).Log("msg", "Adding unit", "unit", unit.Name)
451473
filtered = append(filtered, unit)
452474
} else {

collector/systemd_linux_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ func getUnitListFixtures() [][]unit {
8989

9090
func TestSystemdIgnoreFilter(t *testing.T) {
9191
fixtures := getUnitListFixtures()
92-
whitelistPattern := regexp.MustCompile("^foo$")
93-
blacklistPattern := regexp.MustCompile("^bar$")
94-
filtered := filterUnits(fixtures[0], whitelistPattern, blacklistPattern, log.NewNopLogger())
92+
includePattern := regexp.MustCompile("^foo$")
93+
excludePattern := regexp.MustCompile("^bar$")
94+
filtered := filterUnits(fixtures[0], includePattern, excludePattern, log.NewNopLogger())
9595
for _, unit := range filtered {
96-
if blacklistPattern.MatchString(unit.Name) || !whitelistPattern.MatchString(unit.Name) {
96+
if excludePattern.MatchString(unit.Name) || !includePattern.MatchString(unit.Name) {
9797
t.Error(unit.Name, "should not be in the filtered list")
9898
}
9999
}
@@ -106,7 +106,7 @@ func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) {
106106
}
107107
fixtures := getUnitListFixtures()
108108
collector := c.(*systemdCollector)
109-
filtered := filterUnits(fixtures[0], collector.unitWhitelistPattern, collector.unitBlacklistPattern, logger)
109+
filtered := filterUnits(fixtures[0], collector.unitIncludePattern, collector.unitExcludePattern, logger)
110110
// Adjust fixtures by 3 "not-found" units.
111111
if len(filtered) != len(fixtures[0])-3 {
112112
t.Error("Default filters removed units")

0 commit comments

Comments
 (0)