Skip to content

Commit

Permalink
Refactor netclass_rtnl collector (#2528)
Browse files Browse the repository at this point in the history
* Refactor netclass_rtnl collector

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 authored Nov 29, 2022
1 parent 956a3f8 commit 13a5cc1
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 151 deletions.
46 changes: 46 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,49 @@ var ErrNoData = errors.New("collector returned no data")
func IsNoDataError(err error) bool {
return err == ErrNoData
}

// pushMetric helps construct and convert a variety of value types into Prometheus float64 metrics.
func pushMetric(ch chan<- prometheus.Metric, fieldDesc *prometheus.Desc, name string, value interface{}, valueType prometheus.ValueType, labelValues ...string) {
var fVal float64
switch val := value.(type) {
case uint8:
fVal = float64(val)
case uint16:
fVal = float64(val)
case uint32:
fVal = float64(val)
case uint64:
fVal = float64(val)
case int64:
fVal = float64(val)
case *uint8:
if val == nil {
return
}
fVal = float64(*val)
case *uint16:
if val == nil {
return
}
fVal = float64(*val)
case *uint32:
if val == nil {
return
}
fVal = float64(*val)
case *uint64:
if val == nil {
return
}
fVal = float64(*val)
case *int64:
if val == nil {
return
}
fVal = float64(*val)
default:
return
}

ch <- prometheus.MustNewConstMetric(fieldDesc, valueType, fVal, labelValues...)
}
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
Loading

0 comments on commit 13a5cc1

Please sign in to comment.