From 2d412c6ce6fcc2905dbe9ee66c3bdc4f95888964 Mon Sep 17 00:00:00 2001 From: Julian Kornberger Date: Mon, 2 Dec 2019 15:16:00 +0100 Subject: [PATCH] Move ignore/accept to new netDevFilter struct Signed-off-by: Julian Kornberger --- collector/netdev_bsd.go | 9 ++----- collector/netdev_common.go | 25 +++++++------------ collector/netdev_darwin.go | 9 ++----- collector/netdev_filter.go | 41 +++++++++++++++++++++++++++++++ collector/netdev_filter_test.go | 43 +++++++++++++++++++++++++++++++++ collector/netdev_linux.go | 12 +++------ collector/netdev_linux_test.go | 11 ++++++--- collector/netdev_openbsd.go | 9 ++----- 8 files changed, 110 insertions(+), 49 deletions(-) create mode 100644 collector/netdev_filter.go create mode 100644 collector/netdev_filter_test.go diff --git a/collector/netdev_bsd.go b/collector/netdev_bsd.go index fd8d9a91..43cdbe32 100644 --- a/collector/netdev_bsd.go +++ b/collector/netdev_bsd.go @@ -18,7 +18,6 @@ package collector import ( "errors" - "regexp" "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" @@ -34,7 +33,7 @@ import ( */ import "C" -func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp, logger log.Logger) (netDevStats, error) { +func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error) { netDev := netDevStats{} var ifap, ifa *C.struct_ifaddrs @@ -49,11 +48,7 @@ func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp, logger log.Log } dev := C.GoString(ifa.ifa_name) - if ignore != nil && ignore.MatchString(dev) { - level.Debug(logger).Log("msg", "Ignoring device", "device", dev) - continue - } - if accept != nil && !accept.MatchString(dev) { + if filter.ignored(dev) { level.Debug(logger).Log("msg", "Ignoring device", "device", dev) continue } diff --git a/collector/netdev_common.go b/collector/netdev_common.go index 7b54c472..6802549b 100644 --- a/collector/netdev_common.go +++ b/collector/netdev_common.go @@ -19,7 +19,6 @@ package collector import ( "errors" "fmt" - "regexp" "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" @@ -35,11 +34,10 @@ var ( ) type netDevCollector struct { - subsystem string - deviceExcludePattern *regexp.Regexp - deviceIncludePattern *regexp.Regexp - metricDescs map[string]*prometheus.Desc - logger log.Logger + subsystem string + deviceFilter netDevFilter + metricDescs map[string]*prometheus.Desc + logger log.Logger } type netDevStats map[string]map[string]uint64 @@ -72,29 +70,24 @@ func NewNetDevCollector(logger log.Logger) (Collector, error) { return nil, errors.New("device-exclude & device-include are mutually exclusive") } - var excludePattern *regexp.Regexp if *netdevDeviceExclude != "" { level.Info(logger).Log("msg", "Parsed flag --collector.netdev.device-exclude", "flag", *netdevDeviceExclude) - excludePattern = regexp.MustCompile(*netdevDeviceExclude) } - var includePattern *regexp.Regexp if *netdevDeviceInclude != "" { level.Info(logger).Log("msg", "Parsed Flag --collector.netdev.device-include", "flag", *netdevDeviceInclude) - includePattern = regexp.MustCompile(*netdevDeviceInclude) } return &netDevCollector{ - subsystem: "network", - deviceExcludePattern: excludePattern, - deviceIncludePattern: includePattern, - metricDescs: map[string]*prometheus.Desc{}, - logger: logger, + subsystem: "network", + deviceFilter: newNetDevFilter(*netdevDeviceExclude, *netdevDeviceInclude), + metricDescs: map[string]*prometheus.Desc{}, + logger: logger, }, nil } func (c *netDevCollector) Update(ch chan<- prometheus.Metric) error { - netDev, err := getNetDevStats(c.deviceExcludePattern, c.deviceIncludePattern, c.logger) + netDev, err := getNetDevStats(&c.deviceFilter, c.logger) if err != nil { return fmt.Errorf("couldn't get netstats: %w", err) } diff --git a/collector/netdev_darwin.go b/collector/netdev_darwin.go index cd1e01c7..26ad4198 100644 --- a/collector/netdev_darwin.go +++ b/collector/netdev_darwin.go @@ -20,14 +20,13 @@ import ( "encoding/binary" "fmt" "net" - "regexp" "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "golang.org/x/sys/unix" ) -func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp, logger log.Logger) (netDevStats, error) { +func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error) { netDev := netDevStats{} ifs, err := net.Interfaces() @@ -36,11 +35,7 @@ func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp, logger log.Log } for _, iface := range ifs { - if ignore != nil && ignore.MatchString(iface.Name) { - level.Debug(logger).Log("msg", "Ignoring device", "device", iface.Name) - continue - } - if accept != nil && !accept.MatchString(iface.Name) { + if filter.ignored(iface.Name) { level.Debug(logger).Log("msg", "Ignoring device", "device", iface.Name) continue } diff --git a/collector/netdev_filter.go b/collector/netdev_filter.go new file mode 100644 index 00000000..e809c499 --- /dev/null +++ b/collector/netdev_filter.go @@ -0,0 +1,41 @@ +// Copyright 2018 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 collector + +import ( + "regexp" +) + +type netDevFilter struct { + ignorePattern *regexp.Regexp + acceptPattern *regexp.Regexp +} + +func newNetDevFilter(ignoredPattern, acceptPattern string) (f netDevFilter) { + if ignoredPattern != "" { + f.ignorePattern = regexp.MustCompile(ignoredPattern) + } + + if acceptPattern != "" { + f.acceptPattern = regexp.MustCompile(acceptPattern) + } + + return +} + +// ignores returns whether the device should be ignored +func (f *netDevFilter) ignored(name string) bool { + return ((f.ignorePattern != nil && f.ignorePattern.MatchString(name)) || + (f.acceptPattern != nil && !f.acceptPattern.MatchString(name))) +} diff --git a/collector/netdev_filter_test.go b/collector/netdev_filter_test.go new file mode 100644 index 00000000..13cebbc6 --- /dev/null +++ b/collector/netdev_filter_test.go @@ -0,0 +1,43 @@ +// Copyright 2018 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 collector + +import ( + "testing" +) + +func TestNetDevFilter(t *testing.T) { + tests := []struct { + ignore string + accept string + name string + expectedResult bool + }{ + {"", "", "eth0", false}, + {"", "^💩0$", "💩0", false}, + {"", "^💩0$", "💩1", true}, + {"", "^💩0$", "veth0", true}, + {"^💩", "", "💩3", true}, + {"^💩", "", "veth0", false}, + } + + for _, test := range tests { + filter := newNetDevFilter(test.ignore, test.accept) + result := filter.ignored(test.name) + + if result != test.expectedResult { + t.Errorf("ignorePattern=%v acceptPattern=%v ifname=%v expected=%v result=%v", test.ignore, test.accept, test.name, test.expectedResult, result) + } + } +} diff --git a/collector/netdev_linux.go b/collector/netdev_linux.go index ac582729..02d0fb29 100644 --- a/collector/netdev_linux.go +++ b/collector/netdev_linux.go @@ -33,17 +33,17 @@ var ( procNetDevFieldSep = regexp.MustCompile(` +`) ) -func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp, logger log.Logger) (netDevStats, error) { +func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error) { file, err := os.Open(procFilePath("net/dev")) if err != nil { return nil, err } defer file.Close() - return parseNetDevStats(file, ignore, accept, logger) + return parseNetDevStats(file, filter, logger) } -func parseNetDevStats(r io.Reader, ignore *regexp.Regexp, accept *regexp.Regexp, logger log.Logger) (netDevStats, error) { +func parseNetDevStats(r io.Reader, filter *netDevFilter, logger log.Logger) (netDevStats, error) { scanner := bufio.NewScanner(r) scanner.Scan() // skip first header scanner.Scan() @@ -66,11 +66,7 @@ func parseNetDevStats(r io.Reader, ignore *regexp.Regexp, accept *regexp.Regexp, } dev := parts[1] - if ignore != nil && ignore.MatchString(dev) { - level.Debug(logger).Log("msg", "Ignoring device", "device", dev) - continue - } - if accept != nil && !accept.MatchString(dev) { + if filter.ignored(dev) { level.Debug(logger).Log("msg", "Ignoring device", "device", dev) continue } diff --git a/collector/netdev_linux_test.go b/collector/netdev_linux_test.go index 259bbfeb..8a341bcf 100644 --- a/collector/netdev_linux_test.go +++ b/collector/netdev_linux_test.go @@ -14,10 +14,10 @@ package collector import ( - "github.com/go-kit/kit/log" "os" - "regexp" "testing" + + "github.com/go-kit/kit/log" ) func TestNetDevStatsIgnore(t *testing.T) { @@ -27,7 +27,9 @@ func TestNetDevStatsIgnore(t *testing.T) { } defer file.Close() - netStats, err := parseNetDevStats(file, regexp.MustCompile("^veth"), nil, log.NewNopLogger()) + filter := newNetDevFilter("^veth", "") + + netStats, err := parseNetDevStats(file, &filter, log.NewNopLogger()) if err != nil { t.Fatal(err) } @@ -68,7 +70,8 @@ func TestNetDevStatsAccept(t *testing.T) { } defer file.Close() - netStats, err := parseNetDevStats(file, nil, regexp.MustCompile("^💩0$"), log.NewNopLogger()) + filter := newNetDevFilter("", "^💩0$") + netStats, err := parseNetDevStats(file, &filter, log.NewNopLogger()) if err != nil { t.Fatal(err) } diff --git a/collector/netdev_openbsd.go b/collector/netdev_openbsd.go index 87ee921f..01e22b81 100644 --- a/collector/netdev_openbsd.go +++ b/collector/netdev_openbsd.go @@ -17,7 +17,6 @@ package collector import ( "errors" - "regexp" "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" @@ -31,7 +30,7 @@ import ( */ import "C" -func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp, logger log.Logger) (netDevStats, error) { +func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error) { netDev := netDevStats{} var ifap, ifa *C.struct_ifaddrs @@ -46,11 +45,7 @@ func getNetDevStats(ignore *regexp.Regexp, accept *regexp.Regexp, logger log.Log } dev := C.GoString(ifa.ifa_name) - if ignore != nil && ignore.MatchString(dev) { - level.Debug(logger).Log("msg", "Ignoring device", "device", dev) - continue - } - if accept != nil && !accept.MatchString(dev) { + if filter.ignored(dev) { level.Debug(logger).Log("msg", "Ignoring device", "device", dev) continue }