Skip to content

Commit

Permalink
refactor(linux): ♻️ use a shared preferences file across all network …
Browse files Browse the repository at this point in the history
…sensor workers
  • Loading branch information
joshuar committed Oct 29, 2024
1 parent 446857e commit 9535bd7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
16 changes: 16 additions & 0 deletions internal/linux/net/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 Joshua Rich <[email protected]>.
// SPDX-License-Identifier: MIT

package net

const (
preferencesID = "network_sensors"
loopbackDeviceName = "lo"
)

var defaultIgnoredDevices = []string{}

//nolint:lll
type WorkerPrefs struct {
IgnoredDevices []string `toml:"ignored_devices" comment:"list of prefixes to match for devices to ignore, for e.g., 'eth' will ignore all devices starting with eth"`
}
29 changes: 23 additions & 6 deletions internal/linux/net/linkSensors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Copyright (c) 2024 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
// Copyright 2024 Joshua Rich <[email protected]>.
// SPDX-License-Identifier: MIT

//revive:disable:unused-receiver
package net

import (
Expand Down Expand Up @@ -142,6 +141,7 @@ type AddressWorker struct {
nlconn *rtnetlink.Conn
donech chan struct{}
linux.EventSensorWorker
prefs WorkerPrefs
}

func (w *AddressWorker) Sensors(ctx context.Context) ([]sensor.Entity, error) {
Expand Down Expand Up @@ -227,6 +227,16 @@ func (w *AddressWorker) Events(ctx context.Context) (<-chan sensor.Entity, error
return sensorCh, nil
}

func (w *AddressWorker) ID() string {
return preferencesID
}

func (w *AddressWorker) DefaultPreferences() WorkerPrefs {
return WorkerPrefs{
IgnoredDevices: defaultIgnoredDevices,
}
}

func NewAddressWorker(_ context.Context) (*linux.EventSensorWorker, error) {
worker := linux.NewEventSensorWorker(addressWorkerID)

Expand Down Expand Up @@ -279,8 +289,15 @@ func (w *AddressWorker) filterAddress(msg rtnetlink.AddressMessage) *sensor.Enti
if err != nil {
return nil
}
// Ignore addresses from unwanted links.
if slices.Contains(ifaceFilters, link.Attributes.Name) {

if link.Attributes.Name == loopbackDeviceName {
return nil
}

// Skip ignored devices.
if slices.ContainsFunc(w.prefs.IgnoredDevices, func(e string) bool {
return strings.HasPrefix(link.Attributes.Name, e)
}) {
return nil
}

Expand Down
28 changes: 9 additions & 19 deletions internal/linux/net/networkRates.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) 2024 Joshua Rich <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
// Copyright 2024 Joshua Rich <[email protected]>.
// SPDX-License-Identifier: MIT

// Parts of the code for collecting stats was adapted from Prometheus:
// https://github.com/prometheus/node_exporter//collector/netdev_linux.go
Expand Down Expand Up @@ -47,10 +45,7 @@ const (
totalsName = "Total"
)

var (
sensorList = []netStatsType{bytesRecv, bytesSent, bytesRecvRate, bytesSentRate}
defaultIgnoredDevices = []string{"lo"}
)
var sensorList = []netStatsType{bytesRecv, bytesSent, bytesRecvRate, bytesSentRate}

// linkStats represents a link and its stats.
type linkStats struct {
Expand Down Expand Up @@ -198,16 +193,11 @@ func getTXAttributes(stats *rtnetlink.LinkStats64) map[string]any {
type netStatsWorker struct {
statsSensors map[string]map[netStatsType]*netStatsSensor
nlconn *rtnetlink.Conn
prefs netStatsWorkerPrefs
prefs WorkerPrefs
delta time.Duration
mu sync.Mutex
}

//nolint:lll
type netStatsWorkerPrefs struct {
IgnoredDevices []string `toml:"ignored_devices" comment:"list of prefixes to match for devices to ignore, for e.g., 'eth' will ignore all devices starting with eth"`
}

// updateTotals takes the total Rx/Tx bytes and updates the total sensors.
func (w *netStatsWorker) updateTotals(totalBytesRx, totalBytesTx uint64) {
stats := &rtnetlink.LinkStats64{
Expand Down Expand Up @@ -281,11 +271,11 @@ func (w *netStatsWorker) Sensors(_ context.Context) ([]sensor.Entity, error) {
}

func (w *netStatsWorker) ID() string {
return netRatesWorkerID
return preferencesID
}

func (w *netStatsWorker) DefaultPreferences() netStatsWorkerPrefs {
return netStatsWorkerPrefs{
func (w *netStatsWorker) DefaultPreferences() WorkerPrefs {
return WorkerPrefs{
IgnoredDevices: defaultIgnoredDevices,
}
}
Expand All @@ -302,7 +292,7 @@ func NewNetStatsWorker(ctx context.Context) (*linux.PollingSensorWorker, error)
go func() {
<-ctx.Done()

if err := conn.Close(); err != nil {
if err = conn.Close(); err != nil {
logging.FromContext(ctx).Debug("Could not close netlink connection.",
slog.String("worker", netRatesWorkerID),
slog.Any("error", err))
Expand Down Expand Up @@ -347,7 +337,7 @@ func (w *netStatsWorker) getLinkStats(links []rtnetlink.LinkMessage) []linkStats
}

// Ignore loopback.
if msg.Attributes.Name == "lo" {
if msg.Attributes.Name == loopbackDeviceName {
continue
}

Expand Down

0 comments on commit 9535bd7

Please sign in to comment.