Browse Source

Rename netDevFilter helper

Rename the network device filter to a more generic device filter.

Signed-off-by: Ben Kochie <superq@gmail.com>
pull/2378/head
Ben Kochie 3 years ago
parent
commit
1b6aaeb2e8
No known key found for this signature in database
GPG Key ID: C646B23C9E3245F1
  1. 4
      collector/arp_linux.go
  2. 6
      collector/device_filter.go
  3. 4
      collector/device_filter_test.go
  4. 4
      collector/ethtool_linux.go
  5. 2
      collector/netdev_bsd.go
  6. 4
      collector/netdev_common.go
  7. 2
      collector/netdev_darwin.go
  8. 4
      collector/netdev_linux.go
  9. 4
      collector/netdev_linux_test.go
  10. 2
      collector/netdev_openbsd.go
  11. 2
      collector/netdev_openbsd_amd64.go

4
collector/arp_linux.go

@ -34,7 +34,7 @@ var (
) )
type arpCollector struct { type arpCollector struct {
deviceFilter netDevFilter deviceFilter deviceFilter
entries *prometheus.Desc entries *prometheus.Desc
logger log.Logger logger log.Logger
} }
@ -46,7 +46,7 @@ func init() {
// NewARPCollector returns a new Collector exposing ARP stats. // NewARPCollector returns a new Collector exposing ARP stats.
func NewARPCollector(logger log.Logger) (Collector, error) { func NewARPCollector(logger log.Logger) (Collector, error) {
return &arpCollector{ return &arpCollector{
deviceFilter: newNetDevFilter(*arpDeviceExclude, *arpDeviceInclude), deviceFilter: newDeviceFilter(*arpDeviceExclude, *arpDeviceInclude),
entries: prometheus.NewDesc( entries: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "arp", "entries"), prometheus.BuildFQName(namespace, "arp", "entries"),
"ARP entries by device", "ARP entries by device",

6
collector/netdev_filter.go → collector/device_filter.go

@ -17,12 +17,12 @@ import (
"regexp" "regexp"
) )
type netDevFilter struct { type deviceFilter struct {
ignorePattern *regexp.Regexp ignorePattern *regexp.Regexp
acceptPattern *regexp.Regexp acceptPattern *regexp.Regexp
} }
func newNetDevFilter(ignoredPattern, acceptPattern string) (f netDevFilter) { func newDeviceFilter(ignoredPattern, acceptPattern string) (f deviceFilter) {
if ignoredPattern != "" { if ignoredPattern != "" {
f.ignorePattern = regexp.MustCompile(ignoredPattern) f.ignorePattern = regexp.MustCompile(ignoredPattern)
} }
@ -35,7 +35,7 @@ func newNetDevFilter(ignoredPattern, acceptPattern string) (f netDevFilter) {
} }
// ignores returns whether the device should be ignored // ignores returns whether the device should be ignored
func (f *netDevFilter) ignored(name string) bool { func (f *deviceFilter) ignored(name string) bool {
return ((f.ignorePattern != nil && f.ignorePattern.MatchString(name)) || return ((f.ignorePattern != nil && f.ignorePattern.MatchString(name)) ||
(f.acceptPattern != nil && !f.acceptPattern.MatchString(name))) (f.acceptPattern != nil && !f.acceptPattern.MatchString(name)))
} }

4
collector/netdev_filter_test.go → collector/device_filter_test.go

@ -17,7 +17,7 @@ import (
"testing" "testing"
) )
func TestNetDevFilter(t *testing.T) { func TestDeviceFilter(t *testing.T) {
tests := []struct { tests := []struct {
ignore string ignore string
accept string accept string
@ -33,7 +33,7 @@ func TestNetDevFilter(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
filter := newNetDevFilter(test.ignore, test.accept) filter := newDeviceFilter(test.ignore, test.accept)
result := filter.ignored(test.name) result := filter.ignored(test.name)
if result != test.expectedResult { if result != test.expectedResult {

4
collector/ethtool_linux.go

@ -76,7 +76,7 @@ type ethtoolCollector struct {
entries map[string]*prometheus.Desc entries map[string]*prometheus.Desc
entriesMutex sync.Mutex entriesMutex sync.Mutex
ethtool Ethtool ethtool Ethtool
deviceFilter netDevFilter deviceFilter deviceFilter
infoDesc *prometheus.Desc infoDesc *prometheus.Desc
metricsPattern *regexp.Regexp metricsPattern *regexp.Regexp
logger log.Logger logger log.Logger
@ -100,7 +100,7 @@ func makeEthtoolCollector(logger log.Logger) (*ethtoolCollector, error) {
return &ethtoolCollector{ return &ethtoolCollector{
fs: fs, fs: fs,
ethtool: &ethtoolLibrary{e}, ethtool: &ethtoolLibrary{e},
deviceFilter: newNetDevFilter(*ethtoolDeviceExclude, *ethtoolDeviceInclude), deviceFilter: newDeviceFilter(*ethtoolDeviceExclude, *ethtoolDeviceInclude),
metricsPattern: regexp.MustCompile(*ethtoolIncludedMetrics), metricsPattern: regexp.MustCompile(*ethtoolIncludedMetrics),
logger: logger, logger: logger,
entries: map[string]*prometheus.Desc{ entries: map[string]*prometheus.Desc{

2
collector/netdev_bsd.go

@ -34,7 +34,7 @@ import (
*/ */
import "C" import "C"
func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error) { func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error) {
netDev := netDevStats{} netDev := netDevStats{}
var ifap, ifa *C.struct_ifaddrs var ifap, ifa *C.struct_ifaddrs

4
collector/netdev_common.go

@ -40,7 +40,7 @@ var (
type netDevCollector struct { type netDevCollector struct {
subsystem string subsystem string
deviceFilter netDevFilter deviceFilter deviceFilter
metricDescsMutex sync.Mutex metricDescsMutex sync.Mutex
metricDescs map[string]*prometheus.Desc metricDescs map[string]*prometheus.Desc
logger log.Logger logger log.Logger
@ -86,7 +86,7 @@ func NewNetDevCollector(logger log.Logger) (Collector, error) {
return &netDevCollector{ return &netDevCollector{
subsystem: "network", subsystem: "network",
deviceFilter: newNetDevFilter(*netdevDeviceExclude, *netdevDeviceInclude), deviceFilter: newDeviceFilter(*netdevDeviceExclude, *netdevDeviceInclude),
metricDescs: map[string]*prometheus.Desc{}, metricDescs: map[string]*prometheus.Desc{},
logger: logger, logger: logger,
}, nil }, nil

2
collector/netdev_darwin.go

@ -27,7 +27,7 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error) { func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error) {
netDev := netDevStats{} netDev := netDevStats{}
ifs, err := net.Interfaces() ifs, err := net.Interfaces()

4
collector/netdev_linux.go

@ -34,7 +34,7 @@ var (
procNetDevFieldSep = regexp.MustCompile(` +`) procNetDevFieldSep = regexp.MustCompile(` +`)
) )
func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error) { func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error) {
file, err := os.Open(procFilePath("net/dev")) file, err := os.Open(procFilePath("net/dev"))
if err != nil { if err != nil {
return nil, err return nil, err
@ -44,7 +44,7 @@ func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error
return parseNetDevStats(file, filter, logger) return parseNetDevStats(file, filter, logger)
} }
func parseNetDevStats(r io.Reader, filter *netDevFilter, logger log.Logger) (netDevStats, error) { func parseNetDevStats(r io.Reader, filter *deviceFilter, logger log.Logger) (netDevStats, error) {
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
scanner.Scan() // skip first header scanner.Scan() // skip first header
scanner.Scan() scanner.Scan()

4
collector/netdev_linux_test.go

@ -27,7 +27,7 @@ func TestNetDevStatsIgnore(t *testing.T) {
} }
defer file.Close() defer file.Close()
filter := newNetDevFilter("^veth", "") filter := newDeviceFilter("^veth", "")
netStats, err := parseNetDevStats(file, &filter, log.NewNopLogger()) netStats, err := parseNetDevStats(file, &filter, log.NewNopLogger())
if err != nil { if err != nil {
@ -70,7 +70,7 @@ func TestNetDevStatsAccept(t *testing.T) {
} }
defer file.Close() defer file.Close()
filter := newNetDevFilter("", "^💩0$") filter := newDeviceFilter("", "^💩0$")
netStats, err := parseNetDevStats(file, &filter, log.NewNopLogger()) netStats, err := parseNetDevStats(file, &filter, log.NewNopLogger())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

2
collector/netdev_openbsd.go

@ -31,7 +31,7 @@ import (
*/ */
import "C" import "C"
func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error) { func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error) {
netDev := netDevStats{} netDev := netDevStats{}
var ifap, ifa *C.struct_ifaddrs var ifap, ifa *C.struct_ifaddrs

2
collector/netdev_openbsd_amd64.go

@ -24,7 +24,7 @@ import (
"unsafe" "unsafe"
) )
func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error) { func getNetDevStats(filter *deviceFilter, logger log.Logger) (netDevStats, error) {
netDev := netDevStats{} netDev := netDevStats{}
mib := [6]_C_int{unix.CTL_NET, unix.AF_ROUTE, 0, 0, unix.NET_RT_IFLIST, 0} mib := [6]_C_int{unix.CTL_NET, unix.AF_ROUTE, 0, 0, unix.NET_RT_IFLIST, 0}

Loading…
Cancel
Save