Browse Source

Update all Include and Exclude variables to use the systemdUnit naming (#2740)

prefix.

Leave an annotation about using regexps instead of device_filter.go, so
@SuperQ doesn't need to remember everything.

Signed-off-by: Conall O'Brien <conall@conall.net>
pull/2747/head
Conall O'Brien 1 year ago committed by GitHub
parent
commit
c241ecf8bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 49
      collector/systemd_linux.go
  2. 2
      collector/systemd_linux_test.go

49
collector/systemd_linux.go

@ -42,18 +42,18 @@ const (
) )
var ( var (
unitIncludeSet bool systemdUnitIncludeSet bool
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(".+").PreAction(func(c *kingpin.ParseContext) error { systemdUnitInclude = 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(".+").PreAction(func(c *kingpin.ParseContext) error {
unitIncludeSet = true systemdUnitIncludeSet = true
return nil return nil
}).String() }).String()
oldUnitInclude = kingpin.Flag("collector.systemd.unit-whitelist", "DEPRECATED: Use --collector.systemd.unit-include").Hidden().String() oldSystemdUnitInclude = kingpin.Flag("collector.systemd.unit-whitelist", "DEPRECATED: Use --collector.systemd.unit-include").Hidden().String()
unitExcludeSet bool systemdUnitExcludeSet bool
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 { systemdUnitExclude = 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 {
unitExcludeSet = true systemdUnitExcludeSet = true
return nil return nil
}).String() }).String()
oldUnitExclude = kingpin.Flag("collector.systemd.unit-blacklist", "DEPRECATED: Use collector.systemd.unit-exclude").Hidden().String() oldSystemdUnitExclude = kingpin.Flag("collector.systemd.unit-blacklist", "DEPRECATED: Use collector.systemd.unit-exclude").Hidden().String()
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() 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()
enableTaskMetrics = kingpin.Flag("collector.systemd.enable-task-metrics", "Enables service unit tasks metrics unit_tasks_current and unit_tasks_max").Bool() enableTaskMetrics = kingpin.Flag("collector.systemd.enable-task-metrics", "Enables service unit tasks metrics unit_tasks_current and unit_tasks_max").Bool()
enableRestartsMetrics = kingpin.Flag("collector.systemd.enable-restarts-metrics", "Enables service unit metric service_restart_total").Bool() enableRestartsMetrics = kingpin.Flag("collector.systemd.enable-restarts-metrics", "Enables service unit metric service_restart_total").Bool()
@ -75,9 +75,10 @@ type systemdCollector struct {
socketCurrentConnectionsDesc *prometheus.Desc socketCurrentConnectionsDesc *prometheus.Desc
socketRefusedConnectionsDesc *prometheus.Desc socketRefusedConnectionsDesc *prometheus.Desc
systemdVersionDesc *prometheus.Desc systemdVersionDesc *prometheus.Desc
unitIncludePattern *regexp.Regexp // Use regexps for more flexability than device_filter.go allows
unitExcludePattern *regexp.Regexp systemdUnitIncludePattern *regexp.Regexp
logger log.Logger systemdUnitExcludePattern *regexp.Regexp
logger log.Logger
} }
var unitStatesName = []string{"active", "activating", "deactivating", "inactive", "failed"} var unitStatesName = []string{"active", "activating", "deactivating", "inactive", "failed"}
@ -133,26 +134,26 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) {
prometheus.BuildFQName(namespace, subsystem, "version"), prometheus.BuildFQName(namespace, subsystem, "version"),
"Detected systemd version", []string{"version"}, nil) "Detected systemd version", []string{"version"}, nil)
if *oldUnitExclude != "" { if *oldSystemdUnitExclude != "" {
if !unitExcludeSet { if !systemdUnitExcludeSet {
level.Warn(logger).Log("msg", "--collector.systemd.unit-blacklist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-exclude") level.Warn(logger).Log("msg", "--collector.systemd.unit-blacklist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-exclude")
*unitExclude = *oldUnitExclude *systemdUnitExclude = *oldSystemdUnitExclude
} else { } else {
return nil, errors.New("--collector.systemd.unit-blacklist and --collector.systemd.unit-exclude are mutually exclusive") return nil, errors.New("--collector.systemd.unit-blacklist and --collector.systemd.unit-exclude are mutually exclusive")
} }
} }
if *oldUnitInclude != "" { if *oldSystemdUnitInclude != "" {
if !unitIncludeSet { if !systemdUnitIncludeSet {
level.Warn(logger).Log("msg", "--collector.systemd.unit-whitelist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-include") level.Warn(logger).Log("msg", "--collector.systemd.unit-whitelist is DEPRECATED and will be removed in 2.0.0, use --collector.systemd.unit-include")
*unitInclude = *oldUnitInclude *systemdUnitInclude = *oldSystemdUnitInclude
} else { } else {
return nil, errors.New("--collector.systemd.unit-whitelist and --collector.systemd.unit-include are mutually exclusive") return nil, errors.New("--collector.systemd.unit-whitelist and --collector.systemd.unit-include are mutually exclusive")
} }
} }
level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-include", "flag", *unitInclude) level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-include", "flag", *systemdUnitInclude)
unitIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitInclude)) systemdUnitIncludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *systemdUnitInclude))
level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-exclude", "flag", *unitExclude) level.Info(logger).Log("msg", "Parsed flag --collector.systemd.unit-exclude", "flag", *systemdUnitExclude)
unitExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *unitExclude)) systemdUnitExcludePattern := regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *systemdUnitExclude))
return &systemdCollector{ return &systemdCollector{
unitDesc: unitDesc, unitDesc: unitDesc,
@ -167,8 +168,8 @@ func NewSystemdCollector(logger log.Logger) (Collector, error) {
socketCurrentConnectionsDesc: socketCurrentConnectionsDesc, socketCurrentConnectionsDesc: socketCurrentConnectionsDesc,
socketRefusedConnectionsDesc: socketRefusedConnectionsDesc, socketRefusedConnectionsDesc: socketRefusedConnectionsDesc,
systemdVersionDesc: systemdVersionDesc, systemdVersionDesc: systemdVersionDesc,
unitIncludePattern: unitIncludePattern, systemdUnitIncludePattern: systemdUnitIncludePattern,
unitExcludePattern: unitExcludePattern, systemdUnitExcludePattern: systemdUnitExcludePattern,
logger: logger, logger: logger,
}, nil }, nil
} }
@ -206,7 +207,7 @@ func (c *systemdCollector) Update(ch chan<- prometheus.Metric) error {
level.Debug(c.logger).Log("msg", "collectSummaryMetrics took", "duration_seconds", time.Since(begin).Seconds()) level.Debug(c.logger).Log("msg", "collectSummaryMetrics took", "duration_seconds", time.Since(begin).Seconds())
begin = time.Now() begin = time.Now()
units := filterUnits(allUnits, c.unitIncludePattern, c.unitExcludePattern, c.logger) units := filterUnits(allUnits, c.systemdUnitIncludePattern, c.systemdUnitExcludePattern, c.logger)
level.Debug(c.logger).Log("msg", "filterUnits took", "duration_seconds", time.Since(begin).Seconds()) level.Debug(c.logger).Log("msg", "filterUnits took", "duration_seconds", time.Since(begin).Seconds())
var wg sync.WaitGroup var wg sync.WaitGroup

2
collector/systemd_linux_test.go

@ -106,7 +106,7 @@ func TestSystemdIgnoreFilterDefaultKeepsAll(t *testing.T) {
} }
fixtures := getUnitListFixtures() fixtures := getUnitListFixtures()
collector := c.(*systemdCollector) collector := c.(*systemdCollector)
filtered := filterUnits(fixtures[0], collector.unitIncludePattern, collector.unitExcludePattern, logger) filtered := filterUnits(fixtures[0], collector.systemdUnitIncludePattern, collector.systemdUnitExcludePattern, logger)
// Adjust fixtures by 3 "not-found" units. // Adjust fixtures by 3 "not-found" units.
if len(filtered) != len(fixtures[0])-3 { if len(filtered) != len(fixtures[0])-3 {
t.Error("Default filters removed units") t.Error("Default filters removed units")

Loading…
Cancel
Save