-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(linux): ♻️ use a shared preferences file across all network …
…sensor workers
- Loading branch information
Showing
3 changed files
with
48 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ( | ||
|
@@ -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) { | ||
|
@@ -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) | ||
|
||
|
@@ -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 | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 { | ||
|
@@ -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{ | ||
|
@@ -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, | ||
} | ||
} | ||
|
@@ -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)) | ||
|
@@ -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 | ||
} | ||
|
||
|