Skip to content

Commit

Permalink
Refactor netclass_rtnl collector
Browse files Browse the repository at this point in the history
Merge the netclass_rtnl collector into the netclass collector.
* Disabled by default
* Followup to #2492

Signed-off-by: Ben Kochie <superq@gmail.com>
  • Loading branch information
SuperQ committed Nov 21, 2022
1 parent bba0e2e commit 6a1d0c9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 85 deletions.
34 changes: 17 additions & 17 deletions collector/fixtures/e2e-64k-page-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2355,67 +2355,67 @@ node_netstat_Udp_RcvbufErrors 9
# HELP node_netstat_Udp_SndbufErrors Statistic UdpSndbufErrors.
# TYPE node_netstat_Udp_SndbufErrors untyped
node_netstat_Udp_SndbufErrors 8
# HELP node_network_address_assign_type address_assign_type value of /sys/class/net/<iface>.
# HELP node_network_address_assign_type Network device property: address_assign_type
# TYPE node_network_address_assign_type gauge
node_network_address_assign_type{device="bond0"} 3
node_network_address_assign_type{device="eth0"} 3
# HELP node_network_carrier carrier value of /sys/class/net/<iface>.
# HELP node_network_carrier Network device property: carrier
# TYPE node_network_carrier gauge
node_network_carrier{device="bond0"} 1
node_network_carrier{device="eth0"} 1
# HELP node_network_carrier_changes_total carrier_changes_total value of /sys/class/net/<iface>.
# HELP node_network_carrier_changes_total Network device property: carrier_changes_total
# TYPE node_network_carrier_changes_total counter
node_network_carrier_changes_total{device="bond0"} 2
node_network_carrier_changes_total{device="eth0"} 2
# HELP node_network_carrier_down_changes_total carrier_down_changes_total value of /sys/class/net/<iface>.
# HELP node_network_carrier_down_changes_total Network device property: carrier_down_changes_total
# TYPE node_network_carrier_down_changes_total counter
node_network_carrier_down_changes_total{device="bond0"} 1
node_network_carrier_down_changes_total{device="eth0"} 1
# HELP node_network_carrier_up_changes_total carrier_up_changes_total value of /sys/class/net/<iface>.
# HELP node_network_carrier_up_changes_total Network device property: carrier_up_changes_total
# TYPE node_network_carrier_up_changes_total counter
node_network_carrier_up_changes_total{device="bond0"} 1
node_network_carrier_up_changes_total{device="eth0"} 1
# HELP node_network_device_id device_id value of /sys/class/net/<iface>.
# HELP node_network_device_id Network device property: device_id
# TYPE node_network_device_id gauge
node_network_device_id{device="bond0"} 32
node_network_device_id{device="eth0"} 32
# HELP node_network_dormant dormant value of /sys/class/net/<iface>.
# HELP node_network_dormant Network device property: dormant
# TYPE node_network_dormant gauge
node_network_dormant{device="bond0"} 1
node_network_dormant{device="eth0"} 1
# HELP node_network_flags flags value of /sys/class/net/<iface>.
# HELP node_network_flags Network device property: flags
# TYPE node_network_flags gauge
node_network_flags{device="bond0"} 4867
node_network_flags{device="eth0"} 4867
# HELP node_network_iface_id iface_id value of /sys/class/net/<iface>.
# HELP node_network_iface_id Network device property: iface_id
# TYPE node_network_iface_id gauge
node_network_iface_id{device="bond0"} 2
node_network_iface_id{device="eth0"} 2
# HELP node_network_iface_link iface_link value of /sys/class/net/<iface>.
# HELP node_network_iface_link Network device property: iface_link
# TYPE node_network_iface_link gauge
node_network_iface_link{device="bond0"} 2
node_network_iface_link{device="eth0"} 2
# HELP node_network_iface_link_mode iface_link_mode value of /sys/class/net/<iface>.
# HELP node_network_iface_link_mode Network device property: iface_link_mode
# TYPE node_network_iface_link_mode gauge
node_network_iface_link_mode{device="bond0"} 1
node_network_iface_link_mode{device="eth0"} 1
# HELP node_network_info Non-numeric data from /sys/class/net/<iface>, value is always 1.
# TYPE node_network_info gauge
node_network_info{address="01:01:01:01:01:01",broadcast="ff:ff:ff:ff:ff:ff",device="bond0",duplex="full",ifalias="",operstate="up"} 1
node_network_info{address="01:01:01:01:01:01",broadcast="ff:ff:ff:ff:ff:ff",device="eth0",duplex="full",ifalias="",operstate="up"} 1
# HELP node_network_mtu_bytes mtu_bytes value of /sys/class/net/<iface>.
# HELP node_network_mtu_bytes Network device property: mtu_bytes
# TYPE node_network_mtu_bytes gauge
node_network_mtu_bytes{device="bond0"} 1500
node_network_mtu_bytes{device="eth0"} 1500
# HELP node_network_name_assign_type name_assign_type value of /sys/class/net/<iface>.
# HELP node_network_name_assign_type Network device property: name_assign_type
# TYPE node_network_name_assign_type gauge
node_network_name_assign_type{device="bond0"} 2
node_network_name_assign_type{device="eth0"} 2
# HELP node_network_net_dev_group net_dev_group value of /sys/class/net/<iface>.
# HELP node_network_net_dev_group Network device property: net_dev_group
# TYPE node_network_net_dev_group gauge
node_network_net_dev_group{device="bond0"} 0
node_network_net_dev_group{device="eth0"} 0
# HELP node_network_protocol_type protocol_type value of /sys/class/net/<iface>.
# HELP node_network_protocol_type Network device property: protocol_type
# TYPE node_network_protocol_type gauge
node_network_protocol_type{device="bond0"} 1
node_network_protocol_type{device="eth0"} 1
Expand Down Expand Up @@ -2444,7 +2444,7 @@ node_network_receive_multicast_total{device="lo"} 0
node_network_receive_nohandler_total{device="lo"} 0
# HELP node_network_receive_packets_total Network device statistic receive_packets.
# TYPE node_network_receive_packets_total counter
# HELP node_network_speed_bytes speed_bytes value of /sys/class/net/<iface>.
# HELP node_network_speed_bytes Network device property: speed_bytes
# TYPE node_network_speed_bytes gauge
node_network_speed_bytes{device="eth0"} 1.25e+08
# HELP node_network_transmit_bytes_total Network device statistic transmit_bytes.
Expand All @@ -2469,7 +2469,7 @@ node_network_transmit_errs_total{device="lo"} 0
node_network_transmit_fifo_total{device="lo"} 0
# HELP node_network_transmit_packets_total Network device statistic transmit_packets.
# TYPE node_network_transmit_packets_total counter
# HELP node_network_transmit_queue_length transmit_queue_length value of /sys/class/net/<iface>.
# HELP node_network_transmit_queue_length Network device property: transmit_queue_length
# TYPE node_network_transmit_queue_length gauge
node_network_transmit_queue_length{device="bond0"} 1000
node_network_transmit_queue_length{device="eth0"} 1000
Expand Down
34 changes: 17 additions & 17 deletions collector/fixtures/e2e-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2377,67 +2377,67 @@ node_netstat_Udp_RcvbufErrors 9
# HELP node_netstat_Udp_SndbufErrors Statistic UdpSndbufErrors.
# TYPE node_netstat_Udp_SndbufErrors untyped
node_netstat_Udp_SndbufErrors 8
# HELP node_network_address_assign_type address_assign_type value of /sys/class/net/<iface>.
# HELP node_network_address_assign_type Network device property: address_assign_type
# TYPE node_network_address_assign_type gauge
node_network_address_assign_type{device="bond0"} 3
node_network_address_assign_type{device="eth0"} 3
# HELP node_network_carrier carrier value of /sys/class/net/<iface>.
# HELP node_network_carrier Network device property: carrier
# TYPE node_network_carrier gauge
node_network_carrier{device="bond0"} 1
node_network_carrier{device="eth0"} 1
# HELP node_network_carrier_changes_total carrier_changes_total value of /sys/class/net/<iface>.
# HELP node_network_carrier_changes_total Network device property: carrier_changes_total
# TYPE node_network_carrier_changes_total counter
node_network_carrier_changes_total{device="bond0"} 2
node_network_carrier_changes_total{device="eth0"} 2
# HELP node_network_carrier_down_changes_total carrier_down_changes_total value of /sys/class/net/<iface>.
# HELP node_network_carrier_down_changes_total Network device property: carrier_down_changes_total
# TYPE node_network_carrier_down_changes_total counter
node_network_carrier_down_changes_total{device="bond0"} 1
node_network_carrier_down_changes_total{device="eth0"} 1
# HELP node_network_carrier_up_changes_total carrier_up_changes_total value of /sys/class/net/<iface>.
# HELP node_network_carrier_up_changes_total Network device property: carrier_up_changes_total
# TYPE node_network_carrier_up_changes_total counter
node_network_carrier_up_changes_total{device="bond0"} 1
node_network_carrier_up_changes_total{device="eth0"} 1
# HELP node_network_device_id device_id value of /sys/class/net/<iface>.
# HELP node_network_device_id Network device property: device_id
# TYPE node_network_device_id gauge
node_network_device_id{device="bond0"} 32
node_network_device_id{device="eth0"} 32
# HELP node_network_dormant dormant value of /sys/class/net/<iface>.
# HELP node_network_dormant Network device property: dormant
# TYPE node_network_dormant gauge
node_network_dormant{device="bond0"} 1
node_network_dormant{device="eth0"} 1
# HELP node_network_flags flags value of /sys/class/net/<iface>.
# HELP node_network_flags Network device property: flags
# TYPE node_network_flags gauge
node_network_flags{device="bond0"} 4867
node_network_flags{device="eth0"} 4867
# HELP node_network_iface_id iface_id value of /sys/class/net/<iface>.
# HELP node_network_iface_id Network device property: iface_id
# TYPE node_network_iface_id gauge
node_network_iface_id{device="bond0"} 2
node_network_iface_id{device="eth0"} 2
# HELP node_network_iface_link iface_link value of /sys/class/net/<iface>.
# HELP node_network_iface_link Network device property: iface_link
# TYPE node_network_iface_link gauge
node_network_iface_link{device="bond0"} 2
node_network_iface_link{device="eth0"} 2
# HELP node_network_iface_link_mode iface_link_mode value of /sys/class/net/<iface>.
# HELP node_network_iface_link_mode Network device property: iface_link_mode
# TYPE node_network_iface_link_mode gauge
node_network_iface_link_mode{device="bond0"} 1
node_network_iface_link_mode{device="eth0"} 1
# HELP node_network_info Non-numeric data from /sys/class/net/<iface>, value is always 1.
# TYPE node_network_info gauge
node_network_info{address="01:01:01:01:01:01",broadcast="ff:ff:ff:ff:ff:ff",device="bond0",duplex="full",ifalias="",operstate="up"} 1
node_network_info{address="01:01:01:01:01:01",broadcast="ff:ff:ff:ff:ff:ff",device="eth0",duplex="full",ifalias="",operstate="up"} 1
# HELP node_network_mtu_bytes mtu_bytes value of /sys/class/net/<iface>.
# HELP node_network_mtu_bytes Network device property: mtu_bytes
# TYPE node_network_mtu_bytes gauge
node_network_mtu_bytes{device="bond0"} 1500
node_network_mtu_bytes{device="eth0"} 1500
# HELP node_network_name_assign_type name_assign_type value of /sys/class/net/<iface>.
# HELP node_network_name_assign_type Network device property: name_assign_type
# TYPE node_network_name_assign_type gauge
node_network_name_assign_type{device="bond0"} 2
node_network_name_assign_type{device="eth0"} 2
# HELP node_network_net_dev_group net_dev_group value of /sys/class/net/<iface>.
# HELP node_network_net_dev_group Network device property: net_dev_group
# TYPE node_network_net_dev_group gauge
node_network_net_dev_group{device="bond0"} 0
node_network_net_dev_group{device="eth0"} 0
# HELP node_network_protocol_type protocol_type value of /sys/class/net/<iface>.
# HELP node_network_protocol_type Network device property: protocol_type
# TYPE node_network_protocol_type gauge
node_network_protocol_type{device="bond0"} 1
node_network_protocol_type{device="eth0"} 1
Expand Down Expand Up @@ -2466,7 +2466,7 @@ node_network_receive_multicast_total{device="lo"} 0
node_network_receive_nohandler_total{device="lo"} 0
# HELP node_network_receive_packets_total Network device statistic receive_packets.
# TYPE node_network_receive_packets_total counter
# HELP node_network_speed_bytes speed_bytes value of /sys/class/net/<iface>.
# HELP node_network_speed_bytes Network device property: speed_bytes
# TYPE node_network_speed_bytes gauge
node_network_speed_bytes{device="eth0"} 1.25e+08
# HELP node_network_transmit_bytes_total Network device statistic transmit_bytes.
Expand All @@ -2491,7 +2491,7 @@ node_network_transmit_errs_total{device="lo"} 0
node_network_transmit_fifo_total{device="lo"} 0
# HELP node_network_transmit_packets_total Network device statistic transmit_packets.
# TYPE node_network_transmit_packets_total counter
# HELP node_network_transmit_queue_length transmit_queue_length value of /sys/class/net/<iface>.
# HELP node_network_transmit_queue_length Network device property: transmit_queue_length
# TYPE node_network_transmit_queue_length gauge
node_network_transmit_queue_length{device="bond0"} 1000
node_network_transmit_queue_length{device="eth0"} 1000
Expand Down
10 changes: 9 additions & 1 deletion collector/netclass_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
var (
netclassIgnoredDevices = kingpin.Flag("collector.netclass.ignored-devices", "Regexp of net devices to ignore for netclass collector.").Default("^$").String()
netclassInvalidSpeed = kingpin.Flag("collector.netclass.ignore-invalid-speed", "Ignore devices where the speed is invalid. This will be the default behavior in 2.x.").Bool()
netclassNetlink = kingpin.Flag("collector.netclass.netlink", "Use netlink to gather stats instead of /proc/net/dev.").Default("false").Bool()
)

type netClassCollector struct {
Expand Down Expand Up @@ -63,6 +64,13 @@ func NewNetClassCollector(logger log.Logger) (Collector, error) {
}

func (c *netClassCollector) Update(ch chan<- prometheus.Metric) error {
if *netclassNetlink {
return c.netClassRTNLUpdate(ch)
}
return c.netClassSysfsUpdate(ch)
}

func (c *netClassCollector) netClassSysfsUpdate(ch chan<- prometheus.Metric) error {
netClass, err := c.getNetClassInfo()
if err != nil {
if errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrPermission) {
Expand Down Expand Up @@ -132,7 +140,7 @@ func (c *netClassCollector) getFieldDesc(name string) *prometheus.Desc {
if !exists {
fieldDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, c.subsystem, name),
fmt.Sprintf("%s value of /sys/class/net/<iface>.", name),
fmt.Sprintf("Network device property: %s", name),
[]string{"device"},
nil,
)
Expand Down
59 changes: 9 additions & 50 deletions collector/netclass_rtnl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"errors"
"fmt"
"io/fs"
"regexp"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/jsimonetti/rtnetlink"
"github.com/mdlayher/ethtool"
Expand All @@ -31,37 +29,14 @@ import (
)

var (
netclassRTNLIgnoredDevices = kingpin.Flag("collector.netclass_rtnl.ignored-devices", "Regexp of net devices to ignore for netclass_rtnl collector.").Default("^$").String()
netclassRTNLWithStats = kingpin.Flag("collector.netclass_rtnl.with-stats", "Expose the statistics for each network device, replacing netdev collector.").Bool()
operstateStr = []string{
netclassRTNLWithStats = kingpin.Flag("collector.netclass_rtnl.with-stats", "Expose the statistics for each network device, replacing netdev collector.").Bool()
operstateStr = []string{
"unknown", "notpresent", "down", "lowerlayerdown", "testing",
"dormant", "up",
}
)

type netClassRTNLCollector struct {
subsystem string
ignoredDevicesPattern *regexp.Regexp
metricDescs map[string]*prometheus.Desc
logger log.Logger
}

func init() {
registerCollector("netclass_rtnl", defaultDisabled, NewNetClassRTNLCollector)
}

// NewNetClassCollector returns a new Collector exposing network class stats.
func NewNetClassRTNLCollector(logger log.Logger) (Collector, error) {
pattern := regexp.MustCompile(*netclassRTNLIgnoredDevices)
return &netClassRTNLCollector{
subsystem: "network",
ignoredDevicesPattern: pattern,
metricDescs: map[string]*prometheus.Desc{},
logger: logger,
}, nil
}

func (c *netClassRTNLCollector) Update(ch chan<- prometheus.Metric) error {
func (c *netClassCollector) netClassRTNLUpdate(ch chan<- prometheus.Metric) error {
linkModes := make(map[string]*ethtool.LinkMode)
lms, err := c.getLinkModes()
if err != nil {
Expand Down Expand Up @@ -146,7 +121,7 @@ func (c *netClassRTNLCollector) Update(ch chan<- prometheus.Metric) error {
pushMetric(ch, c.getFieldDesc("transmit_queue_length"), "transmit_queue_length", msg.Attributes.TxQueueLen, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("protocol_type"), "protocol_type", msg.Type, prometheus.GaugeValue, msg.Attributes.Name)

// skip statistics if argument collector.netclass_rtnl.with-stats is false or statistics are unavailable.
// Skip statistics if argument collector.netclass_rtnl.with-stats is false or statistics are unavailable.
if netclassRTNLWithStats == nil || !*netclassRTNLWithStats || msg.Attributes.Stats64 == nil {
continue
}
Expand All @@ -162,22 +137,22 @@ func (c *netClassRTNLCollector) Update(ch chan<- prometheus.Metric) error {
pushMetric(ch, c.getFieldDesc("multicast_total"), "multicast_total", msg.Attributes.Stats64.Multicast, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("collisions_total"), "collisions_total", msg.Attributes.Stats64.Collisions, prometheus.GaugeValue, msg.Attributes.Name)

// detailed rx_errors
// Detailed rx_errors.
pushMetric(ch, c.getFieldDesc("receive_length_errors_total"), "receive_length_errors_total", msg.Attributes.Stats64.RXLengthErrors, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("receive_over_errors_total"), "receive_over_errors_total", msg.Attributes.Stats64.RXOverErrors, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("receive_crc_errors_total"), "receive_crc_errors_total", msg.Attributes.Stats64.RXCRCErrors, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("receive_frame_errors_total"), "receive_frame_errors_total", msg.Attributes.Stats64.RXFrameErrors, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("receive_fifo_errors_total"), "receive_fifo_errors_total", msg.Attributes.Stats64.RXFIFOErrors, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("receive_missed_errors_total"), "receive_missed_errors_total", msg.Attributes.Stats64.RXMissedErrors, prometheus.GaugeValue, msg.Attributes.Name)

// detailed tx_errors
// Detailed tx_errors.
pushMetric(ch, c.getFieldDesc("transmit_aborted_errors_total"), "transmit_aborted_errors_total", msg.Attributes.Stats64.TXAbortedErrors, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("transmit_carrier_errors_total"), "transmit_carrier_errors_total", msg.Attributes.Stats64.TXCarrierErrors, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("transmit_fifo_errors_total"), "transmit_fifo_errors_total", msg.Attributes.Stats64.TXFIFOErrors, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("transmit_heartbeat_errors_total"), "transmit_heartbeat_errors_total", msg.Attributes.Stats64.TXHeartbeatErrors, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("transmit_window_errors_total"), "transmit_window_errors_total", msg.Attributes.Stats64.TXWindowErrors, prometheus.GaugeValue, msg.Attributes.Name)

// for cslip etc
// For cslip, etc.
pushMetric(ch, c.getFieldDesc("receive_compressed_total"), "receive_compressed_total", msg.Attributes.Stats64.RXCompressed, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("transmit_compressed_total"), "transmit_compressed_total", msg.Attributes.Stats64.TXCompressed, prometheus.GaugeValue, msg.Attributes.Name)
pushMetric(ch, c.getFieldDesc("receive_nohandler_total"), "receive_nohandler_total", msg.Attributes.Stats64.RXNoHandler, prometheus.GaugeValue, msg.Attributes.Name)
Expand All @@ -187,23 +162,7 @@ func (c *netClassRTNLCollector) Update(ch chan<- prometheus.Metric) error {
return nil
}

func (c *netClassRTNLCollector) getFieldDesc(name string) *prometheus.Desc {
fieldDesc, exists := c.metricDescs[name]

if !exists {
fieldDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, c.subsystem, name),
fmt.Sprintf("Network device property %s.", name),
[]string{"device"},
nil,
)
c.metricDescs[name] = fieldDesc
}

return fieldDesc
}

func (c *netClassRTNLCollector) getNetClassInfoRTNL() ([]rtnetlink.LinkMessage, error) {
func (c *netClassCollector) getNetClassInfoRTNL() ([]rtnetlink.LinkMessage, error) {
conn, err := rtnetlink.Dial(nil)
if err != nil {
return nil, err
Expand All @@ -216,7 +175,7 @@ func (c *netClassRTNLCollector) getNetClassInfoRTNL() ([]rtnetlink.LinkMessage,

}

func (c *netClassRTNLCollector) getLinkModes() ([]*ethtool.LinkMode, error) {
func (c *netClassCollector) getLinkModes() ([]*ethtool.LinkMode, error) {
conn, err := ethtool.New()
if err != nil {
return nil, err
Expand Down

0 comments on commit 6a1d0c9

Please sign in to comment.