Move ignore/accept to new netDevFilter struct

Signed-off-by: Julian Kornberger <jk+github@digineo.de>
This commit is contained in:
Julian Kornberger 2019-12-02 15:16:00 +01:00
parent e51b508428
commit 2d412c6ce6
8 changed files with 110 additions and 49 deletions

View file

@ -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
}

View file

@ -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)
}

View file

@ -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
}

View file

@ -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)))
}

View file

@ -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)
}
}
}

View file

@ -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
}

View file

@ -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)
}

View file

@ -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
}