Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions collector/bonding_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ func readBondingStats(root string) (status map[string][2]int, err error) {
if err != nil {
return nil, err
}
for _, master := range strings.Fields(string(masters)) {
for master := range strings.FieldsSeq(string(masters)) {
slaves, err := os.ReadFile(filepath.Join(root, master, "bonding", "slaves"))
if err != nil {
return nil, err
}
sstat := [2]int{0, 0}
for _, slave := range strings.Fields(string(slaves)) {
for slave := range strings.FieldsSeq(string(slaves)) {
state, err := os.ReadFile(filepath.Join(root, master, fmt.Sprintf("lower_%s", slave), "bonding_slave", "mii_status"))
if errors.Is(err, os.ErrNotExist) {
// some older? kernels use slave_ prefix
Expand Down
2 changes: 1 addition & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func IsNoDataError(err error) bool {
}

// 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) {
func pushMetric(ch chan<- prometheus.Metric, fieldDesc *prometheus.Desc, name string, value any, valueType prometheus.ValueType, labelValues ...string) {
var fVal float64
switch val := value.(type) {
case uint8:
Expand Down
4 changes: 2 additions & 2 deletions collector/cpufreq_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (c *cpuFreqCollector) Update(ch chan<- prometheus.Metric) error {
)
}
if stats.Governor != "" {
availableGovernors := strings.Split(stats.AvailableGovernors, " ")
for _, g := range availableGovernors {
availableGovernors := strings.SplitSeq(stats.AvailableGovernors, " ")
for g := range availableGovernors {
state := 0
if g == stats.Governor {
state = 1
Expand Down
4 changes: 2 additions & 2 deletions collector/ethtool_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (e *EthtoolFixture) Stats(intf string) (map[string]uint64, error) {

func readModes(modes string) uint32 {
var out uint32
for _, mode := range strings.Split(modes, " ") {
for mode := range strings.SplitSeq(modes, " ") {
switch mode {
case "10baseT/Half":
out |= (1 << unix.ETHTOOL_LINK_MODE_10baseT_Half_BIT)
Expand All @@ -162,7 +162,7 @@ func readModes(modes string) uint32 {

func readPortTypes(portTypes string) uint32 {
var out uint32
for _, ptype := range strings.Split(portTypes, " ") {
for ptype := range strings.SplitSeq(portTypes, " ") {
ptype = strings.Trim(ptype, " \t")
if ptype == "TP" {
out |= (1 << unix.ETHTOOL_LINK_MODE_TP_BIT)
Expand Down
5 changes: 1 addition & 4 deletions collector/filesystem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
statChan := make(chan filesystemStats)
wg := sync.WaitGroup{}

workerCount := *statWorkerCount
if workerCount < 1 {
workerCount = 1
}
workerCount := max(*statWorkerCount, 1)

for i := 0; i < workerCount; i++ {
wg.Add(1)
Expand Down
8 changes: 3 additions & 5 deletions collector/hwmon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -154,11 +155,8 @@ func collectSensorData(dir string, data map[string]map[string]string) error {
continue
}

for _, t := range hwmonSensorTypes {
if t == sensorType {
addValueFile(data, sensorType+strconv.Itoa(sensorNum), sensorProperty, filepath.Join(dir, file.Name()))
break
}
if slices.Contains(hwmonSensorTypes, sensorType) {
addValueFile(data, sensorType+strconv.Itoa(sensorNum), sensorProperty, filepath.Join(dir, file.Name()))
}
}
return nil
Expand Down
19 changes: 9 additions & 10 deletions collector/logind_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"log/slog"
"os"
"slices"
"strconv"

"github.com/godbus/dbus/v5"
Expand Down Expand Up @@ -138,10 +139,8 @@ func collectMetrics(ch chan<- prometheus.Metric, c logindInterface) error {
}

func knownStringOrOther(value string, known []string) string {
for i := range known {
if value == known[i] {
return value
}
if slices.Contains(known, value) {
return value
}

return "other"
Expand Down Expand Up @@ -176,19 +175,19 @@ func newDbus() (*logindDbus, error) {
}

func (c *logindDbus) listSeats() ([]string, error) {
var result [][]interface{}
var result [][]any
err := c.object.Call(dbusObject+".Manager.ListSeats", 0).Store(&result)
if err != nil {
return nil, err
}

resultInterface := make([]interface{}, len(result))
resultInterface := make([]any, len(result))
for i := range result {
resultInterface[i] = result[i]
}

seats := make([]logindSeatEntry, len(result))
seatsInterface := make([]interface{}, len(seats))
seatsInterface := make([]any, len(seats))
for i := range seats {
seatsInterface[i] = &seats[i]
}
Expand All @@ -209,19 +208,19 @@ func (c *logindDbus) listSeats() ([]string, error) {
}

func (c *logindDbus) listSessions() ([]logindSessionEntry, error) {
var result [][]interface{}
var result [][]any
err := c.object.Call(dbusObject+".Manager.ListSessions", 0).Store(&result)
if err != nil {
return nil, err
}

resultInterface := make([]interface{}, len(result))
resultInterface := make([]any, len(result))
for i := range result {
resultInterface[i] = result[i]
}

sessions := make([]logindSessionEntry, len(result))
sessionsInterface := make([]interface{}, len(sessions))
sessionsInterface := make([]any, len(sessions))
for i := range sessions {
sessionsInterface[i] = &sessions[i]
}
Expand Down
9 changes: 3 additions & 6 deletions collector/netstat_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"log/slog"
"maps"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -73,12 +74,8 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) error {
}
// Merge the results of snmpStats into netStats (collisions are possible, but
// we know that the keys are always unique for the given use case).
for k, v := range snmpStats {
netStats[k] = v
}
for k, v := range snmp6Stats {
netStats[k] = v
}
maps.Copy(netStats, snmpStats)
maps.Copy(netStats, snmp6Stats)
for protocol, protocolStats := range netStats {
for name, value := range protocolStats {
key := protocol + "_" + name
Expand Down
2 changes: 1 addition & 1 deletion collector/perf_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func perfTracepointFlagToTracepoints(tracepointsFlag []string) ([]*perfTracepoin
func perfCPUFlagToCPUs(cpuFlag string) ([]int, error) {
var err error
cpus := []int{}
for _, subset := range strings.Split(cpuFlag, ",") {
for subset := range strings.SplitSeq(cpuFlag, ",") {
// First parse a single CPU.
if !strings.Contains(subset, "-") {
cpu, err := strconv.Atoi(subset)
Expand Down
8 changes: 4 additions & 4 deletions collector/sysctl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *sysctlCollector) Update(ch chan<- prometheus.Metric) error {

func (c *sysctlCollector) newMetrics(s *sysctl) ([]prometheus.Metric, error) {
var (
values interface{}
values any
length int
err error
)
Expand Down Expand Up @@ -151,7 +151,7 @@ func (s *sysctl) metricName() string {
return SanitizeMetricName(s.name)
}

func (s *sysctl) newConstMetric(v interface{}) prometheus.Metric {
func (s *sysctl) newConstMetric(v any) prometheus.Metric {
if s.numeric {
return prometheus.MustNewConstMetric(
prometheus.NewDesc(
Expand All @@ -171,7 +171,7 @@ func (s *sysctl) newConstMetric(v interface{}) prometheus.Metric {
)
}

func (s *sysctl) newIndexedMetrics(v interface{}) []prometheus.Metric {
func (s *sysctl) newIndexedMetrics(v any) []prometheus.Metric {
desc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, "sysctl", s.metricName()),
fmt.Sprintf("sysctl %s", s.name),
Expand All @@ -195,7 +195,7 @@ func (s *sysctl) newIndexedMetrics(v interface{}) []prometheus.Metric {
}
}

func (s *sysctl) newMappedMetrics(v interface{}) ([]prometheus.Metric, error) {
func (s *sysctl) newMappedMetrics(v any) ([]prometheus.Metric, error) {
switch values := v.(type) {
case []int:
metrics := make([]prometheus.Metric, len(values))
Expand Down
9 changes: 2 additions & 7 deletions collector/textfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"log/slog"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -91,13 +92,7 @@ func convertMetricFamily(metricFamily *dto.MetricFamily, ch chan<- prometheus.Me
}

for k := range allLabelNames {
present := false
for _, name := range names {
if k == name {
present = true
break
}
}
present := slices.Contains(names, k)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could even drop present and just run the check in the if statement.

if !present {
names = append(names, k)
values = append(values, "")
Expand Down
2 changes: 1 addition & 1 deletion collector/wifi_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ type mockWifiStater struct {
fixtures string
}

func (s *mockWifiStater) unmarshalJSONFile(filename string, v interface{}) error {
func (s *mockWifiStater) unmarshalJSONFile(filename string, v any) error {
b, err := os.ReadFile(filepath.Join(s.fixtures, filename))
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions collector/zfs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c *zfsCollector) updateZfsStats(subsystem string, ch chan<- prometheus.Met
}
defer file.Close()

return c.parseProcfsFile(file, c.linuxPathMap[subsystem], func(s zfsSysctl, v interface{}) {
return c.parseProcfsFile(file, c.linuxPathMap[subsystem], func(s zfsSysctl, v any) {
var valueAsFloat64 float64
switch value := v.(type) {
case int64:
Expand Down Expand Up @@ -212,7 +212,7 @@ func (c *zfsCollector) updatePoolStats(ch chan<- prometheus.Metric) error {
return nil
}

func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler func(zfsSysctl, interface{})) error {
func (c *zfsCollector) parseProcfsFile(reader io.Reader, fmtExt string, handler func(zfsSysctl, any)) error {
scanner := bufio.NewScanner(reader)

parseLine := false
Expand Down
22 changes: 11 additions & 11 deletions collector/zfs_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestArcstatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(arcstatsFile, "arcstats", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(arcstatsFile, "arcstats", func(s zfsSysctl, v any) {

if s == zfsSysctl("kstat.zfs.misc.arcstats.hits") {
if v.(uint64) != 8772612 {
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestZfetchstatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(zfetchstatsFile, "zfetchstats", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(zfetchstatsFile, "zfetchstats", func(s zfsSysctl, v any) {

if s != zfsSysctl("kstat.zfs.misc.zfetchstats.hits") {
return
Expand Down Expand Up @@ -108,7 +108,7 @@ func TestZilParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(zilFile, "zil", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(zilFile, "zil", func(s zfsSysctl, v any) {

if s != zfsSysctl("kstat.zfs.misc.zil.zil_commit_count") {
return
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestVdevCacheStatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(vdevCacheStatsFile, "vdev_cache_stats", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(vdevCacheStatsFile, "vdev_cache_stats", func(s zfsSysctl, v any) {

if s != zfsSysctl("kstat.zfs.misc.vdev_cache_stats.delegations") {
return
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestXuioStatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(xuioStatsFile, "xuio_stats", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(xuioStatsFile, "xuio_stats", func(s zfsSysctl, v any) {

if s != zfsSysctl("kstat.zfs.misc.xuio_stats.onloan_read_buf") {
return
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestFmParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(fmFile, "fm", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(fmFile, "fm", func(s zfsSysctl, v any) {

if s != zfsSysctl("kstat.zfs.misc.fm.erpt-dropped") {
return
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestDmuTxParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(dmuTxFile, "dmu_tx", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(dmuTxFile, "dmu_tx", func(s zfsSysctl, v any) {

if s != zfsSysctl("kstat.zfs.misc.dmu_tx.dmu_tx_assigned") {
return
Expand Down Expand Up @@ -417,7 +417,7 @@ func TestAbdstatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(abdstatsFile, "abdstats", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(abdstatsFile, "abdstats", func(s zfsSysctl, v any) {

if s != zfsSysctl("kstat.zfs.misc.abdstats.linear_data_size") {
return
Expand Down Expand Up @@ -453,7 +453,7 @@ func TestDbufstatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(dbufstatsFile, "dbufstats", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(dbufstatsFile, "dbufstats", func(s zfsSysctl, v any) {

if s != zfsSysctl("kstat.zfs.misc.dbufstats.hash_hits") {
return
Expand Down Expand Up @@ -489,7 +489,7 @@ func TestDnodestatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(dnodestatsFile, "dnodestats", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(dnodestatsFile, "dnodestats", func(s zfsSysctl, v any) {

if s != zfsSysctl("kstat.zfs.misc.dnodestats.dnode_hold_alloc_hits") {
return
Expand Down Expand Up @@ -525,7 +525,7 @@ func TestVdevMirrorstatsParsing(t *testing.T) {
}

handlerCalled := false
err = c.parseProcfsFile(vdevMirrorStatsFile, "vdev_mirror_stats", func(s zfsSysctl, v interface{}) {
err = c.parseProcfsFile(vdevMirrorStatsFile, "vdev_mirror_stats", func(s zfsSysctl, v any) {

if s != zfsSysctl("kstat.zfs.misc.vdev_mirror_stats.preferred_not_found") {
return
Expand Down
4 changes: 2 additions & 2 deletions node_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestFileDescriptorLeak(t *testing.T) {
if err != nil {
return err
}
for i := 0; i < 5; i++ {
for range 5 {
if err := queryExporter(address); err != nil {
return err
}
Expand Down Expand Up @@ -131,7 +131,7 @@ func runCommandAndTests(cmd *exec.Cmd, address string, fn func(pid int) error) e
return fmt.Errorf("failed to start command: %s", err)
}
time.Sleep(50 * time.Millisecond)
for i := 0; i < 10; i++ {
for i := range 10 {
if err := queryExporter(address); err == nil {
break
}
Expand Down
Loading