Rename netDevFilter helper

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

Signed-off-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
Ben Kochie 2022-05-19 10:31:48 +02:00
parent da8b0f694c
commit 1b6aaeb2e8
No known key found for this signature in database
GPG Key ID: C646B23C9E3245F1
11 changed files with 19 additions and 19 deletions

View File

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

View File

@ -17,12 +17,12 @@ import (
"regexp"
)
type netDevFilter struct {
type deviceFilter struct {
ignorePattern *regexp.Regexp
acceptPattern *regexp.Regexp
}
func newNetDevFilter(ignoredPattern, acceptPattern string) (f netDevFilter) {
func newDeviceFilter(ignoredPattern, acceptPattern string) (f deviceFilter) {
if 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
func (f *netDevFilter) ignored(name string) bool {
func (f *deviceFilter) ignored(name string) bool {
return ((f.ignorePattern != nil && f.ignorePattern.MatchString(name)) ||
(f.acceptPattern != nil && !f.acceptPattern.MatchString(name)))
}

View File

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

View File

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

View File

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

View File

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

View File

@ -27,7 +27,7 @@ import (
"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{}
ifs, err := net.Interfaces()

View File

@ -34,7 +34,7 @@ var (
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"))
if err != nil {
return nil, err
@ -44,7 +44,7 @@ func getNetDevStats(filter *netDevFilter, logger log.Logger) (netDevStats, error
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.Scan() // skip first header
scanner.Scan()

View File

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

View File

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

View File

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